mavenjaxb2xjcjaxb2-maven-pluginxjb

Using XJB with jaxb2-maven-plugin


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]

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.


Solution

  • 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.