GWT Maven plugin replace web.xml - java

I have the following maven configuration
my goal is to replace web-xml for development mode from usual web.xml to web-dev.xml
gwt-maven-plugin seems to have configuration wor it (webXml) but it's not working not as eclipse plugin (run as web application), not when mvn gwt:run.
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${gwtVersion}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<gwtSdkFirstInClasspath>true</gwtSdkFirstInClasspath>
<hostedWebapp>${webappDirectory}</hostedWebapp>
<runTarget>index.html</runTarget>
<style>PRETTY</style>
<webXml>src/main/webapp/WEB-INF/web-dev.xml</webXml>
<inplace>true</inplace>
</configuration>
</plugin>

Despite what the doc says, the webXml configuration parameter is actually only used for the mergewebxml goal (almost useless nowadays).
Try doing things the other way around: web.xml for dev, and a web-prod.xml for prod that you configure in the maven-war-plugin, as gwt:run will copy your src/main/webapp as-is, bypassing any configuration you could have made for the maven-war-plugin.

Related

jsonschema2pojo maven plugin configuration in execution not considered

I am trying to use the jsonschema2pojo plugin for generating POJOs based on both schema and json sourceTypes. The configurations are specified per execution. But every time the plugin is reporting "One of sourceDirectory or sourcePaths must be provided". I am able to run it when the configuration is provided at the plugin level ( global ). But then I can only specify one sourceType.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.5.1</version>
<executions>
<execution>
<id>generate-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<outputEncoding>${project.build.sourceEncoding}</outputEncoding>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
<annotationStyle>jackson2</annotationStyle>
<generateBuilders>false</generateBuilders>
<initializeCollections>true</initializeCollections>
<refFragmentPathDelimiters>#/</refFragmentPathDelimiters>
<sourceType>jsonschema</sourceType>
<targetPackage>com.company.app.integration.sabre.stub.rest</targetPackage>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
</configuration>
</execution>
<execution>
<id>generate-json</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<outputEncoding>${project.build.sourceEncoding}</outputEncoding>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
<annotationStyle>jackson2</annotationStyle>
<generateBuilders>false</generateBuilders>
<initializeCollections>true</initializeCollections>
<refFragmentPathDelimiters>#/</refFragmentPathDelimiters>
<sourceType>json</sourceType>
<targetPackage>com.company.app.integration.sabre.stub.rest</targetPackage>
<sourceDirectory>${basedir}/src/main/resources/json</sourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Is there any way to have the plugin use the configuration at execution level per goal ?
Plugin version: 0.5.1
tl;dr
When running the 'compile' from Maven projects lifecycle, the plugin is considering the configuration from execution and is working as expected.
I am using Intellij and was trying to generate the pojo from Plugins -> jsonschema2pojo -> jsonschema2pojo:generate under 'Maven Projects' window. This was giving the above error and was not taking the configuration per execution.
When I run compile from the Maven Lifecycle, it is picking the configuration in the execution and is generating the files as specified.
I am not yet sure if this is a issue with the plugin or maven or if its a issue at all !!
Try moving your configuration out to the plugin level and using the parent folder (${baseDir}/src/main/resources) as the sourceDirectory.
Here's an old bug report describing the same thing:
https://github.com/joelittlejohn/jsonschema2pojo/issues/145

add build tasks to maven pom project

An existing maven pom project <packaging>pom</packaging> which currently collects and packages resources needs to be extended to validate some of the resources.
In the same project I created a java-source directory src/main/java and in there I created a small java class to validate some of the resources. In addition I configured the maven-compiler and exec-maven plugin in the pom.
The java class runs fine in the IDE but it fails when I do mvn clean install it fails because it cant find the compiled class file. This is because the compile/test-compile phase is not available for pom-packaged projects.
My questions are:
Can I modify the compiler plugin to execute (compile) in a different phase than the default compile-phase. (I tried with adding an execution tag but no success)
Why is the exec-maven plugin executed because this was defined in test phase, which according to the docs is not part of the pom-package.
Are there other possibilities to run this validation task in the pom?
Modifying the packaging from pom to jar is a political sub-optimal solution.
Yes, you can configure maven-compiler-plugin to run the compilation in the package phase of the pom packaging.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<mainClass>com.example.validate.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>

Maven module for static files

I have a web application with spring and maven. The application is divided into maven modules. I have an "application-web" module that generates war. Another "application-ear" module that generates an ear. And another "application-static" with css, js and images that generates a zip file.
This would be the outline of my application:
application
application-web (java code)
application-ear
application-static (css, js and images)
application-resources (language properties)
I want to deploy the eclipse use files of "static" module, instead of using the files in the "application/src/main/webapp" directory. How can I do this? It is possible?
Don't waste your time with unpacking resources, everything works out of the box:
Resource bundles: the are always loaded from the classpath, regardlessly it is exploded (WEB-INF/classes) or compressed (WEB-INF/lib)
Serving static resources: bundle them up in a JAR and use either Tomcat (Servlet 3.0) feature to serve from META-INF/resources, see here or use Spring's built in mvc:resources element.
Simply add the dependency snippets in your WAR POM and your are done.
You can use the maven dependency plugin to unpack your static resources included in the zip file using the following:
In your application-web.pom
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-resources</id>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>resources</outputDirectory>
<includeArtifactIds>application-static</includeArtifactIds>
<includeGroupIds>your group id</includeGroupIds>
<includes>**/*.js,**/*.css</includes>
</configuration>
</execution>
</executions>
</plugin
This work:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<excludeTypes>pom</excludeTypes>
<includeArtifactIds>application-static</includeArtifactIds>
<includeGroupIds>com.foo.foo.application</includeGroupIds>
<outputDirectory>src/main/webapp</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>${project.artifactId}-${project.version}</finalName>
</build>
But I have another problem, with this solution only copy the static files when I execute clean package. If I run the application-web with eclipse and I do changes in a js file in application-static, the changes have no effect until I execute clean package. Any solution for this?

Integration testing of multi-war application in Spring Boot

I have an application composed of multiple maven war projects.
I have another maven project that runs JUnit integration tests against the manually-started tomcat-deployed multi-war application using org.springframework.web.client.RestTemplate calls.
However, I'd like my integration test project to actually start my multi-war application (once for the duration of the entire suite) before it runs my tests...in spring-boot!
From my integration test project, I'd like to be able to run all the war projects together as a spring-boot application, each with their own contextPaths (e.g. localhost:8080/a for project 'a', localhost:8080/b for project 'b', etc. ), and without changing the original war projects (that are not (yet) spring-boot aware). If I can't make these projects run from my integration test project in spring-boot without changing them then I'd at least like to minimize the use of spring-boot dependencies and configuration in packaged war files...as much as possible.
I was able to get my integration test project to depend on a single war project, start it up and run tests against it...but I was unsuccessful getting two war projects running together in spring-boot under separate contextPaths.
Any suggestions would be welcome!
Here are some of the resources I've been using to put this together:
(Spring-boot documentation) http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html
(Blog post touching on starting spring app once for test suite) http://www.nurkiewicz.com/2010/12/speeding-up-spring-integration-tests.html
(Suggestions for including war files as dependencies in a integration test project pom) http://eureka.ykyuen.info/2009/10/30/maven-dependency-on-jarwar-package/
As per Andy's suggestion, I went with the Tomcat7 Maven Plugin and it worked just fine. The Jetty Maven Plugin was another option (and better documented IMO) although I couldn't find a way to avoid having to provide a "path" to my WAR files. The Tomcat7 Maven Plugin, let me load up my WARs from my local .m2 repository. I should also say that the following links were helpful as well...
http://cupofjava.de/blog/2013/02/05/integration-tests-with-maven-and-tomcat/
https://stackoverflow.com/a/16936585/1098564
Here's part of my integration test project pom...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<configuration>
<includes>
<include>**/*Test*</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<webapps>
<webapp>
<groupId>com.mycompany</groupId>
<artifactId>app1</artifactId>
<version>${project.version}</version>
<type>war</type>
<asWebapp>true</asWebapp>
</webapp>
<webapp>
<groupId>com.mycompany</groupId>
<artifactId>app2</artifactId>
<version>${project.version}</version>
<type>war</type>
<asWebapp>true</asWebapp>
</webapp>
</webapps>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war</goal>
</goals>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Executing Maven unit tests with classpath set to generated project JAR

Some unit tests in my application are related to finding and manipulating certain files resources that are part of the application itself.
I need these tests to be executed in the real production setting of the application, where it is deployed as a JAR file, not as an exploded directory.
How could I instruct Maven to execute my unit tests considering as the classpath the project generated jar file (and any other declared library dependencies) instead of the compiled classes in the file system as it does by default?.
In other words, right now the classpath for my unit tests is set to: /$PROJECTPATH/target/classes/.
Instead, I would like this classpath to be set to: /$PROJECTPATH/target/myjarfile.jar.
I have tried manually adding and removing dependency classes, as explained here:
http://maven.apache.org/plugins/maven-surefire-plugin/examples/configuring-classpath.html
,but until now it is not working.
My current project POM looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.mygroupid</groupId>
<artifactId>myartifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-resources</phase>
<!-- <phase>package</phase> -->
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.3</version>
<configuration>
<classpathDependencyExcludes>
<classpathDependencyExclude>
${project.build.outputDirectory}
</classpathDependencyExclude>
</classpathDependencyExcludes>
<additionalClasspathElements>
<additionalClasspathElement>
${project.build.directory}/${project.build.finalName}.${project.packaging}
</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
</project>
Thanks in advance for any help!.
The standard unit tests executed as part of the test lifecycle phase cannot see the project JAR because the test phase is executed before the package phase, so your tests are run before Maven generates the JAR. See this page for a list of lifecycle phases and their order.
What you want it to run your tests as integration tests, which execute in the integration-test phase.
There are a number of tutorials for setting up Maven to run integration tests. Here and here are a couple of starters. The failsafe plugin is typically used for executing integration tests.
I can't recall exactly if integration tests use target/classes or your project's JAR file in the classpath. But if it doesn't you could always create another Maven project, add your tests in there and add the main project as a dependency to this integration test project. In some cases this can be preferable to using the integration test phase in the main project if it is not just a standard Java library, for example if you are writing an annotation processor.

Categories

Resources