2 plugins not working simultaneously in pom - java

I have added 2 plugins under build tag, functionality of both the plugin's is to generate some classes under target folder. Whenever I am trying to clean install maven application, by default target gets clean each time and then installs a fresh content into target folder which is the ideal way.
But in the following code Java classes are generated only when, if there is only single plugin. I have to manually comment any one of the plugin and then I need to install maven goal and then Java classes get generated for a single plugin, same thing I need to repeat for second plugin.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/somefolder</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<id>somefolder</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlDirectory>src/main/webapp/WEB-INF/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>myfirstwsdl.wsdl</wsdlFile>
</wsdlFiles>
<wsdlLocation>/WEB-INF/wsdl/*</wsdlLocation>
<extension>true</extension>
<target>2.2</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<id>wsimport</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlDirectory>src/main/webapp/WEB-INF/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>mysecondwsdl.wsdl</wsdlFile>
</wsdlFiles>
<wsdlLocation>/WEB-INF/wsdl/*</wsdlLocation>
<extension>true</extension>
<target>2.2</target>
</configuration>
</plugin>
</plugins>
</build>
My question is, how can I generate Java classes simultaneously without commenting any one of the plugin under target folder?

You're specifying the same plugin twice, that's not going to work. You need to merge the two like this (move <configuration> inside <execution>):
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<id>somefolder</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlDirectory>src/main/webapp/WEB-INF/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>myfirstwsdl.wsdl</wsdlFile>
</wsdlFiles>
<wsdlLocation>/WEB-INF/wsdl/*</wsdlLocation>
<extension>true</extension>
<target>2.2</target>
</configuration>
</execution>
<execution>
<id>wsimport</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlDirectory>src/main/webapp/WEB-INF/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>mysecondwsdl.wsdl</wsdlFile>
</wsdlFiles>
<wsdlLocation>/WEB-INF/wsdl/*</wsdlLocation>
<extension>true</extension>
<target>2.2</target>
</configuration>
</execution>
</executions>
</plugin>

Related

How do I add additional source directories to maven to generate javadoc?

I used this in my pom.xml to recognize the second source directory I have in my project:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/java-extra/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
But generating javadoc fails saying it can't find the classes in the java-extra directory. How can I make maven javadoc see the second directory?
There is a sourcepath parameter for maven-javadoc-plugin. There you provide list of your source folders, separated by : or ;. See https://maven.apache.org/plugins/maven-javadoc-plugin/javadoc-mojo.html#sourcepath
For example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<sourcepath>src/main/java;${defines.dir}</sourcepath>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

How to generate java classes into source folder using jaxb2-maven-plugin?

I use jaxb2-maven-plugin to generate java classes.
There is plugin properties:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- The package of your generated sources -->
<packageName>com.bcap.me.JaxB</packageName>
<sources>
<source>src/main/resources/xsds/pos.xsd</source>
</sources>
</configuration>
</plugin>
After running mvn clean compile the plugin creates classes in the target\classes\com\bcap\me\JaxB directory.
But i need to have classes in the source folder (package): src\main\java\com\bcap\me\JaxB
How to do this?
UPDATE
I add outputDirectory property, but i am not sure about the correctness of this approach:
<!--<packageName>com.bcap.me.JaxB</packageName>-->
<outputDirectory>src/main/java/com/bcap/me/JaxB</outputDirectory>
UPDATE
I solved my case like:
<execution>
<id>xjc_pos</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<!-- The package of your generated sources -->
<packageName>com.bcap.me.JaxB</packageName>
<outputDirectory>src/main/java</outputDirectory>
<sources>
<source>src/main/resources/xsds/pos.xsd</source>
</sources>
<generateEpisode>false</generateEpisode>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
Thanks to #ulab
You could use following maven plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/xjc</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

Can I generate JPA metamodels and QueryDSL models in same project

I have a project where we have code implemented using JPA and we use metamodels. To generate hibernate metamodels we use following as a maven plaugin
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>process-sources</phase>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/annotations/</outputDirectory>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>
Now we like to try out QueryDSL, and I added the maven configuration to generate QueryDSL-models. It looks like following.
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
But now when I run maven clean test it complains that JPA Hibernate metamodels are missing. But if I switch the order then it complaints that other metamodels are missing. So is there a way to use both of them in same porject?
I guess your problem is not the generation itself, but that you have classes in your codebase that depend on the generated classes.
Have you tried to add the generation output directories as source directories like this?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/annotations</source>
<source>${project.build.directory}/generated-sources/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Should work for mvn on CLI as well for m2e in Eclipse.

maven-jaxb2-plugin VS jaxb2-maven-plugin for multiple schemas

I have multiple xsd schemas that I want to unmarshall into different packages under the same folder target/generated-sources/xjc. I tried both plugins and both seem to work fine with these 2 configurations but in case of maven-jaxb2-plugin the eclipse plugin keeps generating classes indefinitely (because of the forceRegenerate = true) but if I don't specify forceRegenerate it won't generate the second and third set of classes at all when I run mvn clean package Are there any issues with my configuration?
jaxb2-maven-plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>xjc-scores</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.generated.scores</packageName>
<schemaDirectory>src/main/resources/schemas/scores</schemaDirectory>
</configuration>
</execution>
<execution>
<id>xjc-videos-ramp</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.generated.ramp</packageName>
<schemaDirectory>src/main/resources/schemas/ramp</schemaDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
<execution>
<id>xjc-schedules</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.generated.schedules</packageName>
<schemaDirectory>src/main/resources/schemas/schedules</schemaDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
</executions>
<configuration>
</configuration>
</plugin>
maven-jaxb2-plugin
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<id>xjc-scores</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.generated.scores</generatePackage>
<schemaDirectory>src/main/resources/schemas/scores</schemaDirectory>
<removeOldOutput>true</removeOldOutput>
</configuration>
</execution>
<execution>
<id>xjc-ramp</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.generated.ramp</generatePackage>
<schemaDirectory>src/main/resources/schemas/ramp</schemaDirectory>
<removeOldOutput>false</removeOldOutput>
</configuration>
</execution>
<execution>
<id>xjc-schedules</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.generated.schedules</generatePackage>
<schemaDirectory>src/main/resources/schemas/schedules</schemaDirectory>
<removeOldOutput>false</removeOldOutput>
</configuration>
</execution>
</executions>
<configuration>
<forceRegenerate>true</forceRegenerate>
</configuration>
</plugin>
and the build-helper-maven-plugin config:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/xjc</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-resource</id>
<phase>generate-sources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>target/generated-sources/xjc</directory>
<targetPath>target/classes</targetPath>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
General advice: specify your packages in bindings.xjb rather than in different executions with individual generatePackages.
<jxb:bindings schemaLocation="common1.xsd" node="/xsd:schema">
<jxb:schemaBindings>
<jxb:package name="mypackage.commonclasses"/>
</jxb:schemaBindings>
</jxb:bindings>
generatePackage does not really work well with multiple schemas.
And please file a bug in
https://java.net/jira/browse/MAVEN_JAXB2_PLUGIN
citing the problem with the multiple schemas and Eclipse. I'll take a look into it.
ps. SO disclaimer: I'm the author of maven-jaxb2-plugin.
My solution:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>xjc-scores</id>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.generated.scores</packageName>
<schemaDirectory>src/main/resources/schemas/scores</schemaDirectory>
<clearOutputDir>true</clearOutputDir>
</configuration>
</execution>
<execution>
<id>xjc-videos-ramp</id>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.generated.ramp</packageName>
<schemaDirectory>src/main/resources/schemas/ramp</schemaDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
<execution>
<id>xjc-schedules</id>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>com.generated.schedules</packageName>
<schemaDirectory>src/main/resources/schemas/schedules</schemaDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
</executions>

How to execute multiple cmd commands using maven?

I want to run multiple CMD commands in maven using single pom.xml.
May I know how can I do that?
for example
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>id1</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cmd1</executable>
</configuration>
</execution>
<execution>
<id>id2</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cmd2</executable>
</configuration>
</execution>
</executions> </plugin>
You could introduce two different profiles for cmd1 and cmd2.
Maybe exec-maven-plugin:
run a script that will run your cmds sequentially::
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>run a backup</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>/path/to/backup-script/bkp.sh</executable>
</configuration>
</execution>
</executions>
</plugin>

Categories