首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >mvn依赖项:树在简单的项目上失败

mvn依赖项:树在简单的项目上失败
EN

Stack Overflow用户
提问于 2015-03-03 15:20:15
回答 5查看 13.3K关注 0票数 12

我有一个相当复杂的项目(大约100个模块),我想在这个项目上运行mvn dependency:tree。它失败了,抱怨它无法解决依赖关系。否则该项目会编译得很好。因此,我创建了一个最基本的项目,但它仍然失败,同样的错误。显然,要么我做了一些非常基本的错误,否则maven依赖插件还没有被任何人使用。以下是三个POMs im by测试项目:

pom.xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>root</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>foo</module>
        <module>bar</module>
    </modules>
</project>

foo/pu.xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
</project>

bar/pu.xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>bar</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>foo</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

然后,在顶层目录中发出以下命令mvn dependency:tree,并获得以下输出:

代码语言:javascript
运行
复制
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] foo
[INFO] bar
[INFO] root
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building foo 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ foo ---
[INFO] com.example:foo:jar:1.0.0-SNAPSHOT
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building bar 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] foo ................................................ SUCCESS [  0.756 s]
[INFO] bar ................................................ FAILURE [  0.011 s]
[INFO] root ............................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.065 s
[INFO] Finished at: 2015-03-03T16:19:18+01:00
[INFO] Final Memory: 13M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project bar: Could not resolve dependencies for project com.example:bar:jar:1.0.0-SNAPSHOT: Could not find artifact com.example:foo:jar:1.0.0-SNAPSHOT -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :bar

我遗漏了什么?这样不就行了吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-09-19 05:07:31

我很沮丧为什么它不起作用。

一个简单的依赖分析就是不起作用。

而这位官员也没有任何指南。

另一个很有用的命令也没起作用

代码语言:javascript
运行
复制
mvn dependency:resolve

但是,也许您可以尝试使用以下命令

代码语言:javascript
运行
复制
mvn test-compile dependency:resolve
mvn test-compile dependency:tree

不管怎么说,对我来说很管用

2017年3月13日最新消息

通过跳过编译,我们可以使它更快。

代码语言:javascript
运行
复制
 mvn test-compile dependency:resolve -Dmaven.main.skip=true -Dmaven.test.skip=true
 mvn test-compile dependency:tree    -Dmaven.main.skip=true -Dmaven.test.skip=true

很遗憾我们的项目没有成功,因为我们的项目使用了kotlin,也许是kotlin的bug没有跳过编译,也许我应该把这个bug报告给jetbrains。

票数 22
EN

Stack Overflow用户

发布于 2015-03-04 09:19:33

好的,让我们给出一个正确的答案,因为评论太短了,无法给出正确的解释。

Maven是一个具有各种组合面的工具,有时很难确定哪些部分对给定的命令起作用。

在您的示例中,您有两个经典项目,foobar,以及一个特殊的项目root

root在它所扮演的意义上是特殊的,在您的示例中有两个角色。

  • 第一个被称为父级pom。它通常用于修复依赖项和插件版本,以及一些需要级联到子项目的常见配置。它还具有一个有时有用的属性,即子项目继承版本,除非在子项目themselfs中显式指定。
  • 第二个角色被称为反应器pom。这是主要在<modules>标记中定义的部分。它定义了一组可以将命令作为组发送到的其他项目(例如:mvn clean install)。在执行此类命令时,maven将查看<modules>中描述的项目,并根据它们声明的依赖项,确定它们必须使用给定命令调用的顺序,以便最大限度地提高构建成功的可能性。

现在,关于您尝试过的各种命令的行为(假设它们都是在root项目中调用的):

  • mvn dependency:tree将对<modules>标记中列出的所有项目执行依赖分析。此分析是针对存储库执行的,这意味着您的本地.m2回购以及其他需要的外部存储库。如果您没有首先在您的存储库中安装您的项目,它将在bar上失败,因为它无法在其中找到com.example:foo:1.0.0-SNAPSHOT
  • 使用相同的顺序,mvn [clean] install将执行将项目打包和部署到本地存储库的完整过程。由于bar将在foo被放入您的存储库后执行,所以一切都会很好,每个人都会很高兴。

但是,在那个时候工作的mvn compile是什么呢?

你的案子有点棘手。由于您的示例没有要编译的实际代码,因此将跳过编译的依赖解决方案,因此在foo工件不可用时不会发生错误。

现在,关于你在回购中的分支和手工艺品的评论.

当您切换到分支时,除非所有其他项目(模块)都使用稳定的版本号(即没有快照后缀),否则您应该在反应堆pom上执行mvn [clean] install运行,以确保您开始使用一组连贯的模块和依赖项。

您可能会认为,与例如解释语言相比,这是浪费时间,但这是处理项目的MAVEN方法。它确保所有模块在开始工作之前都对齐。

票数 9
EN

Stack Overflow用户

发布于 2022-08-19 12:25:18

对我来说,这个问题出现了,试图强制执行requireUpperBoundDeps规则。我无法将maven-enforcer plugin降级(从3.1.0降至3.0.0-M3)。我不得不在某些依赖项中添加排除项。

代码语言:javascript
运行
复制
<dependency>
  <groupId>net.sf.ehcache</groupId>
  <artifactId>ehcache-core</artifactId>
  <version>${dep.ehcache}</version>
  <exclusions>
    <exclusion>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>sizeof-agent</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.richfaces</groupId>
  <artifactId>richfaces</artifactId>
  <version>${dep.richfaces}</version>
  <exclusions>
    <exclusion>
      <groupId>jacorb</groupId>
      <artifactId>jacorb</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.richfaces.cdk</groupId>
      <artifactId>annotations</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.richfaces</groupId>
  <artifactId>richfaces-a4j</artifactId>
  <version>${dep.richfaces}</version>
  <exclusions>
    <exclusion>
      <groupId>jacorb</groupId>
      <artifactId>jacorb</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.richfaces.cdk</groupId>
      <artifactId>annotations</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.richfaces</groupId>
  <artifactId>richfaces-core</artifactId>
  <version>${dep.richfaces}</version>
  <exclusions>
    <exclusion>
      <groupId>jacorb</groupId>
      <artifactId>jacorb</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.richfaces.cdk</groupId>
      <artifactId>annotations</artifactId>
    </exclusion>
  </exclusions>
</dependency>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28835418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档