How do I show the Maven POM hierarchy? - 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?

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.

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

Related

maven release:perform and parent pom

I have a library which I "mavenized" recently and put into a local git repository.
In order to lock some plugin versions I created a simple parent pom which defines the plugin versions via pluginManagement (the parent pom file is not checked into any SCM repository). I specify the parent pom in my libraries pom file:
<parent>
<groupId>org.my.company</groupId>
<artifactId>superpom</artifactId>
<version>1</version>
</parent>
I use default directory structure.
When I try to perform a release using the release plugin I run into a problem.
mvn release:prepare runs fine however when I run mvn release:perform maven checks out the corresponding tag from my local git repository into the target/checkout folder and tries to run the deploy goal.
However the build fails with the error message that it can't find the parent pom file defined in my library pom file.
I assume that's related to the fact that maven tries to find the parent pom file in the target folder and it is not available there.
Is there an easy way how to solve this problem?
Update:
I have multiple unrelated GWT libraries which should share the common company parent pom file in order to specify plugin versions.
The parent pom is just used for defining some default versions and won't contain any module definitions because all GWT libraries are unrelated.
The GWT library are really simple and have no real dependencies to any other libraries apart from the default ones (gwt, junit)
Update2:
I solved the problem by installing the superpom into my local repository by running mvn install in the folder of my superpom.
The first fail you did is not to versionise the parent pom where you defined the pluginManagement area. This is the first step you must do put the pom.xml which you like to use a parent. Secondly you have to put the information about the VCS into the scm area of that pom. After you cleaned up everything you must do a mvn release:prepare release:perform of the parent pom. After that you are able to use it as a parent in your other projects. Furthermore you should define the distributionManagement area in your parent pom.

How can I validate the pluginManagement section of my parent POM in Maven?

I use a project layout like the first one described in the accepted answer to this question. If my parent-pom is managed, built and deployed separate from my project POMs, how can I ensure the pluginManagement section of my parent-pom is valid?
Maven only checks plugins that actually get used in the build as far as I can tell. Since most of the plugins I'm declaring in pluginManagement don't get used when I build the parent-pom, I have no way of knowing if I have an invalid entry until a child project tries to use a plugin it expects to be managed by the parent-pom.
I've tried the versions plugin, but it seems to ignore plugins that don't exist (ex: typos). I've tried declaring the plugins in my parent-pom with inherited=false, but then I have to tie every declared plugin to a phase. Plus, I don't necessarily want to run those plugins against my parent-pom.
I know lots of people use a parent-pom or a super-pom of some type, so there has to be something I'm overlooking.
In your parent pom module create a set of test maven projects, all inheriting parent pom, using some or all of the plugins defined there, and have parent pom run and verify build success of test maven projects. maven-invoker-plugin can help you in accomplishing all this. This plugin is used a lot for testing maven core plugins so you can find more usage examples in sources of maven core plugins.
Another advice is to add plugins to parent pom pluginManagement only when you need them, then you will have opportunity to test it as well. So steps are, start building a project which uses a given plugin not yet present in parent pom, add plugin to parent pom, release parent pom snapshot, make use of that snapshot in new project, if all OK release parent pom, and adjust reference to parent pom in new project. Later for another or same project if you need additional plugin or newer version of plugin already specified in parent pom, adjust parent pom, release new parent pom snapshot, check if it works for the given project, and if it does release parent pom, and adjust parent reference to newly released parent pom.

how to run only parent pom.xml in maven multi-module project

I have maven multi-modules project. At the parent level, i have some java files. And in the parent pom.xml, at the package phase i do some stuff.
Usually, when i run mvn package at parent level, the package phase of parent pom will be run and all the modules will be packaged as well.
I am looking for a way that allow me to do these (when i run mvn package):
allow me to run only paren pom.xml (the script at the package phase), not the modules. This is the 1st priority.
allow me to run paren pom.xml and some particular modules (like module 1, module 2 BUT not module 3 , module 4).
Can i use profile for those issue?
Thanks.
While I agree with the fact that you may not have optimal project structure, the answer is that Maven 2.2.1 has an option "--non-recursive" which satisfies your first requirement:
-N,--non-recursive Do not recurse into sub-projects
So something like this:
mvn --non-recursive clean compile
Why do you want to have java code on the top level? In my opinion this is not a very good idea. Have your code in the subprojects and let the top-level project be responsible for holding the general information and configuration of the entire project.
If you have some base-library code in the top-level project now, you can put it in a sub-project and set up dependencies between the projects.
Take a look at Maven parent pom vs modules pom
The nature of your question indicates that your project structure may not be optimal.

Maven 'versions' plugin not picking up properties from parent

I don't know if I'm missing something, but I have a problem with the Codehaus versions plugin for Maven. Here's a simplification of my situation:
root:
moduleA
moduleB
'root' is an aggregator POM, i.e. declaring moduleA and moduleB as child modules, and A and B both have root as parent.
Both A and B have a dependency on project-C, but the version of this dependency is defined in a property - say <project-C.version> - not hardcoded. This property is defined in root, so both A and B inherit the same version value for project-C.
In root POM:
<properties>
<project-C.version>1.0</project-C.version>
In module A and B (remember root is parent):
<dependency>
<groupId>com.blah</groupId>
<artifactId>project-C</artifactId>
<version>${project-C.version}</version>
</dependency>
Now, say newer version 1.1-SNAPSHOT of project-C becomes available. I want to use the versions plugin to show this to me, so I run:
$ cd /projects/root
$ mvn versions:display-property-updates -DallowSnapshots=true
but all I get is something like this:
[INFO] Building root
[INFO] This project does not have any properties associated with versions
[INFO] Building moduleA
[INFO] This project does not have any properties associated with versions
[INFO] Building moduleB
[INFO] This project does not have any properties associated with versions
It doesn't make a difference if I run it on root, moduleA or moduleB: it always says that. Of course it works if I hardcode project-C's version number in the dependencies.
So, in summary: I want to centralise dependency versions as properties in a root (aggregator/parent) POM, and I want the versions plugin to pick those up for goals that work on properties, i.e.:
display-property-updates
update-properties
How do I do this? Am I missing something? Is this a bug in versions plugin?
It looks like the answer you received in MVERSIONS-123 is not accurate:
If the properties are defined in the parent pom, then run the goal from the parent pom (including the child projects as part of the aggregator if necessary in order to pick up the dependencies)
This part is not true, or at least doesn't occur. It seems the versions plugin only look or properties in the current pom. Declaring the following in the parent does work:
<properties>
<project-C.version>1.0</project-C.version>
</properties>
<dependencies>
<dependency>
<groupId>com.blah</groupId>
<artifactId>project-C</artifactId>
<version>${project-C.version}</version>
</dependency>
</dependencies>
But declaring the property in the parent and the dependency in the child doesn't (even when running the plugin on the parent).
Now, if the behavior described by Stephen in MVERSIONS-123 is the expected behavior, then there is a bug (and you should actually provide a simple project allowing to reproduce, I did a simple test with Junit as dependency).
But in your case (it was maybe just an example though) since both A and B depend on C, why don't you put C in the parent? This would work. You should use dependencyManagement anyway (and this will work if you don't want to inherit it everywhere).
This seemed to be a job for dependencyManagement section in your root pom...

How to include the packaged parent in the child (war) package

I have a parent pom whose packaging type is jar.
Then I have a child pom whose packaging is war. I want to include the packaged parent jar in WEB-INF/lib of the packaged child.
How do I achieve this?
I'm not sure to understand (a parent pom with a jar packaging, is this working?) but the "normal way" would be to add a dependency on the JAR artifact in your WAR project. But I think that you know that and that there is a problem (which is the part that is unclear).
Maybe you could use the dependency plugin instead. Use dependency:copy to get the dependency and copy it to ${project.build.directory}/${finalName}/WEB-INF/lib during prepare-package.