Maven 不会在复制依赖期间排除

ver*_*uso 5 java maven

我有一个使用 Netty 4.0.29 的项目,我还有另一个依赖 netty 3.9.0。我加入了一个排除项,但当我运行复制依赖项时,它仍然在 3.9.0 中徘徊。

    <dependency>
        <groupId>com.ning</groupId>
        <artifactId>async-http-client</artifactId>
        <version>1.9.31</version>
        <exclusions>
            <exclusion>
                <groupId>io.netty</groupId>
                <artifactId>netty</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

如果我运行 mvn dependency:tree 并设置此排除项,我会看到它确实被排除在外:

[INFO] +- com.ning:async-http-client:jar:1.9.31:compile
Run Code Online (Sandbox Code Playgroud)

但是当我运行 mvn clean dependency:copy-dependencies 时,我看到 jar 3.9.0 与 4.0.29 一起被复制。根据文档和谷歌,当有排除时,不应复制。

[INFO] Copying netty-3.9.0.Final.jar to /Users/udonom1/wk/141/coursecopy-api/target/dependency/netty-3.9.0.Final.jar
[INFO] Copying netty-all-4.0.29.Final.jar to /Users/udonom1/wk/141/coursecopy-api/target/dependency/netty-all-4.0.29.Final.jar
Run Code Online (Sandbox Code Playgroud)

我尝试按照下面第一个答案的建议进行排除,但没有奏效。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>                               <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                        <excludeArtifactIds>io.netty:netty:3.9.0.Final</excludeArtifactIds>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

我还按照进一步的建议添加了一个依赖项:

    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.0.29.Final</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

小智 2

如果您编写的不是库,您可以通过简单的方法来控制项目中任何依赖项的版本 -dependencyManagement根 pom 文件中的块,例如:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty</artifactId>
            <version>4.0.29.Final</version>
        </dependency>
    </dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)

此块的额外好处 - 您可以省略具体依赖项中依赖项的版本和范围(具有相同的组 ID、工件 ID 和打包)。

PS 再看看你的依赖关系让我问你:你确定这个依赖关系有单个 Maven 工件 id 吗?netty-all-4.0.29.Final.jar- 似乎这个神器应该有netty-all神器 ID...如果他们有不同的神器 ID,我的食谱将无济于事。在这种情况下,您应该定义构建配置maven-dependency-plugin,例如:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <configuration>
                <excludeArtifactIds>io.netty:netty:3.9.0.Final</excludeArtifactIds>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

-DexcludeArtifactIds或者只是在你的 Maven 调用中使用参数