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>
Related
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>
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>
I have successfully generated metamodels and all metamodel classes are outputted in target/annotations directory.
But my problem is that my other classes such as DAOImpl doesn't recognize these metamodel generated classes. Any help please?
Here is how I generate metamodels using my maven project's pom.xml file:
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>4.3.4.Final</version>
</dependency>
</dependencies>
</plugin>
Check do you have set this:
<plugin> <!--Compiler instructions to generate model, add to sources.-->
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</compilerArguments>
</configuration>
</plugin>
<plugin><!--path were to generate model, add to -->
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<!-- source output directory -->
<outputDirectory>target/metamodel</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<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>target/metamodel</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
I think main problem is that folder where you generated classes isn't linked to project sources. You should tell your project that this directory is source folder or generate everything under src directory.
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.
As i've already described in title i have a problem in accessing generated classes from my source classes when i add #XmlRootElement annotation on resource -generation step.
My jaxb2-maven-plugin config:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/resources/schemas</schemaDirectory>
<packageName>my.classses</packageName>
<bindingDirectory>${project.basedir}/src/main/resources/xjb</bindingDirectory>
<extension>true</extension>
</configuration>
</plugin>
Any help will be useful, thanks in advance!
The generated source files are probably located under generated-sources which is not added as a source directory by default, try using something like:
<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/DIRNAME</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Just replace DIRNAME with the name of the directory the generated files are in.