如何解决父POM依赖问题:无法读取构件描述符;找不到构件?

8
我最近在 Maven Central 上发表了三个文件:https://search.maven.org/search?q=ced2ar3-rdb
这三个文件都属于同一项目,同时发布。
我现在正在尝试使用 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>,即使文件中<properties>中正确定义了${ced2ar.version}有关?

2个回答

17

问题与父级pom中有${ced2ar.version}有关吗?即使在文件的较低位置正确定义了${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>

子项目中定义的${ced2ar.version}属性必须通过先构建定义此属性的父POM的反应堆项目来解析。这就是为什么在开发中(使用反应堆)可以正常构建,但没有反应堆时不能构建的原因。

要解决此问题,您可以使用revision标准属性和flatten-maven-plugin,帮助您在父级和子级之间设置唯一版本。

您的反应堆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 后,我没有看到这些字段

这是预期的,因为扁平化插件会剥离原始 POM 中的一些元数据

扁平化 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下的父级项目中使用此功能...但是,如果根项目有一个父级项目,则必须提供一个文字版本号...(详见https://issues.apache.org/jira/browse/MNG-6438) - khmarbaise
让我们在聊天中继续讨论 - davidxxx
@davidxxx 感谢您的建议;我正在尝试它,但是在部署到maven中央时卡住了,出现了一个无效的POM错误:“缺少项目名称、缺少项目描述、缺少项目URL、缺少SCM URL、缺少开发人员信息”。事实上,在检查生成的.flattened-pom.xml后,我没有看到这些字段,如果这相关的话。当前状态:https://github.com/ncrncornell/ced2ar-rdb/tree/e6ce9c951904d6b1f1aad637a9bce7abe2448778 - bbarker
@bbarker,你的配置看起来很好。但是flatten插件会剥离一些元数据。因此,你应该指定要保留它们。我更新了我的答案。 - davidxxx
@davidxxx 感谢您指出这一点;现在已经上传并将尝试在今天稍后使用它。我发现 <flattenMode>ossrh</flattenMode> 也可以作为列出单个 pomElements 的替代方法。但我发现一个奇怪的问题,即我仍然需要手动添加 <name>CED2AR 3</name> 到每个子 POM 中。 - bbarker
显示剩余6条评论

0
这对于直接在父模块下的子模块非常有效,但是当其中一个子模块依赖于另一个子模块时,我无法让mvn install对超过1级深度的子模块起作用。例如,请考虑以下层次结构:
└── root (pom type)
    └── child (pom type) 
        ├── child-1 (jar type)
        └── child-2 (jar type; depends on child-1)

使用Maven 文档中描述的配置,在除child-2之外的所有目录下,我可以成功运行mvn install。在child-2中,我会遇到以下错误:

[ERROR] Failed to execute goal on project child-2: Could not resolve dependencies for project com.mygroup:child-2:maven-archetype:local: Failed to collect dependencies at com.mygroup:child-1:jar:local: Failed to read artifact descriptor for com.mygroup:child-1:jar:local: com.mygroup:root:pom:${revision} was not found in https://maven-central.storage-download.googleapis.com/maven2/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接