从Maven 2.2.1升级到Maven 3.0.4时的DependencyResolutionException

时间:2012-04-10 16:34:50

标签: maven maven-2 maven-3 dependency-resolution

我正在尝试将我的项目从Maven 2.2.1升级到Maven 3.0.4,但遇到了依赖项解析的问题。我花了整整一天试图通过查看Maven文档和类似的帖子来研究这个,但我仍然卡住了。这是我关于stackoverflow的第一个问题,所以希望我在这里遵循礼仪。谢谢你的帮助!

我的项目结构如下:

pom.xml (acme-parent)
child-alpha
+-----> pom.xml

另外,我在自己的存储库中有以下文件:

http://maven.my-own-repo.com/acme/child-dep-jdk15/maven-metadata-local.xml
http://maven.my-own-repo.com/acme/child-dep-jdk15/1.0/child-dep-jdk15-1.0.jar
http://maven.my-own-repo.com/acme/child-dep-jdk15/1.0/child-dep-jdk15-1.0.pom

child-alpha / pom.xml又依赖于我自己的acme:child-dep-jdk15,它已经单独构建到一个jar文件中,并且有自己的.pom文件,指定父元素是相同的acme-parent parent as child-alpha。

以下是相关文件的摘录:

acme-parent pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>acme</groupId>
    <artifactId>acme-parent</artifactId>
    <name>Acme Parent Project</name>
    <version>1</version>
    <packaging>pom</packaging>

    <properties>...</properties>

    <modules>
        <module>child-alpha</module>
    </modules>

    <repositories>
        <repository>
            <id>acme-repo</id>
            <url>http://maven.my-own-repo.com</url>
        </repository>
        <repository>
            <id>central</id>
            <url>http://repo1.maven.org/maven2</url>
        </repository>
    </repositories>

    <pluginRepositories>...</pluginRepositories>

    <build>
      <plugins>...</plugins>
    </build>

    <dependencies>...</dependencies>
</project>

child-alpha pom.xml:

<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>acme</groupId>
        <artifactId>acme-parent</artifactId>
        <version>1</version>
    </parent>
    <artifactId>child-alpha</artifactId>
    <name>Child Alpha Project</name>
    <version>1.0</version>
    <packaging>jar</packaging>

    <scm>...</scm>

    <properties>...</properties>

    <build>
      <resources>...</resources>
      <plugins>...</plugins>
    </build>

    <profiles>
        <profile>
            <id>jdk15</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.5</jdk>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>acme</groupId>
                    <artifactId>child-dep-jdk15</artifactId>
                    <version>1.0</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

    <dependencies>...</dependencies>
</project>

子-DEP-jdk15-1.0.pom:

<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>acme</groupId>
        <artifactId>acme-parent</artifactId>
        <version>1</version>
    </parent>
    <groupId>acme</groupId>
    <artifactId>child-dep-jdk15</artifactId>
    <name>Acme Child Dependency</name>
    <version>1.0</version>
    <packaging>jar</packaging>

    <build>...</build>

    <dependencies>...</dependencies>
</project>

当我尝试构建child-alpha(在child-alpha目录中)时,我运行了以下命令:

mvn -U -e clean install

我收到了以下日志消息和堆栈跟踪(删除了无关的消息):

Downloading: http://maven2.my-own-repo.com/acme/acme-parent/1/acme-parent-1.pom
Downloading: http://repo1.maven.org/maven2/acme/acme-parent/1/acme-parent-1.pom

[...]

[ERROR] Failed to execute goal on project child-alpha: Could not resolve dependencies for project acme:child-alpha:jar:1.0: [...]

Caused by: org.apache.maven.project.DependencyResolutionException: Could not resolve dependencies for project acme:child-alpha:jar:1.0: [...]

Caused by: org.sonatype.aether.collection.DependencyCollectionException: Failed to collect dependencies for [...]
    at org.sonatype.aether.impl.internal.DefaultDependencyCollector.collectDependencies(DefaultDependencyCollector.java:258)
    at org.sonatype.aether.impl.internal.DefaultRepositorySystem.collectDependencies(DefaultRepositorySystem.java:308)
    at org.apache.maven.project.DefaultProjectDependenciesResolver.resolve(DefaultProjectDependenciesResolver.java:150)
    ... 23 more
Caused by: org.sonatype.aether.resolution.ArtifactDescriptorException: Failed to read artifact descriptor for acme:child-dep-jdk15:jar:1.0
    at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:331)
    at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.readArtifactDescriptor(DefaultArtifactDescriptorReader.java:186)
    at org.sonatype.aether.impl.internal.DefaultDependencyCollector.process(DefaultDependencyCollector.java:412)
    at org.sonatype.aether.impl.internal.DefaultDependencyCollector.collectDependencies(DefaultDependencyCollector.java:240)
    ... 25 more
Caused by: org.apache.maven.model.resolution.UnresolvableModelException: Could not find artifact acme:acme-parent:pom:1 in acme-repo (http://maven.my-own-repo.com)
    at org.apache.maven.repository.internal.DefaultModelResolver.resolveModel(DefaultModelResolver.java:126)
    at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:813)
    at org.apache.maven.model.building.DefaultModelBuilder.readParent(DefaultModelBuilder.java:664)
    at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:310)
    at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:232)
    at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:322)
    ... 28 more
Caused by: org.sonatype.aether.resolution.ArtifactResolutionException: Could not find artifact acme:acme-parent:pom:1 in acme-repo (http://maven.my-own-repo.com)
    at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:538)
    at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:216)
    at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:193)
    at org.apache.maven.repository.internal.DefaultModelResolver.resolveModel(DefaultModelResolver.java:122)
    ... 33 more
Caused by: org.sonatype.aether.transfer.ArtifactNotFoundException: Could not find artifact acme:acme-parent:pom:1 in acme-repo (http://maven.my-own-repo.com)
    at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$4.wrap(WagonRepositoryConnector.java:947)
    at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$4.wrap(WagonRepositoryConnector.java:941)
    at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$GetTask.run(WagonRepositoryConnector.java:669)
    at org.sonatype.aether.util.concurrency.RunnableErrorForwarder$1.run(RunnableErrorForwarder.java:60)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)

在使用Maven 2时,我从未在以下位置需要acme-parent pom.xml: http://maven2.my-own-repo.com/acme/acme-parent/1/acme-parent-1.pom

我们在自己的repo上没有父pom.xml的原因是我们在创建版本控制分支时使用了一个脚本,该分支在acme-parent pom.xml中更改某些值,具体取决于我们是哪个分支因此,将pom.xml放在repo中并保持正确更新,以便我们尝试与我们的持续集成服务器同时构建的所有不同分支都会很复杂。

在Maven 2中,当进行child-alpha的构建时,Maven可以毫不费力地找到一个目录的acme-parent pom.xml,即相对路径为../pom.xml,所以它永远不会尝试去到我的存储库找到它。只在我的本地结账中使用父pom.xml而没有在回购中使用它在Maven 2中正常工作。

但是,当我尝试在Maven 3.0.4下构建child-alpha时,发生了上述异常。起初,我以为我需要在child-alpha的pom.xml顶部的标签中显式设置relativePath,但是没有修复它 - 而acme-parent的pom.xml确实已经在默认的relativePath中了../pom.xml,所以不需要显式设置它。

然后,我尝试在child-alpha的pom.xml部分中注释掉child-dep-jdk15依赖项。 child-alpha maven构建成功完成,并将此依赖项注释掉。

我不明白为什么当child-dep-jdk15依赖项到位时,child-alpha不会在Maven 3中构建。如果Maven最初能够正确找到acme-parent pom.xml(按照顶部的块),为什么现在尝试在处理child-dep-jdk15依赖时从repos下载它?我是否错误地配置了某些内容,如果是,我应该如何解决?

另一件事:如果我首先在与acme-parent的pom.xml相同的目录中运行mvn -N install,然后尝试构建child-alpha,那么即使保持依赖项子项,子-α构建也会成功代码中的-dep-jdk15。运行该命令后,我能够验证acme-parent-1.pom是否在我的本地.m2目录中。虽然这种解决方法可能适用于个别开发人员,但对于我们的持续集成服务器来说,这是一个问题,它需要能够为不同的分支运行并发构建。

由于我上面提到的版本控制分支更改,我强烈不希望不必重新配置我的存储库来托管acme-parent pom.xml。

1 个答案:

答案 0 :(得分:0)

在Maven 3中,如果您刚刚下载失败并修复了它(例如将jar上传到存储库),它将缓存失败。要强制刷新,请将-U添加到命令行。

DependencyResolutionException的其他可能原因请参阅Maven Confluence