JAXB2-maven only builds in target - jaxb

I'm on my first Java-Spring project. I need to communicate with a couple of webservices. I have some WSDL's provided, so i'm using Jax2B to autogenerate classes.
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.9.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>hello.wsdl</generatePackage>
<forceRegenerate>true</forceRegenerate>
<schemas>
<schema>
<url>http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl</url>
</schema>
</schemas>
</configuration>
</plugin>
My project is a web project. The problem here is, my classes are generated in the targets folder, and not in my project. Does somebody have ideas how to fix this? Classes are generated properly, but not at the proper directory. As you can see, i'm using a test wsdl and mockup names at the moment. I followed this tutorial: http://spring.io/guides/gs/consuming-web-service/
Many thanks in advance

Author of the maven-jaxb2-pugin here.
target/generated-sources/xjc IS the proper directory. This is how generated code is handled in Maven builds, you never generate anything into src/main/java.
The maven-jaxb2-plugin also adds this directory to the Maven's sources directories. You just have to make sure that this directory is considered a source directory by your IDE. In Eclipse, m2eclipse plugin does this automatically when you do "Update project".
See this part of the docs.

I use this plugin to add the classes generated to source....
<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

Karate tests in other folder than src/test

I want to move Karate tests from src/test to a new folder src/it because it makes sense to me to have integration tests separated from unit tests.
Is that possible?
I tried to put java classes in src/it/java and features in src/it/resources and Karate tests run, but without test cases.
Thanks in advance.
Issue was solved using a Maven plugin to add src/it/java as test sources and src/it/resources as test resources:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-test-source</id>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-resource</id>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/it/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Add generated sources to classpath

I use maven-jaxb2-plugin. It generate my classes in the correct directory, but on Eclipse Neon.2 Release (4.6.2), the folder is not automaticaly added to the classpath.
Here is my plugin config :
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<addCompileSourceRoot>true</addCompileSourceRoot>
<generateDirectory>${project.build.directory}/generated-sources/jaxb</generateDirectory>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<generatePackage>foo.bar.pojo</generatePackage>
</configuration>
</plugin>
</plugins>
</build>
Is it possible with maven-jaxb2-plugin to define the genearted directory as source folder ? If yes, how ?
Eclipse shows me an error in the pom.xml on <execution> :
Execution default of goal org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.13.1:generate failed: A required class was missing while executing org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.13.1:generate: com/sun/xml/bind/api/ErrorListener
To fix my problem, I had to change the version of the plugin.
From :
<version>0.13.1</version>
To :
<version>0.12.1</version>

jaxb2-annotate-plugin: Adding annotations to the XJC classpath

Is it possible to add custom annotations to the XJC classpath, while they are defined within my project itself? This when using the maven jaxb2-annotate-plugin.
The problem regards this piece of the documentation:
Annotation classes must be known in compile time. I.e. annotation classes must be made available in the XJC classpath. If you want to use your own annotations, you have to pre-compile them and add your annotation classes to the XJC classpath.
When I make a seperate project and add it to the plugin, it works fine as shown in the documentation examples.
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<id>generate</id>
<phase>process-resources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>1.0.2</version>
</plugin>
<plugin>
<groupId>com.acme</groupId>
<artifactId>external-project</artifactId>
<version>1.0.0</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
</plugin>
But I'd like to use the annotation that resides in the same project. How do I let it get picked up? If I compile it in a stage before process-resources, with the following piece:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>compile</id>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
The class gets compiled before the generation from my XSD's, but I still get the exception AnnotationClassNotFoundException. I'd like to avoid making seperate projects and/or modules just for adding annotations. How come it can't find my classes and the only way to provide annotations seems to be external projects/modules?
I have found the solution to my problem.
When configuring the maven-jaxb2-plugin plugin, it's possible to provide dependencies for that plugin. When providing my own project as the dependency, it can find the classes for it. So the following works out:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.1</version>
<executions>
<execution>
<id>generate</id>
<phase>process-resources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<args>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>1.0.2</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>current-project</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
It is required for your annotations to be compiled beforehand however. So to compile it beforehand, the following plugin should be added before the maven-jaxb2-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<phase>process-resources</phase>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
The problem with that is the first time of mvn clean install.
What if you haven't compiled it yet? For example go to your .m2 or mavel-local-repo and cancel your project and then do mvn clean install and see if it fails

jaxb2 goal is not invoked

I am using maven-jaxb2-plugin to generate some classes from xsd.
It is defined in child pom as follows:
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.0</version>
<executions>
<execution>
<id>jaxb2-generate</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<forceRegenerate>true</forceRegenerate>
<!-- Generate classes from XSD (XML Schema) using JAXB -->
<schemaDirectory>src/main/resources/com/reportcenter/settings/</schemaDirectory>
<generatePackage>com.reportcenter.settings</generatePackage>
<schemaIncludes>
<include>**/*.xsd</include>
</schemaIncludes>
<strict>false</strict>
<extension>true</extension>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.2</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.2</version>
</plugin>
</plugins>
<args>
<arg>-Xannotate</arg>
<arg>-XtoString</arg>
<arg>-Xcopyable</arg>
</args>
</configuration>
</plugin>
</plugins>
</pluginManagement>
The problem is the jaxb2 is not called from mvn install or mvn compile or mv generate-sources.
If I call mvn jaxb2:generate (as the name of the goal) the classes are created OK.
I looked at some questions here and used the answers provided, but I am still missing something.
Thank you.
Disclaimer: I am the author of the maven-jaxb2-plugin.
Seems like you only configure the plugin in pluginManagement but don't actualy use it in your build part.
This is how it should look like:
<project ...>
...
<build>
<plugins>
...
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
...
</project>
Few other comments on your configuration:
0.8.0 is a very old version, 0.12.3 is the actual one.
With modern Maven you no longer need to configure maven-compiler-plugin with source/target version 1.6.
Do not use forceRegenerate.
Consider using binding files instead of generatePackage.
Current version of jaxb2-basics is 0.9.2.
I ran into an error related to this plugin, and the Eclipse Maven integration.
Most searches for an answer were fruitless, but often lead to this thread. So, I'm going to post the issue, and a workaround here, for any others that run into it.
While using the maven-jaxb2-plugin:0.12.3 plugin correctly in maven on the command line - in Eclipse, source generation would fail with the following error:
Execution default of goal
org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.12.3:generate failed: A
required class was missing while executing
org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.12.3:generate:
com/sun/xml/bind/api/ErrorListener
Various attempts at adding jar files that contain the class it was looking for would just result in another class missing, going to a rabbit hole of jar file hell and mis-matched classes that wasn't resolvable.
I don't know if the problem here is something that is broken in the Eclipse / M2E integration, or if the problem is in the dependency stack of maven-jaxb2-plugin.
But the solution is to launch Eclipse itself (not the JREs / JDKs installed into eclipse) with a JDK, instead of a JRE.
The easiest way to do that is to add -vm parameter to your eclipse.ini file:
-startup plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar
-vm C:\Program Files\Java\jdk1.8.0_31\bin\javaw.exe
--launcher.library plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20150204-1316
-product
...snip...
Upgrade to version 0.13.2 +
Kudos to #lexicore for the comment below

How to generate the Schema Objectsfrom xsd present in external jar file using maven

I had prepared some xsd under src\main\resources folder structure and created a.jar file now I want to generate the scheam objects for xsd in a.jar in another application like generateSource application can anybody help me in writing the maven plugin in pom.xml file
like what all dependencies and plugins required.
1st you need to extract the jar which contains XSD using maven-dependency-plugin like this.
<plugin>
<!-- Unpack the jar into target directory -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>unpack</id>
<phase>validate</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${unpack.directory}</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
<includeGroupIds>com.companyname</includeGroupIds>
<includeArtifactIds>jarname</includeArtifactIds>
<excludes>**/*.html,samples/**</excludes>
</configuration>
</execution>
</executions>
Later you need to use maven-jaxb2-plugin to convert extracted xsds to java classes as mentioned below.
<plugin>
<!-- Convert XSD to java classes using jaxb plugin -->
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<extension>true</extension>
<generateDirectory>${generated.source.directory}</generateDirectory>
</configuration>
<executions>
<execution>
<id>Generate java-from-schema</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>${unpack.directory}/XXX</schemaDirectory>
<catalog>${unpack.directory}/catalog</catalog>
<schemaIncludes>
<schemaInclude>aaa/*.xsd</schemaInclude>
<schemaInclude>bbb/*.xsd</schemaInclude>
<schemaInclude>ccc/*.xsd</schemaInclude>
</schemaIncludes>
</configuration>
</execution>
</executions>
Please see the separate schema compilation documentation.

Resources