2

I have a maven project in which jaxb2-maven-plugin (mojohaus one) is used to generate sources.

When invoked on Jenkins, it fails due to all generated classes being duplicated.

After some investigations, I could track it down to this configuration in maven-compiler-plugin

[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ my-maven-module ---
[DEBUG] Using compiler 'javac'.
[DEBUG] Source directories: [/var/lib/jenkins/jobs/MY_JENKINS_JOB/workspace/myProject/src/main/java
 /var/lib/jenkins/jobs/MY_JENKINS_JOB/workspace/myProject/target/generated/src/main/java
 /appli/projects/jenkins/jobs/MY_JENKINS_JOB/workspace/myProject/target/generated/src/main/java]

As it appears, the /var/lib/jenkins folder is linked to /appli/projects/jenkins. And, in jaxb2-maven-plugin, the configuration we set is

+=================== [16 XJC Arguments]
|
| [0]: -xmlschema
| [1]: -encoding
| [2]: UTF-8
| [3]: -p
| [4]: fr.erdf.sge.f5.asm
| [5]: -d
| [6]: /var/lib/jenkins/jobs/MY_JENKINS_JOB/workspace/myProject/target/generated/src/main/java
| [7]: -classpath
| [8]: /logiciels/maven/apache-maven-3.1.1/conf/logging/
| [9]: -extension
| [10]: -episode
| [11]: /appli/projects/jenkins/jobs/MY_JENKINS_JOB/workspace/myProject/target/generated/src/main/java/META-INF/sun-jaxb.episode
| [12]: -Xvisitor
| [13]: -Xvisitor-package:com.massfords.humantask
| [14]: m-maven-supermodule/my-maven-module/src/main/resources/xsd/asm/asmbuilder.xsd
| [15]: m-maven-supermodule/my-maven-module/src/main/resources/xsd/import_export_xml.xsd
|
+=================== [End 16 XJC Arguments]

We can see the difference is already present here : the -d argument refers to /var/lib/jenkins where the -episode argument refers to /appli/projects/jenkins/.

And it's at the end of this plugin execution that the generated source folder will be added to Jenkins path, as stated here

[DEBUG] Adding existing JAXB outputDirectory [/appli/projects/jenkins/jobs/MY_JENKINS_JOB/workspace/myProject/target/generated/src/main/java] to Maven's sources.

EDIT 1 Configuration of jaxb2 plugin is

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <configuration>
                <arguments>
                    <argument>-Xvisitor</argument>
                    <argument>-Xvisitor-package:com.massfords.humantask</argument>
                </arguments>
                <sources>
                    <source>${project.basedir}/src/main/resources/xsd/import_export_xml.xsd</source>
                    <source>${xsd}</source>
                </sources>
                <packageName>myPackage</packageName>
                <outputDirectory>${jaxb.src}</outputDirectory>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>com.massfords</groupId>
                    <artifactId>jaxb-visitor</artifactId>
                    <version>2.0</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Please notice the jaxb generation folder contains classes only once. It i's fact the config, which do not resolve path "absolutly", which duplicates that config.

Riduidel
  • 22,052
  • 14
  • 85
  • 185
  • I'm not sure I understand "all generated classes being duplicated". The plugin removes the generated classes by default before running (see [clearOutputDir](http://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.2/xjc-mojo.html#clearOutputDir)). Can you post the error message you have, and your POM configuration of the plugin? – Tunaki Sep 26 '16 at 09:59

1 Answers1

3

As it appear, there is a difference in the way jaxb2-maven-plugin behaves between 2.2 and 2.3 (which was released during september '16). Indeed, in 2.3, we have in AbstractJaxbMojo the following code

    // 4) If the output directories exist, add them to the MavenProject's source directories
    if(getOutputDirectory().exists() && getOutputDirectory().isDirectory()) {

        final String canonicalPathToOutputDirectory = FileSystemUtilities.getCanonicalPath(getOutputDirectory());

        if(log.isDebugEnabled()) {
            log.debug("Adding existing JAXB outputDirectory [" + canonicalPathToOutputDirectory
                    + "] to Maven's sources.");
        }

        // Add the output Directory.
        getProject().addCompileSourceRoot(canonicalPathToOutputDirectory);
    }

Unfortunatly, there is no equivalent code in XjcMojo#addGeneratedSourcesToProjectRoot(), which is

    getProject().addCompileSourceRoot(getOutputDirectory().getAbsolutePath());

As a consequence, the absolute path is added once, and the canonical path is added afterwards, hence the bug.

Considering that, the best solution was to set the maven version property :

<version>2.2</version>

Obviously, there is a bug report for that.

Riduidel
  • 22,052
  • 14
  • 85
  • 185