I'm a little puzzled by my Jenkins output.
Job on Jenkins: (shortened pom.xml at the bottom)
mvn deploy -Pprofile1
All my plugins will run 4 times:
I need:
Why:
Parent pom.xml
<project ...>
<groupId>com.test.parent</groupId>
<modelVersion>4.0.0</modelVersion>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>parent</name>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
<profiles>
<profile>
<id>profile1</id>
<build>
<plugins>
<plugin>
<groupId>com.test.plugin</groupId>
<artifactId>first-maven-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<execution>
<id>execution1</id>
<phase>initialize</phase>
<goals>
<goal>doit</goal>
</goals>
</execution>
</plugin>
<plugin>
<groupId>com.test.plugin2</groupId>
<artifactId>second-maven-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<execution>
<id>another</id>
<phase>package</phase>
<goals>
<goal>goforit</goal>
</goals>
</execution>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Steps to Configure Maven Profile with Maven Project 1 Project should be a Maven Project. 2 Maven Jar files should be downloaded and path must be set for the same in Environment Variables and ensure that you... More ...
Plugin configurations allow us to reuse a common build logic across projects. If the parent has a plugin, the child will automatically have it without additional configuration. This is like an inheritance. To achieve this, Maven merges the XML files at the element level.
Let’s learn to create maven projects with parent child relationship. Project creation wizard. Select Project Archtype. Fill details and create project. Now change packaging from jar to pom in pom.xml. Additionally, add project properties and dependencies. Create a new maven project just like you did for parent project.
Maven plugins (build and reporting) are configured by specifying a <configuration> element where the child elements of the <configuration> element are mapped to fields, or setters, inside your Mojo. (Remember that a plug-in consists of one or more Mojos where a Mojo maps to a goal.) Say, for example, you have a Mojo that performs a query ...
You can use <inherited>false</inherited>
in the first plugin configuration. So it will only run in parent pom execution.
<build>
<plugins>
<plugin>
<groupId>com.test.plugin</groupId>
<artifactId>first-maven-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<inherited>false</inherited>
<execution>
<id>execution1</id>
<phase>initialize</phase>
<goals>
<goal>doit</goal>
</goals>
</execution>
</plugin>
<plugin>
<groupId>com.test.plugin2</groupId>
<artifactId>second-maven-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<execution>
<id>another</id>
<phase>package</phase>
<goals>
<goal>goforit</goal>
</goals>
</execution>
</plugin>
</plugins>
</build>
See https://stackoverflow.com/a/1671175
First if you define a plugin in the parent which is inherited to all children than it is exactly behaving as you wished which means it is executing on every pom (or in other word on every module doesn't matter if it is a child or not).
The problem of your plugins is that they handle the use cases not very good. Cause you are saying the first one first-maven-plugin
should run only on root level (Apart from that i don't understand what you mean by cleanup operation...removing target folder?)
And the second plugin second-maven-plugin
should run for all pom's? Which is not very accurate cause do you mean by pom all child modules which having packaging pom
? But i assume you mean all children which have packaging jar
?
Apart from the above i'm not sure if the usage of your profile is only based on the lack of handling the use cases correct.
The result of the above i would conclude you need to change the implementation of your plugins.
If you like having a plugin run only on the root level of such a multi module structure this can handle in a very simple way in your plugin like this:
public void execute()
throws MojoExecutionException, MojoFailureException
{
if (mavenProject.isExecutionRoot()) {
} else {
}
..
By using the above your plugin can decide if it is running on root level or not.
So your first-maven-plugin
can use the following:
public void execute()
throws MojoExecutionException, MojoFailureException
{
if (!mavenProject.isExecutionRoot()) {
getLog().info("Not running at root level");
return;
}
// here the time consuming operations
..
And your second-maven-plugin
to do the oposite:
public void execute()
throws MojoExecutionException, MojoFailureException
{
if (mavenProject.isExecutionRoot()) {
getLog().info("Not running at root level");
return;
}
// here the operation on the childs.
..
The behaviour can be improved via the following:
public void execute()
throws MojoExecutionException, MojoFailureException
{
if (!mavenProject.isExecutionRoot()) {
getLog().debug("Not running at root level");
return;
}
if ("pom".equals(project.getPackaging())) {
getLog().debug("Ignoring pom packaging.");
return;
}
// ..now the operations you would like to do...
So if you have several levels of module hierarchy you can ignore the pom
packaging parts or other parts etc.
And last but not least. What do your plugins archive?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With