java 在父依赖项管理中定义依赖项版本时未解决传递依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14780274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:21:56  来源:igfitidea点击:

Transitive dependencies not resolved when dependency version defined in parents dependencyManagement

javamavenmaven-3

提问by Arek

I've got a multi-module project with layout as below:

我有一个多模块项目,其布局如下:

pom.xml
projA
  trunk
    pom.xml
    projA1
      pom.xml
    projA2
      pom.xml

In the parent's pom I've defined dependencyManagementand properties:

在父母的 pom 中,我定义了dependencyManagementproperties

<properties>
  <javaee-api.version>6.0</javaee-api.version>
  <log4j.version>1.2.11</log4j.version>
</properties>
...
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
    </dependency>
    <dependency>
      <groupId>javax</groupId>  
      <artifactId>javaee-api</artifactId>
      <version>${javaee-api.version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>

pom.xml for projA2 contains:

projA2 的 pom.xml 包含:

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
</dependency>

pom.xml for projA1 contains:

projA1 的 pom.xml 包含:

<dependency>
  <groupId>${project.groupId}</groupId>
  <artifactId>projA2</artifactId>
  <version>${project.version}</version>
</dependency>

In projA1 I'm using log4j but the dependency is transitive (from projA2).

在 projA1 中,我使用的是 log4j,但依赖项是可传递的(来自 projA2)。

Now when I run mvn dependency:tree -XI get:

现在,当我跑步时,mvn dependency:tree -X我得到:

[WARNING] Invalid POM for mypackage:projA2:jar:1.0, transitive dependencies (if any) will not be available, enable debug logging for more details: Some problems were encountered while processing the POMs:
[ERROR] 'dependencies.dependency.version' for javax:javaee-api:jar is missing. @ line 37, column 15
[ERROR] 'dependencies.dependency.version' for log4j:log4j:jar is missing. @ line 56, column 15

but just below this lines I get:

但就在这条线之下,我得到:

[DEBUG]   testArtifact: artifact=log4j:log4j:jar:1.2.11:compile
[DEBUG]   includeArtifact: artifact=log4j:log4j:jar:1.2.11:compile
[DEBUG]   startProcessChildren: artifact=log4j:log4j:jar:1.2.11:compile
[DEBUG]   endProcessChildren: artifact=log4j:log4j:jar:1.2.11:compile
[DEBUG]   testArtifact: artifact=javax:javaee-api:jar:6.0:provided
[DEBUG]   includeArtifact: artifact=javax:javaee-api:jar:6.0:provided
[DEBUG]   startProcessChildren: artifact=javax:javaee-api:jar:6.0:provided
[DEBUG]   endProcessChildren: artifact=javax:javaee-api:jar:6.0:provided

The only solution is to specify log4j version (<version>${project.version}</version>) in projA2 pom.xml.

唯一的解决方案是<version>${project.version}</version>在 projA2 pom.xml 中指定 log4j 版本 ( )。

Question: what am I doing wrong :-(? I don't want to specify versions that are already defined in parent's dependencyManagementsection.

问题:我做错了什么:-(?我不想指定已经在父dependencyManagement部分定义的版本。

回答by Andrey Lyubimov

I has faced same problem. In my case if forgot update parent POM in local repository. Try to do

我遇到了同样的问题。在我的情况下,如果忘记更新本地存储库中的父 POM。试着做

mvn install

for parent module.

对于父模块。

回答by alexgrimaldi

Had a similar issue. Removing (manually) all the projects involved from the local repository and run "mvn clean install" later worked for me.

有一个类似的问题。从本地存储库中删除(手动)所有涉及的项目,然后运行“mvn clean install”对我有用。

回答by beka

I had a similar problem: I defined a property in my settings.xml and used this for systemPaths in dependencies in my poms and my parent pom. The ignore of the "invalid" package leads to compile errors, because packages used as transitive dependencies are missing in the classpath.

我遇到了类似的问题:我在 settings.xml 中定义了一个属性,并将其用于我的 poms 和父 pom 中的依赖项中的 systemPaths。忽略“无效”包会导致编译错误,因为类路径中缺少用作传递依赖项的包。

I found (transfered to your problem), that if I call maven with "mvn -Dlog4j.version=2.1.11" the warning disappears and also the compile errors. For me this is no satisfieing solution but it solves the problem with invisible transitive dependencies.

我发现(转移到你的问题),如果我用“mvn -Dlog4j.version=2.1.11”调用maven,警告就会消失,编译错误也会消失。对我来说,这不是令人满意的解决方案,但它解决了不可见的传递依赖问题。

回答by szegedi

I had the exact same problem as the OP. It turns out it was a maven bug, as updating to the newest maven version (3.3.3 as of writing) solved the issue.

我遇到了与 OP 完全相同的问题。事实证明这是一个 maven 错误,因为更新到最新的 maven 版本(撰写本文时为 3.3.3)解决了这个问题。

回答by Ebuzer Taha KANAT

It can be repository problem search your missing dependencies find out right repositories and add to you pom like this:

可能是存储库问题搜索您缺少的依赖项找出正确的存储库并添加到您的 pom 中,如下所示:

<repositories>
        <repository>
            <id>jboss-3rd-party-releases</id>
            <url>https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/</url>
        </repository>
    </repositories>

回答by Marcel St?r

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
</dependency>

needs to be in /projA/trunk/pom.xmlinstead of /projA/trunk/projA2/pom.xml.

需要在/projA/trunk/pom.xml而不是/projA/trunk/projA2/pom.xml.

Of course, if you want this be available across all your projects and not just projA it has to be in pom.xml.

当然,如果您希望它在您的所有项目中可用,而不仅仅是 projA,它必须在pom.xml.

回答by user944849

Add:

添加:

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>${log4j.version}</version>
</dependency>

To the dependencyManagementsection of the parent POM (just as you did with JUnit). Any children that require log4j may specify a dependency:

dependencyManagement父 POM的部分(就像您对 JUnit 所做的那样)。任何需要 log4j 的孩子都可以指定一个依赖项:

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
</dependency>

without the version, and it will work, because the version will be pulled from the parent.

没有版本,它会工作,因为版本将从父级中拉出。