mojohaus / jaxb2-maven-plugin

JAXB2 Maven Plugin
https://www.mojohaus.org/jaxb2-maven-plugin/
Apache License 2.0
106 stars 77 forks source link

On Linux, generated folder may be added twice using different absolute path. #55

Closed Riduidel closed 6 years ago

Riduidel commented 7 years ago

This issue come from the linked StackOverflow question.

How to reproduce it ?

On a Linux machine, create a jaxb test project under, as a example, /home/User/project1. Link that folder into project2. You should then have the following situation

project1
project2 => project1

Now, in project2, run xjc transformation using jaxb2-maven-plugin. It will fail with duplicated generated classes.

How does it happens ?

In AjbstractJaxbPlugin, there is this code fragment which resolves the path to its canonical representation

    // 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);
    }

However, in XjcMojo, source directory is added using the following code

protected void addGeneratedSourcesToProjectSourceRoot() {
    getProject().addCompileSourceRoot(getOutputDirectory().getAbsolutePath());
}

Which do not resolve the path to its canonical version.

As a consequence, directory is added twice to maven compiler roots, and code appear as duplicated in compiler.

How to solve it ?

Replace code in XjcMojo replace the method with the following code

protected void addGeneratedSourcesToProjectSourceRoot() {
    getProject().addCompileSourceRoot(FileSystemUtilities.getCanonicalPath(getOutputDirectory()));
}

Why don't you fix it by yourself ?

I'm behind a corporate firewall, but will try to edit code directly on Github interface

JWT007 commented 7 years ago

Hello, also affected by this problem in jaxb2-maven-plugin versions 2.3 / 2.3.1.

Locally on windows development machine there are no problems. However, on our Jenkins Linux build-servers the plugin has problems with symbolic links in the working directory.

As such 2 source-files are generated and both paths are added automatically by the JAXB plugin to the build-path. /export/build/jenkins-localfs/workspace/.../MyJAXBClass.java /export1/build/jenkins-localfs/workspace/.../MyJAXBClass.java

This results in duplicate classes during the build.

The above behaviour is new in 2.3+. With 2.2 the Jenkins build runs without problems.

poireauxvinaigrette commented 7 years ago

Same pb, do we have to wait for 2.4 ?

veithen commented 6 years ago

I think the underlying problem is that the plugin adds the source folder twice to the project. This would actually be fixed as a side effect of #94.