javamavenparent-childpom.xmlparent-pom

How to setup maven parent and child project as separate projects?


There is a project (type=pom), which is supposed to be used as a parent for another project.

parent

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>my-firm</groupId>
  <artifactId>custom-parent</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>

  <distributionManagement>
    <repository>
      <id>lib-repo-local</id>
      <name>my-releases</name>
      <url>http://artifactory.local:8081/artifactory/libs-release-local</url>
    </repository>
    <snapshotRepository>
      <id>lib-repo-snapshots</id>
      <name>my-snapshots</name>
      <url>http://artifactory.local:8081/artifactory/libs-snapshot-local</url>
    </snapshotRepository>
  </distributionManagement>

</project>

child

<project>
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>my-firm</groupId>
    <artifactId>custom-parent</artifactId>
    <version>1.0.0</version>
  </parent>

  <artifactId>child-sample</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>
    
  <distributionManagement>
    <repository>
      <id>lib-repo-local</id>
      <name>my-releases</name>
      <url>http://artifactory.local:8081/artifactory/libs-release-local</url>
    </repository>
    <snapshotRepository>
      <id>lib-repo-snapshots</id>
      <name>my-snapshots</name>
      <url>http://artifactory.local:8081/artifactory/libs-snapshot-local</url>
    </snapshotRepository>
  </distributionManagement>

</project>

The parent project is deployed to the remote repository - artifact my-firm:custom-parent:1.0.0 is available.

When I run mvn clean on the child project there's an error

[FATAL] Non-resolvable parent POM for my-firm:child-sample:0.0.1: 
        Failure to find my-firm:custom-parent:1.0.0 in https://repo.maven.apache.org/maven2
        was cached in the local repository, resolution will not be reattempted until
        the update interval of central has elapsed or updates are forced
        and 'parent.relativePath' points at wrong local POM @ line 7, column 11

All the three points in the error message seem to be unrelated to my intentions.

  1. Maven does not try to look in the repo described in distributionManagement, but complaints about maven.central
  2. Nothing is cached in local repository - all artifacts removed from there before the build.
  3. The parent.relativePath is intentionally absent to have the child project agnostic to the parent project location and let it rely only on the deployed artifact (parent pom).

Please, show how to edit the poms to have child and parent as separate projects and let child to depend only on the parent artifact.


Solution

  • Please note that <distributionManagement> is only for upload. This is where Maven puts the artifacts if you run mvn deploy.

    So if you want Maven to look into your artifactory for the parent POM, you need to add an appropriate <repository> element or -- which is the preferred way -- configure your artifactory in the settings.xml.

    Nevertheless if you build the parent first on some machine and then build the child on the same machine, the parent POM is read from the local repository. It is not removed from there in any way. I don't know what went wrong in your case, but I guess you either had a different local repository for both builds or the content was somehow erased in between.