【问题标题】:How to resolve parent pom dependency issue: Failed to read artifact descriptor; Could not find artifact?如何解决父 pom 依赖问题:无法读取工件描述符;找不到神器?
【发布时间】:2019-02-09 21:33:54
【问题描述】:

我最近向 Maven Central 发布了三个工件:https://search.maven.org/search?q=ced2ar3-rdb

这三个是same project 的一部分,同时发布。

我现在正在尝试使用 ced2ar-rdb 和 ced2ar-rdb-tests 作为依赖项构建一个新项目,但我没有在我的代码中引用父 pom 文件的位置(ced2ar3-rdb-parent;我没有实际上想使用它并且认为我不需要它)。但是,当我尝试构建使用 ced2ar-rdb 作为依赖项的项目时,出现此错误:

[ERROR] Failed to execute goal on project ced2ar3-services-core: Could not resolve dependencies for project edu.cornell.
ncrn.ced2ar:ced2ar3-services-core:jar:0.0.0: Failed to collect dependencies at edu.cornell.ncrn.ced2ar:ced2ar3-rdb:jar:0
.0.1: Failed to read artifact descriptor for edu.cornell.ncrn.ced2ar:ced2ar3-rdb:jar:0.0.1: Could not find artifact edu.
cornell.ncrn.ced2ar:ced2ar3-rdb-parent:pom:${ced2ar.version} in central (https://repo.maven.apache.org/maven2) -> [Help   

这个问题是否与我在父 pom 中有 <version>${ced2ar.version}</version> 的事实有关,即使 ${ced2ar.version} 正确定义在文件下方的 <properties> 中?

【问题讨论】:

    标签: maven parent-pom


    【解决方案1】:

    这个问题是否与我有 ${ced2ar.version} 在父 pom 中,即使 ${ced2ar.version} 出现在进一步的正确定义 在文件中?

    不,问题来自您声明子模块的方式。
    这是 rdb 模块pom 的摘录。

    <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">
        <parent>
            <artifactId>ced2ar3-rdb-parent</artifactId>
            <groupId>edu.cornell.ncrn.ced2ar</groupId>
            <version>${ced2ar.version}</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>ced2ar3-rdb</artifactId>
    
    </project>
    

    如果不构建首先构建定义此属性的父 pom 的反应器项目,则无法解析子项目的父版本中定义的 ${ced2ar.version} 属性。这就是为什么您的构建在开发中工作(使用反应器)但没有它就无法工作的原因。

    使用flatten-maven-plugin 解决您的问题you could use the revision standard property,这将帮助您在父母和孩子之间设置唯一的版本。

    你的反应堆 pom 可能看起来像:

    <project>
      <modelVersion>4.0.0</modelVersion>     
      <groupId>my-group</groupId>
      <artifactId>my-parent</artifactId>
      <version>${revision}</version>
      ...
      <properties>
        <revision>1.0.0</revision>
      </properties>
      <modules>
        <module>rdb</module>
        <module>rdb-tests</module>
        ..
      </modules>
    
     <build>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>flatten-maven-plugin</artifactId>
          <version>1.0.0</version>
          <configuration>
            <updatePomFile>true</updatePomFile>
          </configuration>
          <executions>
            <execution>
              <id>flatten</id>
              <phase>process-resources</phase>
              <goals>
                <goal>flatten</goal>
              </goals>
            </execution>
            <execution>
              <id>flatten.clean</id>
              <phase>clean</phase>
              <goals>
                <goal>clean</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
      </build>
    </project>
    

    例如像这样的rdb pom.xml:

    <project>
      <parent>
        <groupId>my-group</groupId>
        <artifactId>my-parent</artifactId>
        <version>${revision}</version>
      </parent>
    
      <artifactId>rdb</artifactId>
       ...
    </project>
    

    关于你的评论:

    我收到一个无效的 POM 错误:“缺少项目名称,项目 缺少描述、缺少项目 URL、缺少 SCM URL、开发人员 信息丢失”。确实,在检查了生成的 .flattened-pom.xml,我没有看到这些字段

    预计为flattened plugin strips some metadata of the original POM

    扁平化 POM 是原始 POM 的简化版本,带有 专注于仅包含使用它的重要信息。 因此,仅在维护时需要的信息 开发人员和构建项目工件被剥离。开始 从这里我们指定如何从 原POM及其项目

    但您可以通过在插件的pomElements 参数中添加您不想去除的元素来覆盖此默认设置。
    例如:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>flatten-maven-plugin</artifactId>
        <version>1.0.0</version>
        <configuration>
            <updatePomFile>true</updatePomFile>
            <pomElements>
                <name/>
                <description/>
                <developers/>
                <contributors/>
                <url/>
                <scm/>
            </pomElements>                  
        </configuration>
        <executions>
            <execution>
                <id>flatten</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>flatten</goal>
                </goals>
            </execution>
            <execution>
                <id>flatten.clean</id>
                <phase>clean</phase>
                <goals>
                    <goal>clean</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    【讨论】:

    • 对于独立的父级,您不能使用它,它不会工作也不打算用于此。只有在多模块构建中,您才能在根 pom 下的父级中使用它...但是如果根有父级,则必须给出文字版本号....(详细信息请参阅issues.apache.org/jira/browse/MNG-6438
    • @khmarbaise 你确定吗?实际上,父项目不是一个独立的项目,而是一个多模块项目。我已经在类似的设置中使用了这个,如果我的记忆正确的话,这很有效..
    • @khmarbaise 首先:感谢您实现了它:) 我刚刚用 OP 的 git 项目测试了它,并通过在分发管理中指定一个本地路径来工作:依赖项在已部署工件的 pom。在实际项目中,父/多模块项目的 pom.xml 没有任何父项。我认为你引用的案例不同。
    • @khmarbaise 只是个人。我非常喜欢 Maven,想知道我是否可以做出贡献。据您所知,社区是否需要一些支持,或者问题已经得到足够快的处理?
    • Apache Maven 是一个开源项目,我们总是感谢以任何方式提供贡献/帮助。请转到maven.apache.org/guides/development/guide-helping.html 并阅读...您也可以在开发人员列表中询问如何/在哪里可以提供帮助...我认为有很多机会...可以提供帮助...如果您愿意提供帮助会很棒...如果您愿意,也可以亲自给我写一封电子邮件....
    猜你喜欢
    • 2021-01-13
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2011-10-02
    • 2017-02-12
    相关资源
    最近更新 更多