简体   繁体   English

如何检测两个Linux路径何时引用Maven中的同一文件?

[英]How to detect when two linux paths refer to the same file in maven?

I have a maven project in which jaxb2-maven-plugin (mojohaus one) is used to generate sources. 我有一个maven项目,其中jaxb2-maven-plugin(mojohaus一个)用于生成源。

When invoked on Jenkins, it fails due to all generated classes being duplicated. 在Jenkins上调用时,由于所有生成的类均被复制而失败。

After some investigations, I could track it down to this configuration in maven-compiler-plugin 经过一些调查,我可以在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 . 看起来, /var/lib/jenkins文件夹链接到/appli/projects/jenkins And, in jaxb2-maven-plugin, the configuration we set is 并且,在jaxb2-maven-plugin中,我们设置的配置为

+=================== [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/ . 我们可以看到这里已经存在差异: -d参数引用/var/lib/jenkins ,而-episode参数引用/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 并且在此插件执行的最后,生成的源文件夹将被添加到Jenkins路径中,如此处所述

[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 编辑1 jaxb2插件的配置是

        <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. 请注意,jaxb生成文件夹仅包含一次类。 It i's fact the config, which do not resolve path "absolutly", which duplicates that config. 我确实是配置,它不能“绝对”解析路径,它会复制该配置。

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). 看起来,jaxb2-maven-plugin的行为方式在2.2和2.3之间有所不同(在16年9月发布)。 Indeed, in 2.3, we have in AbstractJaxbMojo the following code 确实,在2.3中,我们在AbstractJaxbMojo具有以下代码

    // 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 不幸的是, XjcMojo#addGeneratedSourcesToProjectRoot()没有等效的代码,即

    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 : 考虑到这一点,最好的解决方案是设置maven version属性:

<version>2.2</version>

Obviously, there is a bug report for that . 显然,有一个关于此的错误报告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM