Maven JAXB plugin executing only one excecution - java

I am trying to generated sources from two XSD schemas. My JAXB maven plugin looks like this:
<plugin>
<groupId>com.sun.tools.xjc.maven2</groupId>
<artifactId>maven-jaxb-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>GenerateKenexa</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<includeBindings>
<includeBinding>**/jaxb-bindings-kenexa.xml</includeBinding>
</includeBindings>
<includeSchemas>
<includeSchema>**/KenexaXMLConfiguration.xsd</includeSchema>
</includeSchemas>
</configuration>
</execution>
<execution>
<id>GenerateTalentQ</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<includeBindings>
<includeBinding>**/jaxb-bindings-talentq.xml</includeBinding>
</includeBindings>
<includeSchemas>
<includeSchema>**/TalentQXMLConfiguration.xsd</includeSchema>
</includeSchemas>
</configuration>
</execution>
</executions>
</plugin>
The first one gets generated fine. But the second one does not. I see in the maven output:
[INFO] --- maven-jaxb-plugin:1.1.1:generate (GenerateKenexa) # online.tests.management ---
[INFO] Compiling file:/D:/Projects/GTIWebApplications/gti_online_tests_management/src/main/resources/xsd/KenexaXMLConfiguration.xsd
[INFO] Writing output to D:\Projects\GTIWebApplications\gti_online_tests_management\target\generated-sources\xjc
[INFO]
[INFO] --- maven-jaxb-plugin:1.1.1:generate (GenerateTalentQ) # online.tests.management ---
[INFO] files are up to date
It says that files are up to date, but they aren't even generated. What might be wrong?

For people coming in to this question as I did, a year later :/
The problem persists in maven-jaxb2-plugin aswell, it's probably some sort of bug in 0.8.3.
When you generate the files into the same directory, the plugin "thinks" that the files have allready been generated and skips that second execution.
I found that in order to generate the second execution you will have to set the argument
<forceRegenerate>true</forceRegenerate>
In the configuration section.

I solved the problem. I have changed the maven jaxb plugin into maven jaxb2 plugin and now everything works. Now my maven configuration looks like this:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>GenerateKenexa</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<schemaIncludes>
<include>KenexaXMLConfiguration.xsd</include>
</schemaIncludes>
<generatePackage>com.groupgti.onlinetest.kenexa.jaxb</generatePackage>
<generateDirectory>${project.build.directory}/generated-sources/kenexa</generateDirectory>
</configuration>
</execution>
<execution>
<id>GenerateTalentQ</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<schemaIncludes>
<include>TalentQXMLConfiguration.xsd</include>
</schemaIncludes>
<generatePackage>com.groupgti.onlinetest.talentq.jaxb</generatePackage>
<generateDirectory>${project.build.directory}/generated-sources/talentq</generateDirectory>
</configuration>
</execution>
</executions>
</plugin>

I am using jaxb2 while still was facing the problem when I reached here. I added the below piece into config from other folks answers and it works now.
For previous answers the part that did the trick should be:
<generateDirectory>${project.build.directory}/generated-sources/kenexa</generateDirectory>
Also a unique execution id is needed
<id>GenerateKenexa</id>
But different directories make the code lies into two top level packages, so at last I am using:
<forceRegenerate>true</forceRegenerate>

First, I would recommend to specify separate output folders for each xsd <outputdirectory>${basedir}/target/generated-sources/xjc</outputdirectory>
And second, try to set it up as separate plugin entries, no separate executions:
<plugin>
<groupId>com.sun.tools.xjc.maven2</groupId>
...
<includeSchema>**/KenexaXMLConfiguration.xsd...
...
</plugin>
<plugin>
<groupId>com.sun.tools.xjc.maven2</groupId>
...
<includeSchema>**/TalentQXMLConfiguration.xsd...
...
</plugin>

Related

How do I tell Maven to not disregard namespace attribute in imported XSD?

I am using the mojohaus jaxb2-maven-plugin to generate Java sources out of xsd schema files. My pom.xml looks like this:
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>xjc-1</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>my.first.package.types</packageName>
<sources>
<source>src/main/java/META-INF/wsdl/firstSchema.xsd</source>
</sources>
</configuration>
</execution>
<execution>
<id>xjc-2</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>my.second.package.types</packageName>
<sources>
<source>src/main/java/META-INF/wsdl/secondSchema.xsd</source>
</sources>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>src/main/javagen</outputDirectory>
</configuration>
</plugin>
This plugin configuration should correspond to the one found here.
When I run the build, the generated source files from the first schema are also put into the second package. Can anyone explain to me why this is the case? Is that a bug or am I missing something?
Thanks a lot for any input!
Edit:
I tried the maven-jaxb2-plugin as well. Same result! So this seems to be a generell maven issue. My plugin configuration for the maven-jaxb2-plugin is as follows:
...
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
<executions>
<execution>
<id>first</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaIncludes>
<include>firstSchema.xsd</include>
</schemaIncludes>
<generatePackage>my.first.package.types</generatePackage>
</configuration>
</execution>
<execution>
<id>second</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaIncludes>
<include>secondSchema.xsd</include>
</schemaIncludes>
<generatePackage>my.second.package.types</generatePackage>
</configuration>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/java/META-INF/wsdl</schemaDirectory>
<generateDirectory>src/main/javagen</generateDirectory>
</configuration>
</plugin>
Does anyone have any ideas? This is starting to annoy me somewhat...
Edit:
I found out that this has to do with the fact that some xsd files have imported files like so:
<xs:import namespace="http://referenced/namespace"
schemaLocation="referencedSchema.xsd" />
Seems to me like Maven is ignoring the namespace tag. How can I tell Maven to stop doing that?
I can answer my own question from a very, very long time ago. The problem was that we also used the maven jaxws plugin to generate web services from wsdl files. Both plugins actually take the underlying xsf files and generate the data structure classes to the respective packages. So the solution is to remove the jaxb plugin from the pom. All xsds get only generated once.

How to specify Maven Goals' Parameter value

http://www.eclemma.org/jacoco/trunk/doc/prepare-agent-mojo.html
I am like really not familiar with maven at all. And the project that I am working on requires it....
I am trying to customize this Jacoco tool in maven. Especially the "include" parameter for prepare-agent goal. I am testing a big project with about 4000 classes in many different packages. But the only coverage information that I need is only from 5-10 classes.
Any idea how I can specify something like this? Basically specify "include" while running test. Or do I have to specify it in the POM file?
"mvn jacoco:prepare-agent -Dinclude = "weka.associations.Apriori" test"
yes you can specify in the pom.xml file
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
<configuration>
<excludes>
<exclude>**/*_.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin
where exclude tag will include your exclusion list , the classes which you want yo exclude to get code coverage, right now , it will not exclude anything
Kindly use the new version of jacoco as it is old one which i have specified

JaCoCo with Maven - missing execution data file

We have a Maven multi module project consisting of a parent (HelloWorld) and different children (HelloWorldServices and HelloWorldPresentation) and use Jenkins to build.
The error after running the successful test is
[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:report (default-cli) # HelloWorldServices ---
[INFO] Skipping JaCoCo execution due to missing execution data file:/var/lib/jenkins/workspace/HelloWorld/HelloWorldServices/target/jacoco.exec
The lines before it says
[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (default-cli) # HelloWorldServices ---
[INFO] argLine set to -javaagent:/var/lib/jenkins/.m2/repository/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=/var/lib/jenkins/workspace/HelloWorld/HelloWorldServices/target/jacoco.exec
This is how I defined the parent pom JaCoCo plugin:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<destfile>${project.artifactId}/target/jacoco.exec</destfile>
<datafile>${project.artifactId}/target/jacoco.exec</datafile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
In no pom did I explicitly mention surefire. I also tried what you find everywhere to put the argLine in the configuration but all with the same result. The JaCoCo .exec file has never been created, no matter what I do. As for the goals, I use
mvn clean install jacoco:prepare-agent jacoco:report
Since when I omit the jacoco goals, it doesn't even display the INFO message.
You should not invoke the agent after the install phase but before, so instead of invoking:
mvn clean install jacoco:prepare-agent jacoco:report
You should invoke
mvn clean jacoco:prepare-agent install jacoco:report
The main reason is: the agent will not participate to the build lifecycle, the test phase will already be executed as part of the install phase, then Maven will execute the agent as per command line invocation, but it will be too late.
You should probably also change the plugin configuration above to:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Note: I removed the configuration section, because it was actually pointing to default values. Moreover, XML elements are case sensitives here, so your datafile element was simply ignored, it should have been dataFile instead. The same applies to destFile.
The prepare-agent goal is already using ${project.build.directory}/jacoco.exec as default destFile value, the same applied to the dataFile value for the report goal. The main reason for this change would be a more flexible and standard build, not relying on artifactId as project name (the default, but still not mandatory) and using the more generic ${project.build.directory} property instead to point directly at target.
Final note: make sure to configure the Jacoco Plugin executions within the build/plugins section and not build/pluginManagement/plugins section. The pluginManagement section is meant for governance and common harmonization of versions or configurations, but it will be ignored if the corresponding plugin would not be declared under build/plugins.
As per official Maven POM reference
pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.
(note: bold is mine)
JaCoCo reports get created from the execution data file.
If this file is not present then JaCoCo report goal skips the report creation.
So it is compulsory to create the execution data file.
Reasons due to which execution data file will not get created are the following
- Tests are not present.
- All tests are ignored.
- Surefire plugin is missing.
- JaCoCo's prepare-agent goal is not executed, which sets argLine which is needed to configure to surefire.
- Surefire plugin is not configured with JaCoCo's agent.
I think that "destfile" and "datafile" are case sensitive so try to replace them with "destFile" and "dataFile", maybe it'll work :)
I just dealt with this issue today and found out that this happening when I set the argLine parameter in Surefire plugin (which I needed to do for running JavaFx-related tests) which replaces the default even after you have prepare-agent in your jacoco-maven-plugin goals.
So basically, the solution is to add the original argLine and append your additional arg lines using #{argLine} as noted here:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<argLine>#{argLine} --enable-native-access ALL-UNNAMED --add-modules jdk.incubator.foreign</argLine>
</configuration>
</plugin>
It mentioned in that post to assign the jacoco agent argline variable ${surefire.argLine} but i think those steps are not necessary (at least not in my case).
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<configuration>
<target>
<propertyfile file="lombok.config">
<entry key="config.stopBubbling" value="true" />
<entry key="lombok.addLombokGeneratedAnnotation" value="true" />
</propertyfile>
</target>
<excludes>
<exclude>**/domain/**</exclude>
<exclude>**/enumeration/**</exclude>
<exclude>**/exception/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<!-- Add this checking -->
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>65%</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
I share with you my response may be someone else work for him
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<!-- <configuration>
<excludes>
<exclude>package you want exclude</exclude>
</excludes>
</configuration>-->
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<!-- <execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.9</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>-->
</executions>
</plugin>

Maven generated-sources/annotations

Hey guy's I'm posting this question having spent a good deal of time researching and not found a detailed answer. Currently I'm having an issue with generating sources from AWS Workflow. I'm using Maven apt-maven-plugin and aspectj-maven-plugin. These plugins both work for generating the client classes for the activities yet fail with the following error when running mvn clean package or mvn clean install against my workflow classes.
Error
[ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-
plugin:1.7:compile (default) on project (myproject): Execution default
of goal org.codehaus.mojo:aspectj-maven-plugin:1.7:compile failed:
basedir (myproject)\target\generated-sources\annotations does not exist
-> [Help 1]
Plugins
<groupId>org.codehaus.mojo</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.0-alpha-5</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-flow-build-tools</artifactId>
</aspectLibrary>
</aspectLibraries>
<complianceLevel>1.7</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<sources>
<source>
<basedir>${basedir}/target/generated-sources/annotations</basedir>
</source>
<source>
<basedir>src/main/java</basedir>
<includes>
<include>**/*.java</include>
</includes>
</source>
</sources>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
I'm not sure how to go about fixing this issue and any help would be great.
The error message is clear- mvn could not find /target/generated-sources/annotations, but in pom it is stated as the source for aspectj-maven-plugin.
Does your code intend to generate sources under /target/generated-sources/annotations? If yes, then there is issue with generation, you'll need to expose more of your pom for me to tell what went wrong. If no, why not remove this part and give it another shot.
<source>
<basedir>${basedir}/target/generated-sources/annotations</basedir>
</source>
ps: I'd rather put this as comment but I'm not able to :(

Maven2 compiler custom execution source directory and target directory

I want to run the maven compiler plugin in a different phase and with different sourceDirectories and destinationDirectories such that code from directories other than src/main/java and src/test/java can be used.
I thought the solution would look something like the below, where the phase I was linking it to was pre-integration-test. However the properties for testSourceDirectory and testOutputDirectory don't seem to be specified in this way as they are in the section of the POM.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile mytests</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<testSourceDirectory>${basedir}/src/inttest/java</testSourceDirectory>
<testOutputDirectory>${basedir}/target/inttest-classes</testOutputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Is there a way to get this plug-in to compile different directories in different phases without affecting its default operation?
The source directories are set outside the compiler-plugin inside the <build> element, so this won't work.
You can use the build-helper-maven-plugin's add-source and add-test-source to specify additional source directories for your integration tests, but this will not remove the existing source dirs.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-it-source</id>
<phase>pre-integration-test</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/inttest/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
If you bind the add-test-source goal to run just before the testCompile goal, your integration tests will be included. Note you want them to be output to target/test-classes so the surefire plugin will find them.
To handle removal of the standard test sources, I wrote a small plugin to modify the model to remove existing testSource locations before adding the ones for integration tests.
After more research it is apparent this is not actually possible in Maven 2 in the way I want, a hack of some form is necessary to introduce integration tests. While you can add additional directories (as suggested by Rich Seller) there is no plugin to remove the other sources or to compile a directory separately from the main compilation.
The best solution I have found for adding integration tests is to first use the build helper plugin to add the directory inttest directory to be compiled as tests.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/inttest/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Now in order to get the integration tests to execute on the integration-test phase you need to use excludes and includes to manipulate when they get run as below. This allow any custom parameters you might want (in my case an agent being added via argline).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/itest/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>inttests</id>
<goals>
<goal>test</goal>
</goals>
<phase>integration-test</phase>
<configuration>
<excludes><exclude>none</exclude></excludes>
<includes>
<include>**/itest/**/*Test.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>

Categories

Resources