意外的Maven依赖关系中介行为

时间:2016-04-20 21:46:37

标签: java maven

我遇到了一个非常奇怪的maven依赖调解行为。我正在使用

$ mvn -version
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128M; support was removed in 8.0
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T04:57:37-07:00)
Maven home: /usr/local/Cellar/maven/3.3.3/libexec
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre 

pom的依赖部分是这样的:

<dependencies>
    <dependency>
        <groupId>com.company.app</groupId>
        <artifactId>foo</artifactId>
        <version>3.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.company.app</groupId>
        <artifactId>bar</artifactId>
        <version>3.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>1.3.1</version>
    </dependency>
</dependencies>

当我运行mvn clean compile时,它失败并出现此错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project my-module: Compilation failure: Compilation failure:
[ERROR] /Users/morpheus/my-project/my-module/src/main/java/com/company/app/Main.java:[23,32] cannot find symbol
[ERROR] symbol:   method builder(java.lang.String)
[ERROR] location: class org.apache.commons.cli.Option

此错误正在发生,因为maven正在使用旧版本的commons-cli(早于1.3.1,我已明确要求它使用)。我将依赖项的顺序更改为:

<dependencies>
    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.company.app</groupId>
        <artifactId>foo</artifactId>
        <version>3.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.company.app</groupId>
        <artifactId>bar</artifactId>
        <version>3.0-SNAPSHOT</version>
    </dependency>
</dependencies>

现在错误消失了!这是对maven陈述dependency mediation rules的公然违反。这不是maven中的错误吗?

0 个答案:

没有答案