Maven complaining about parent relative path

2020-02-26 04:42发布

问题:

Consider a maven project with modules consists of some utilities (jar) and some poms for others to reference to if they want to use some of these utilities.

e.g. inside the parent pom.xml

<artifactId>project-parent</artifactId>
<modules>
  <module>client-ref-pom</module> (a module with just one pom.xml)
  <module>server-ref-pom</module> (a module with just one pom.xml)
  <module>client-utils</module> (a module with some utility classes, needs to ref. client-ref-pom)
  <module>server-utils</module> (a module with some utility classes, needs to ref. server-ref-pom)
  <module>utils</module> (a module with some utility classes, needs to ref. project-parent)
</modules>

So if there is another project wishes to use the utils, it will reference to ref-pom as its parent pom, so that the properties can be inherited. This purpose is served.

The current issue is when the module utils also needs to reference to ref-pom as its parent pom (and ref-pom will ref. project-parent as its parent pom), maven is complaining about 'parent.relativePath' pointing to project-parent instead of ref-pom, suggesting to verify again the project structure.

As that is just a warning, I can still compile the project, but I wonder the proper way to setup the project structure so that maven is happy and my purpose is served.

回答1:

In order to resolve the parent project, these possible sources are checked:

  • relativePath
  • local repository
  • remote repositories

The relative path, if not given explicitly, defaults to .., i.e. the pom in the parent directory of the current project. So Maven checks whether a) there is a pom file in that directory and b) that pom file contains the same coordinates as stated in the parent definition of the current project.

If a) and b) are true, that pom file is used as the parent for the resolving of the effective pom.

If a) is true, and b) is false, a warning is given, because this usually points to a misconfigured project (as in your case) and the pom is ignored.

If a) is false, the other sources are checked.

So, in your case, I assume you have the following in your utils/pom.xml

<parent>
  <groupId>...</groupId>
  <artifactId>ref-pom</artifactId>
  <version>..</version>
</parent>

which implicitly includes <relativePath>..</relativePath>. So Maven checks the parent directory of utils, finds a POM, but this point is named project-parent instead of the expected ref-pom. Thus the warning.

The following would work:

<parent>
  <groupId>...</groupId>
  <artifactId>ref-pom</artifactId>
  <version>..</version>
  <relativePath>../ref-pom</relativePath>
</parent>

(Note that in your text, you write about ref-pom, but in the modules above there is only client-ref-pom and server-ref-pom)

however

You should think about whether this is really what you want, in your case, if the separate *-ref-pom modules are really necessary or if the content of those poms could and should be better placed inside of the respective *-util modules.