How to resolve parent pom dependency issue: Failed

2020-04-17 06:35发布

问题:

I recently published three artifacts to Maven Central: https://search.maven.org/search?q=ced2ar3-rdb

The three are part of the same project and are published concurrently.

I'm now trying to build a new project using ced2ar-rdb and ced2ar-rdb-tests as dependencies, but not where in my code do I reference the parent pom file (ced2ar3-rdb-parent; I don't actually want to use it and didn't think I needed it). However, when I try to build my project that uses ced2ar-rdb as a dependency, I get this error:

[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   

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

回答1:

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?

No, the problem comes from the way which you declared the child modules.
Here is an extract of the rdb module 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>

The ${ced2ar.version} property defined in the parent version of the child project cannot be resolved without building the reactor project that first builds the parent pom that defines this property. That's why your build works in development (with the reactor) but doesn't work without it.

To solve your issue you could use the revision standard property with the flatten-maven-plugin that will help you to set a unique version between the parent and the child.

Your reactor pom could look like :

<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>

And the rdb pom.xml for example like that :

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

  <artifactId>rdb</artifactId>
   ...
</project>

About your comment :

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

it is expected as the 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

But you can override this default by adding the elements that you don't want to strip in the pomElements parameter of the plugin.
For example :

<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>