Maven依赖插件无法解析插件的手动指定依赖项

时间:2015-08-11 10:00:22

标签: java maven

我遇到了maven-dependency-plugin(Maven版本3.2.3maven-dependency-plugin版本2.10)的问题。我试图介绍插件依赖项。我创建了一个简单的项目:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sze</groupId>
    <artifactId>mvn-project</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.2</version>
            </plugin>
        </plugins>
    </build>
</project>

我选择了maven-antrun-plugin 1.2版本,因为Maven Official Guide to Configuring Plug-ins中提到了这个插件及其依赖项。然后我运行一个命令来解决它的依赖:

mvn dependency:resolve-plugins

输出:

[INFO] Plugin Resolved: maven-antrun-plugin-1.2.jar
[INFO]     Plugin Dependency Resolved: maven-plugin-api-2.0.1.jar
[INFO]     Plugin Dependency Resolved: maven-project-2.0.1.jar
[INFO]     Plugin Dependency Resolved: ant-launcher-1.6.5.jar
[INFO]     Plugin Dependency Resolved: ant-1.6.5.jar

确实,我有antant-launcher版本1.6.5。现在我想更改这些依赖项的版本并更改我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sze</groupId>
    <artifactId>mvn-project</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.2</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant</artifactId>
                        <version>1.7.1</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant-launcher</artifactId>
                        <version>1.7.1</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

但是,当我再次运行mvn dependency:resolve-plugins时,属于maven-antrun-plugin的依赖项版本仍为1.6.5

[INFO] Plugin Resolved: maven-antrun-plugin-1.2.jar
[INFO]     Plugin Dependency Resolved: maven-plugin-api-2.0.1.jar
[INFO]     Plugin Dependency Resolved: maven-project-2.0.1.jar
[INFO]     Plugin Dependency Resolved: ant-launcher-1.6.5.jar
[INFO]     Plugin Dependency Resolved: ant-1.6.5.jar

我如何检查这些依赖项的版本是否已更改?

1 个答案:

答案 0 :(得分:4)

出于某种原因,resolve-plugins目标解决自定义插件依赖项。如果您想查看实际使用的插件,则必须在调试模式下运行Maven:

mvn -X antrun:run

这将显示添加到类路径的依赖项:

[DEBUG] org.apache.maven.plugins:maven-antrun-plugin:jar:1.2:
[DEBUG]    org.apache.ant:ant:jar:1.7.1:runtime
[DEBUG]    org.apache.ant:ant-launcher:jar:1.7.1:runtime
[DEBUG]    org.apache.maven:maven-plugin-api:jar:2.0.1:compile
[DEBUG]    org.apache.maven:maven-project:jar:2.0.1:compile
[DEBUG]       org.apache.maven:maven-profile:jar:2.0.1:compile
[DEBUG]       org.apache.maven:maven-model:jar:2.0.1:compile
[DEBUG]       org.apache.maven:maven-artifact-manager:jar:2.0.1:compile
[DEBUG]          org.apache.maven:maven-repository-metadata:jar:2.0.1:compile
[DEBUG]       org.codehaus.plexus:plexus-utils:jar:1.0.5:compile
[DEBUG]          classworlds:classworlds:jar:1.1-alpha-2:compile
[DEBUG]       org.apache.maven:maven-artifact:jar:2.0.1:compile
[DEBUG]       org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9:compile
[DEBUG]          junit:junit:jar:3.8.1:compile
[DEBUG]    ant:ant-launcher:jar:1.6.5:runtime
[DEBUG]    ant:ant:jar:1.6.5:compile

请注意,原始依赖项的组ID是不是 org.apache.ant,而只是ant。这将导致Maven同时包含ant:ant:1.6.5org.apache.ant:ant:1.7.1。但是,自定义依赖项将首先出现在类路径中,因此您将能够使用较新的ant版本。