Using XJB with jaxb2-maven-plugin - maven

I have a multi-module maven project in the following structure:
root-module
|__module-a
| |__src
| |__main
| |__xsd
| | |__my.xsd
| |__xjb
| |__my.xjb
|__module-b
The POM for root module simply aggregates module a and b (among other things):
<project>
<artifactId>root-module</artifactId>
<packaging>pom</packaging>
<modules>
<module>module-a</module>
<module>module-b</module>
</modules>
</project>
And the POM for module a is as follows (among other things):
<project>
<parent>
<artifactId>root-module</artifactId>
</parent>
<artifactId>module-a</artifactId>
<properties>
<my-definitions.xsd>${basedir}/src/main/xsd/my.xsd</my-definitions.xsd>
<my-bindings.xjb>${basedir}/src/main/xjb/my.xjb</my-bindings.xjb>
<my.output>${basedir}/target/generated-sources/jaxb/my</my.output>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-my-classes</id>
<phase>generate-sources</phase>
<goals><goal>xjc</goal></goals>
<configuration>
<sources><source>${my-definitions.xsd}</source></sources>
<xjbSources><xjbSource>${my-bindings.xjb}</xjbSource></xjbSources>
<outputDirectory>${my.output}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
So when I run mvn at module-a, everything works fine and the build succeeds. But when I run it at root-module, I get an exception from the XJC plugin where it tries to find the bindings file under the root-module:
com.sun.istack.SAXParseException2; IOException thrown when processing "file:/home/root-module/src/main/xjb/my.xjb". Exception: java.io.FileNotFoundException: /home/root-module/src/main/xjb/my.xjb (The system cannot find the path specified).
What is interesting is, it is able to locate the XSD correctly:
[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.1:xjc (generate-my-classe) on project module-a:
[ERROR] +=================== [XJC Error]
[ERROR] |
[ERROR] | 0: file:/home/root-module/module-a/src/main/xsd/my.xsd
[ERROR] |
[ERROR] +=================== [End XJC Error]
Any clues?
Is this a configuration issue in the build script?
Specifics of my build system:
Using Maven 3.2.5
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.1</version>
Referring JAXB2 Maven plugin documentation from here.
Also searched few related questions on SO, but they do not explain my specific problem anywhere.
UPDATE: Looks like an open issue. Keeping the thread open in case there is a workaround.

Updating to version 2.2 of the plugin appears to work.
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
I had the same problem when using version 2.1 of the plugin. Simply changing to version 2.2 fixed the issue.

Until a plugin resolution is available, I am using the following ant-run hack:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-my-classes</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<mkdir dir="${project.build.directory}/generated-sources/jaxb/my" />
<exec executable="${env.JAVA_HOME}/bin/xjc.exe" dir="${project.basedir}/src/main/xsd">
<arg value="-p" />
<arg value="my.package" />
<arg value="-b" />
<arg value="${project.basedir}/src/main/xjb" />
<arg value="-d" />
<arg value="${project.build.directory}/generated-sources/jaxb" />
<arg value="." />
</exec>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
UPDATES:
Discussion on Github.
Considering Apache CXF Utils as an alternative.

Related

Detekt plugin not able to download from Maven central

I am working with Kotlin and want to do static analysis using Detekt plugin. My problem is, when I want to download it from Maven central repository, it is giving me below error. But when I use company's repository (Nexus), it does not give me any error and downloads it smoothly. I don't understand this.
Can someone tell me why this is happening? I want to use this plugin and have not found out any other alternative.
Below is my error :
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (detekt) on project agathe: Execution detekt of goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run failed: Plugin org.apache.maven.plugins:maven-antrun-plugin:1.8 or one of its dependencies could not be resolved: Could not find artifact com.beust:jcommander:jar:1.74 in central (https://repo.maven.apache.org/maven2) -> [Help 1]
Kindly find my POM file below.
<properties>
..
<sonar.kotlin.detekt.reportPaths>${project.build.directory}/detekt.xml</sonar.kotlin.detekt.reportPaths>
..
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<!-- This can be run separately with mvn antrun:run#detekt -->
<id>detekt</id>
<phase>verify</phase>
<configuration>
<target name="detekt">
<java taskname="detekt" dir="${basedir}"
fork="true"
failonerror="false"
classname="io.gitlab.arturbosch.detekt.cli.Main"
classpathref="maven.plugin.classpath">
<arg value="--input"/>
<arg value="${basedir}/src"/>
<arg value="--filters"/>
<arg value=".*/target/.*,.*/resources/.*"/>
<arg value="--report"/>
<arg value="xml:${project.build.directory}/detekt.xml"/>
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>io.gitlab.arturbosch.detekt</groupId>
<artifactId>detekt-cli</artifactId>
<version>1.0.0-RC14</version>
</dependency>
</dependencies>
</plugin>

Using build-helper-maven-plugin

I have the pom.xml below.
I'd like to pass a tag property to my build, using this command:
mvn clean package -Dtag=test
It should split this property into two others, my.group an my.version, and then use it in a URI to build an XLDeploy package, using the xldeploy-maven-plugin (https://docs.xebialabs.com/xldeploy-maven-plugin/6.0.x/).
My problem is that the regex-properties goal is actually doing the job, as I can see thanks to the maven-antrun-plugin:
[INFO] --- maven-antrun-plugin:1.1:run (default) # myArtifactId ---
[INFO] Executing tasks
[echo] Displaying value of 'my.group' property
[echo] [my.group] group/test
[echo] Displaying value of 'my.version' property
[echo] [my.version] test
But the command grep fileUri target\deployit-working-dir\deployit-manifest.xml show that the vars in Uri does not get replaced:
grep fileUri target\deployit-working-dir\deployit-manifest.xml
<fileUri>http://mynexus.ur/service/local/repositories/my-repo/content/${my.group}/anArtefact/${my.version}/anArtefact-1.0.zip</fileUri>
The POM is the following file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myGroupId</groupId>
<artifactId>myArtifactId</artifactId>
<version>1.0</version>
<packaging>dar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>regex-properties</id>
<goals>
<goal>regex-properties</goal>
</goals>
<configuration>
<regexPropertySettings>
<regexPropertySetting>
<name>my.group</name>
<value>group/${tag}</value>
<regex>(asm-[0-9]+)-([0-9]+.[0-9]+)-([0-9]+)$</regex>
<replacement>$1</replacement>
<failIfNoMatch>false</failIfNoMatch>
</regexPropertySetting>
<regexPropertySetting>
<name>my.version</name>
<value>${tag}</value>
<regex>(asm-[0-9]+)-([0-9]+.[0-9]+)-([0-9]+)$</regex>
<replacement>$1-SNAPSHOT</replacement>
<failIfNoMatch>false</failIfNoMatch>
</regexPropertySetting>
</regexPropertySettings>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Displaying value of 'my.group' property</echo>
<echo>[my.group] ${my.group}</echo>
<echo>Displaying value of 'my.version' property</echo>
<echo>[my.version] ${my.version}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.xebialabs.xldeploy</groupId>
<artifactId>xldeploy-maven-plugin</artifactId>
<version>6.0.0</version>
<extensions>true</extensions>
<configuration>
<deployables>
<file.Folder name="file">
<scanPlaceholders>false</scanPlaceholders>
<fileUri>http://mynexus.ur/service/local/repositories/my-repo/content/${my.group}/anArtefact/${my.version}/anArtefact-${project.version}.zip</fileUri>
</file.Folder>
</deployables>
</configuration>
</plugin>
</plugins>
</build>
</project>
I'm not quite sure if the build-helper-maven-plugin wrong, or anywhere else in my POM, or if it's simply lack of replacing properties in the xldeploy-maven-plugin...
Thanks for the help ;)
I checked the source of xldeploy-maven-plugin-6.0.1, and it seems it is the limitation of the current implementation.
The problem is that GenerateDeploymentPackageMojo does not rely on maven to do the substitution of the properties. Instead, it uses a custom AbstractConfigurationConverter named DeployitCIConverter (which delegates to MavenDeployableConverter) to convert the <deployables> node to a list of MavenDeployable)
This boils down to:
you have the access to effective POM
you don't have access to properties defined dynamically via build helper plugin
Of course the properties defined dynamically can be acessed in any mojo:
if you use a parameter
if you evaluate them manually via expression evaluator (check: get mojo parameters in maven plugin)
For the antrun plugin, it uses ExpressionEvaluator explicitely in the echo task.
Informative article:
Properties resolution in Maven and its implications on Antrun plugin
Ideas to fix the problem:
do your group and version parsing in external script and pass the values to mvn command.
create a cutom mojo extending GenerateDeploymentPackageMojo and preprocess deployables in the execute method (not that hard as it sounds).

Wrapping Ant in Maven - JAVA_HOME points to the JRE but works with just Ant

I have an Ant project that builds just fine on its own. I'm now trying to wrap it in a Maven build that will kick off the Ant build using maven-antrun-plugin. When I do this the build fails and I get this error,
[ERROR] C:\Users\bobby\workspace\libraries\build-targets\common-targets.xml:170: Unable to find a javac compiler;
[ERROR] com.sun.tools.javac.Main is not on the classpath.
[ERROR] Perhaps JAVA_HOME does not point to the JDK.
[ERROR] It is currently set to "C:\Java\jdk1.8.0_65\jre"
There are a lot of SOF posts on this error but I feel like mine is unique since it only happens when I'm wrapping the Ant build in Maven i.e., I do not get this error on the same project when I just say $ ant build.
This is part of my pom.xml file
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<tasks>
<ant antfile="build.xml" target="build" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-jar</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>build/bin/myWarFile.war</file>
<type>war</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
My JAVA_HOME Environment Variable is set to C:\Java\jdk1.8.0_65.
The file that is mentioned in the error is from a library my work maintains where we keep all of our Jars. In that file here is what's on line 170
<target name="compile-src">
<!-- Compile source -->
<javac srcdir="${java.src.dir}"
destdir="${class.dir}"
debug="${debug.flag}"
deprecation="${deprecation.flag}"
nowarn="${warnings.flag}"
optimize="off"
source="${source.value}">
<classpath refid="compile.classpath"/>
</javac>
</target>
The line with source= is line 170.
It's a common issue. Try with this configuration:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
...
<!-- Add this dependency to your ant-run configuration -->
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
...
</plugin>
Maven uses Java's system property java.home, which is not the same as the environment variable JAVA_HOME, but it is using it to compute its java.home by tacking on the jre sub-directory, as witnessed. Consequently, stuff needed by Ant is simply not available in the jre directory.
However, this configuration ensures that Ant's plugin dependencies are correctly satisfied.
You need to point to JDK not JRE. Just remove ire and try.
It is currently set to "C:\Java\jdk1.8.0_65\jre"
And if your JDK is set - another workaround - Can you copy tools.jar from jdk lib to jre lib and see if it works.

How to generate classes from wsdl using Maven and wsimport?

When I attempt to run "mvn generate-sources" this is my output :
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building gensourcesfromwsdl 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.104s
[INFO] Finished at: Tue Aug 20 15:41:10 BST 2013
[INFO] Final Memory: 2M/15M
[INFO] ------------------------------------------------------------------------
I do not receive any errors but there are no java classes generated from the wsdl file.
Here is my pom.xml file that I'm running the plugin against :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>gensourcesfromwsdl</groupId>
<artifactId>gensourcesfromwsdl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlLocation>http://mysite/firstwsdl.asmx?wsdl</wsdlLocation>
<packageName>com</packageName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
What am I doing wrong ? The package com exists in the project 'gensourcesfromwsdl' and the wsdl location is valid.
When I run wsimport via the command line : >wsimport -keep -verbose http://mysite/firstwsdl.asmx?wsdl the class is generated.
To generate classes from WSDL, all you need is build-helper-maven-plugin and jaxws-maven-plugin in your pom.xml
Make sure you have placed wsdl under folder src/main/resources/wsdl and corresponding schema in src/main/resources/schema, run command "mvn generate-sources" from Project root directory.
C:/Project root directory > mvn generate-sources
generated java classes can be located under folder
target/generated/src/main/java/com/raps/code/generate/ws.
pom.xml snippet
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>${project.build.directory}/generated/src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>1.12</version>
<configuration>
<wsdlDirectory>${project.basedir}/src/main/resources/wsdl</wsdlDirectory>
<packageName>com.raps.code.generate.ws</packageName>
<keep>true</keep>
<sourceDestDir>${project.build.directory}/generated/src/main/java</sourceDestDir>
</configuration>
<executions>
<execution>
<id>myImport</id>
<goals><goal>wsimport</goal></goals>
</execution>
</executions>
</plugin>
Here is an example of how to generate classes from wsdl with jaxws maven plugin from a url or from a file location (from wsdl file location is commented).
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<build>
<plugins>
<!-- usage of jax-ws maven plugin-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>wsimport-from-jdk</id>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- using wsdl from an url -->
<wsdlUrls>
<wsdlUrl>
http://myWSDLurl?wsdl
</wsdlUrl>
</wsdlUrls>
<!-- or using wsdls file directory -->
<!-- <wsdlDirectory>src/wsdl</wsdlDirectory> -->
<!-- which wsdl file -->
<!-- <wsdlFiles> -->
<!-- <wsdlFile>myWSDL.wsdl</wsdlFile> -->
<!--</wsdlFiles> -->
<!-- Keep generated files -->
<keep>true</keep>
<!-- Package name -->
<packageName>com.organization.name</packageName>
<!-- generated source files destination-->
<sourceDestDir>target/generatedclasses</sourceDestDir>
</configuration>
</plugin>
</plugins>
</build>
Even though this is bit late response, may be helpful for someone. Look like you have used pluginManagement. If you use pluginManagement
, it will not pick the plug-in execution.
It should be under
<build>
<plugins>
<plugin>
Try to wrap wsdlLocation in wsdlUrls
<wsdlUrls>
<wsdlLocation>http://url</wsdlLocation>
</wsdlUrls>
I see some people prefer to generate sources into the target via jaxws-maven-plugin AND make this classes visible in source via build-helper-maven-plugin. As an argument for this structure
the version management system (svn/etc.) would always notice changed
sources
With git it is not true. So you can just configure jaxws-maven-plugin to put them into your sources, but not under the target folder. Next time you build your project, git will not mark these generated files as changed. Here is the simple solution with only one plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.6</version>
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-fluent-api</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-tools</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<packageName>som.path.generated</packageName>
<xjcArgs>
<xjcArg>-Xfluent-api</xjcArg>
</xjcArgs>
<verbose>true</verbose>
<keep>true</keep> <!--used by default-->
<sourceDestDir>${project.build.sourceDirectory}</sourceDestDir>
<wsdlDirectory>src/main/resources/META-INF/wsdl</wsdlDirectory>
<wsdlLocation>META-INF/wsdl/soap.wsdl</wsdlLocation>
</configuration>
</execution>
</executions>
</plugin>
Additionally (just to note) in this example SOAP classes are generated with Fluent API, so you can create them like:
A a = new A()
.withField1(value1)
.withField2(value2);
The key here is keep option of wsimport. And it is configured using element in
About keep from the wsimport documentation :
-keep keep generated files
i was having the same issue while generating the classes from wsimport goal. Instead of using jaxws:wsimport goal in eclipse Maven Build i was using clean compile install that was not able to generate code from wsdl file. Thanks to above example.
Run jaxws:wsimport goal from Eclipse ide and it will work

bring -f option to multi module

there is -f option in maven, that allows to specify alternate pom.xml file. Is there a possibility, that I can also bring this behaviour to the executed modules? Now it looks like, that when I have this structure: projectA: pom.xml pom.xml2
projectB: pom.xml pom.xml2
And when I run maven with -f pom.xml2 option as reactor with projectB specified as module, it looks like that it picks pom.xml2 from the projectA, and it picks pom.xml from projectB. Is there a way, how can I propagate the -f option to the modules?
Thanks for answering.
Because we can specified pom file in module definition.1
Here's an example for using alternative pom file in module.
<modules>
<module>child1/pom-jdk14.xml</module>
<module>child2/pom-jdk14.xml</module>
</modules>
As Jörn Horstmann comments I would try lots of things to get this working with profiles in one pom.
If that's not possible the only way I can think of to get this working is to bypass the normal maven mechanism by using a "switching pom" with profiles. This pom is put as pom.xml in each module and has a profile for each of your pom.xml2 (or others) and in that profile executes another maven build f.e. via the antrun-plugin with the -f for the pom you need:
<profile>
<id>xml2</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>build pom.xml2</id>
<phase>prepare-package</phase> <!-- whatever suits you -->
<configuration>
<target>
<echo level="info" message="Building pom.xml2..." />
<exec executable="cmd" dir=".">
<arg value="/c" />
<arg value="mvn" />
<arg value="-f" />
<arg value="pom.xml2" />
<arg value="install" /> <!-- enter which phase you need -->
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

Resources