Generated sources being compiled twice - maven

Using Eclipse Luna with m2eclipse, I have a parent Maven project (facturas_root) and two Maven modules inheriting from it (sharepoint_ws and api_sharepoint).
sharepoint_ws was to be used only to generate JAXWS classes to connect to the Sharepoint WebServices, so I downloaded the related WSDL and included those as resources of the project. At generate-sources phase, it works correctly and generates the sources in target\generated-sources\ws-consume\mypackage\.
Now, the issue is that I made api_sharepoint import the sharepoint_ws dependency, but it does not detect any class. I assumed that it was because the generated classes were not at src/main/java, so I added a plugin to copy them there. Now, the problem is that at the compile phase of sharepoint_ws, it finds twice the source file of each class and throws an error.
My pom.xml -> build
<plugins>
<!-- clean /src/main/java and /target/generated-sources -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>${basedir}/src/main/java/es</directory>
</fileset>
<fileset>
<directory>${basedir}/target/generated-sources</directory>
</fileset>
</configuration>
</execution>
</executions>
</plugin>
<!-- generate jaxws -->
<plugin>
<groupId>org.jboss.ws.plugins</groupId>
<artifactId>maven-jaxws-tools-plugin</artifactId>
<version>1.1.2.Final</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>wsconsume</goal>
</goals>
<configuration>
<wsdls>
<wsdl>${basedir}/resources/lists.wsdl</wsdl>
</wsdls>
<targetPackage>es.ssib.otic.facturas.sharepoint_ws</targetPackage>
<outputDirectory>${basedir}/target/generated-sources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- copy sources -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target/generated-sources/wsconsume</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
In order to try to exclude target/generated-sources I have addded this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<configuration>
<excludes>
<exclude>**/target/generated-sources/*.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
As stated above, I do comment the "copy" plugin, the module depending on sharepoint_ws does not have any ot its classes available; I do use it I get errors in the tune of
[ERROR] /C:/Users/s004256/workspace/facturas_root/sharepoint_ws/src/main/java/es/ssib/otic/facturas/sharepoint_ws/DeleteList.java:[34,8] duplicate class: es.ssib.otic.facturas.sharepoint_ws.DeleteList
for each generated list.

In the first place, I recommend you'd better declare target/generated-sources as a source folder, instead of copying files here and there:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>initialize</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
This should be enough to make Maven compile the target/generated-sources/*.java and package them all in the library, and also for Eclipse to recognize target/generated-sources as a source directory (after you execute Maven/Update Project).
By the way: You should take care of binding the plugins to a phase in the correct order: If you bound all tasks to "generate-sources", you have no gurantee about in which order will they be executed. And the same goes for the "compile" phase: You have to set properly the source folders, with its inclusions and exclusions, before the compile phase.
Take a look a the Default Maven Lifecycle and try to chose different, sequential phases for your tasks.

Related

Integration tests create files for the packaged jar

I'm generating spring-restdocs snippets with rest assured in my integration tests.
With maven and the failsafe plugin that defaults to the integration-test and verify phase. Problem is, that asciidoctor-maven-plugin (to generate the final HTML file out of the snippets) and maven-resources-plugin (to copy the final HTML file into the correct place) run before the integration tests in prepare-package.
With that asciidoctor naturally fails, because the snippets are not generated yet.
If I configure asciidoctor to run at post-integration-test, it succeeds, but then the finished HTML page is not in my jar, cause the jar was already created in the package phase.
So I feel like the only option is to run my integration tests already in the tests phase (probably with surefire instead of failsafe).
I could also split out the Documentation related tests from the rest of the integration tests, but I actually like to have them in the seemingly correct place.
I wonder if there is a best practice I'm ignoring?
Should integration tests never produce something to put inside the jar?
It looks to me, that integration tests in maven are meant for cross-jar tests (hence running them after package). And not like I'm using them, just for bigger tests that involve multiple parts (especially DB) all within one jar.
What I would like:
run all tests
compile documentation
package everything into the jar
Excerpt from my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skip>${skipSurefire}</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*IT.java</include>
<include>**/*E2ET.java</include>
</includes>
</configuration>
</plugin>
<!-- Compile API documentation -->
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.8</version>
<executions>
<execution>
<id>generate-docs</id>
<phase>prepare-package</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<backend>html</backend>
<doctype>book</doctype>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-asciidoctor</artifactId>
<version>${spring-restdocs.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- Package API documentation -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.outputDirectory}/public/docs
</outputDirectory>
<resources>
<resource>
<directory>
${project.build.directory}/generated-docs
</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
The phases integration-test and verify are just the default bindings of the integration-test and the verify goal. You can bind your integration tests to the test phase, as well:
...
<artifactId>maven-failsafe-plugin</artifactId>
...
<executions>
<execution>
<id>integration-tests</id>
<phase>test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
and such achieve the order you like/need.

How to skip version in jar generated by maven-jar-plugin and clasifier

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>client</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>client</classifier>
<includes>
<include>**/com.service/**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
I want the version to be appended to end of the jar.Right now it is building jar like ship-service-0.1-client.jar,I want it like ship-service-client-0.1.jar and i need to add it as dependency in another project.
You can use
<build>
<finalName>FinalNameOfYourProyect</finalName>
</build>
check this related question

Compile and execute a single file to generate code before compiling all the code together

A maven project with some generated files:
my.package.R // generated from resources
my.package.ClassThatGeneratesRFromResources
my.package.AClassThatUsesR
my.package.AnotherClassThatUsesR
my.package.YetAnotherClassThatUsesR
When building, I would like to compile ClassThatGeneratesRFromResources, execute it (thus generating the R.java class) and then compile everything else together.
I can make this work with modules, a reactor, and isolating ClassThatGeneratesRFromResources in its own little module. However, I wonder why my ugly, clunky solution is not working. Here is my current POM:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>
generate-r-file-creator
</id>
<configuration>
<phase>generate-sources</phase>
<goal>compile</goal>
<includes>
<include>my/package/ClassThatGeneratesRFromResources.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>generate-r-files</id>
<phase>process-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>my.package.ClassThatGeneratesRFromResources</mainClass>
<arguments>
<argument>${basedir}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The result is that the initial compiler run fails, and then the code-generation run fails too (because ClassThatGeneratesRFromResources has not been compiled). Why is my compiler plugin not being triggered? How can I fix this (other than going modular)?

How to add WSDL, XSD (and possibly other files) to WAR

I have a very simple pom.xml that generates a fully working web service if deployed locally (Tomcat 7). This is its <build> section:
<build>
<finalName>${artifact.id}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>generate-sources</id>
<configuration>
<sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/wsdl/mywebservice.wsdl</wsdl>
<extraargs>
<extraarg>-impl</extraarg>
<extraarg>-verbose</extraarg>
</extraargs>
<wsdlLocation>${basedir}/src/main/wsdl/mywebservice.wsdl</wsdlLocation>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${basedir}/src/main/wsdl/mywebservice.wsdl</file>
<type>wsdl</type>
</artifact>
<artifact>
<file>${basedir}/src/main/xsd/myschema.xsd</file>
<type>xsd</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The reason it only works if deployed locally and not when deployed to a remote server is because the remote server cannot find the .wsdl and the .xsd files in development source directories ${basedir}/target/generated/src/main/ and they are also nowhere to be found in the WAR file.
Apparently, I am missing something in my pom.xml that would make Maven add or attach those files to the WAR.
I tried the attach-artifact goal (as quoted above) but it only copies the files to my local (development) .m2 repository, not to the WAR file.
How do I add or attach files to the actual .war file to be deployed?
I solved the mystery.
Posting the answer here in case another newbie to Maven (plus CXF?) encounters this problem:
It turns out that the attach-artifact execution is totally unneeded and is a shot in the wrong direction.
All I had to do was to add at the top of the <build> section, just before <plugins> the following:
<resources>
<resource>
<directory>src/main/wsdl</directory>
</resource>
<resource>
<directory>src/main/xsd</directory>
</resource>
</resources>
That's all. Maven then automagically places all files in those directories in WEB-INF/classes/.

Maven - Excluding Resource from a specific artifact

I'm new to maven, and found myself stuck with something which is really bothering me.
I have a multi-module project, and my parent pom.xml contains the following plugin:
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<executions>
<execution>
<id>process-remote-resources</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>
${shared.resources.version}
</resourceBundle>
</resourceBundles>
<includes>
<include>version.info</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
This code generates a version.info file and places them in each of my module jar files.
I was wondering if it's possible to make this code create the version.info file for only 2 modules out of the 3.
For example if I have modules: A, B and C.
I would like the version.info file to be in A and B but not in C.
I hope I explained myself well enough, in case not please let me know.
Thanks in advance,
Meny
Best solution would be moving this plugin configuration to the project/build/pluginManagement element of the parent pom:
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<executions>
<execution>
<id>process-remote-resources</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>
${shared.resources.version}
</resourceBundle>
</resourceBundles>
<includes>
<include>version.info</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
and call it at project/build/plugins in module poms:
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
</plugin>
</plugins>
if such refactoring of parent pom is not allowed then override plugin configuration with skip set into true or use the none phase to disable unwanted plugin calls:
http://thomaswabner.wordpress.com/2010/02/16/howto-disable-inherited-maven-plugin/

Resources