Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show the Maven POM hierarchy?

Tags:

maven-2

I'm doing some scripting and I need to get a list of all the parent poms for any given pom. The dependency plugin seems to be only interested in the dependencies that are listed in the dependency section of the pom, but there doesn't seem to be a way to show the parent poms, which are also required dependencies for Maven to work.

Am I missing something basic?

like image 948
Todd Avatar asked Feb 02 '12 21:02

Todd


People also ask

What is POM hierarchy?

POM hierarchyA child POM file inherits all the configuration elements from its parent POM file. This is how Maven sticks to its design philosophy, which is convention over configuration. The pom file used by project is obtained by merging the project pom file, parent pom file (if any) and super pom file.

How do you determine dependency hierarchy?

Maven Dependency Tree in Eclipse IDExml “Dependency Hierarchy” tab shows the dependency tree of the project. It has two sides - the left side shows verbose output and the right side shows the resolved dependencies. We can use the “Filter” option to look for a specific dependency.

Which Maven command is to list all dependencies of a project hierarchically?

In Maven, you can use mvn dependency:tree to display the project dependencies in tree format.

Where can I find parent POM files?

Parent POM Relative PathBy 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.


2 Answers

There is no simple Maven command that will show you the chain of parent POMs for a pom.xml. The reason for this is that it is not a common question one would typically ask (more on that below). For your script, you'll just have to parse the pom.xml file, get the parent artifact coordinates, get a hold of the artifact's pom.xml file and then parse it's pom.xml file (and repeat). Sorry, but there is no short cut I know of, but other folks have solved similar problems.

You are right that technically the parent pom is a dependency of your project, but it is not a literal Maven Dependency and is handled completely differently. The chain of parent poms, along with active profiles, your settings.xml file, and the Maven super pom from the installation directory are all combined together to create your project's effective pom. The effective POM is what Maven really uses to do its work. So basically, the parent pom inheritance chain is already resolved and combined before the dependency plugin (or any other plugin) is even activated.

The questions most people typically ask is 'what does my REAL pom.xml really look like when Maven is done combining everything?' or 'What is the result my inheritance chain of parent poms?' or 'How are my pom.xml properties affected by an active profile?' The effective pom will tell you all of this.

I know you didn't ask, but for others reading this, if you want to see your parent pom.xml, simply open up the pom.xml in the M2Eclipse POM editor and click on the parent artifact link on the overview tab. In this way you can quickly move up the chain of pom.xml files with just a single click per pom. It would be a strange project that had more than 3 or 4 parent poms of inheritance.

If you want to see your effective pom, you can run the command mvn help:effective-pom. Alternatively, you can click on the Effective POM tab in M2Eclipse's POM editor.

like image 102
HDave Avatar answered Oct 06 '22 04:10

HDave


Basic solution

mvn org.apache.maven.plugins:maven-dependency-plugin:3.1:display-ancestors 

If your project defines version 3.1 or later you can use:

mvn dependency:display-ancestors 

The output looks similar to:

[INFO] Ancestor POMs: org.springframework.boot:spring-boot-starter-parent:1.4.0.RELEASE <- org.springframework.boot:spring-boot-dependencies:1.4.0.RELEASE

Improved solution

The hierarchy-maven-plugin (that I wrote) can display additional information about imported poms like this :

[INFO] Displaying hierarchy. Set level=full to display dependencies in dependencyManagement [INFO]  PARENT org.springframework.boot:spring-boot-samples:1.4.1.BUILD-SNAPSHOT [INFO]    PARENT org.springframework.boot:spring-boot-starter-parent:1.4.1.BUILD-SNAPSHOT [INFO]      PARENT org.springframework.boot:spring-boot-dependencies:1.4.1.BUILD-SNAPSHOT [INFO]        IMPORT org.springframework:spring-framework-bom:4.3.3.BUILD-SNAPSHOT [INFO]        IMPORT org.springframework.data:spring-data-releasetrain:Hopper-BUILD-SNAPSHOT [INFO]          PARENT org.springframework.data.build:spring-data-build:1.8.4.BUILD-SNAPSHOT [INFO]        IMPORT org.springframework.integration:spring-integration-bom:4.3.1.RELEASE [INFO]        IMPORT org.springframework.security:spring-security-bom:4.1.3.RELEASE 

Details are here : https://github.com/ExampleDriven/hierarchy-maven-plugin

like image 27
Peter Szanto Avatar answered Oct 06 '22 04:10

Peter Szanto