Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In maven, how can I refer to a parent pom that does not exist at that location on the file system?

I didn't realize this was possible to do, but apparently it is because this project I inherited does it. The project's pom looks like this:

<parent>
    <groupId>my.group</groupId>
    <artifactId>artifact</artifactId>
    <relativePath>../parent/pom.xml</relativePath>
    <version>1.0.14</version>
</parent>

That "../parent/pom.xml" does not exist on my file system, yet this project builds without issue. Even Intellij is confused and marks this relativePath in red.

I know this has something to do with the way my ~/.m2/settings.xml is configured, because the project did not build and complained about the missing pom.xml until I used the company's settings.xml. But I'm not sure where in that file it is making this work.

Can someone point me to some documentation that describes how this feature works? We are using the Nexus maven repository.

like image 958
Daniel Kaplan Avatar asked Jan 20 '15 20:01

Daniel Kaplan


People also ask

Where does Maven look for parent POM?

By default, Maven looks for the parent POM first at project's root, then the local repository, and lastly in the remote repository. If parent POM file is not located in any other place, then you can use code tag. This relative path shall be relative to project root.

How do I find parent relativePath?

We can find the details in the Maven Model Documentation and sum up the behavior: If there is a pom. xml file in the parent folder, and if this file has the matching GAV coordinate, it is classified as the project's Parent POM. If not, Maven reverts to the repositories.

What is relativePath /> in POM xml?

relativePath allows you to select a different location, for example when your structure is flat, or deeper without an intermediate parent POM. However, the group ID, artifact ID and version are still required, and must match the file in the location given or it will revert to the repository for the POM.


1 Answers

When Maven goes looking for the parent POM, it first looks in the place specified by <relativePath/> (which is ../pom.xml by default); if it cannot be found there, it goes and looks in your local repository (~/.m2) and then tries to download it from the remote repository.

Without seeing your settings.xml it is impossible for me to be sure but my best guess is you have a remote repository listed there that made it possible for maven to perform that last step - download the parent POM from a remote repository that is defined in settings.xml.

Reference: https://maven.apache.org/pom.html#Inheritance - "Notice the relativePath element. It is not required, but may be used as a signifier to Maven to first search the path given for this project's parent, before searching the local and then remote repositories."

like image 83
dcsohl Avatar answered Sep 17 '22 15:09

dcsohl