如何解决父 pom 依赖问题:读取工件描述符失败;找不到神器?

How to resolve parent pom dependency issue: Failed to read artifact descriptor; Could not find artifact?

我最近向 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> 中正确定义?

Is the issue related to the fact that I have ${ced2ar.version} in the parent pom, even though ${ced2ar.version} appears correctly defined in further down in the file?

不,问题出在您声明子模块的方式上。
这是 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的reactor项目是无法解决的。这就是为什么您的构建在开发中(使用反应器)工作但没有它就无法工作的原因。

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

你的 reactor 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>

关于您的评论:

I get an invalid POM error with: "Project name missing, Project description missing, Project URL missing, SCM URL missing, Developer information missing". Indeed, after inspecting the generated .flattened-pom.xml, I do not see these fields

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

The flattened POM is a reduced version of the original POM with the focus to contain only the important information for consuming it. Therefore information that is only required for maintenance by developers and to build the project artifact(s) are stripped. Starting from here we specify how the flattened POM is created from the original POM and its project

但是您可以通过在插件的 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>