pluginManagement does not work - maven

This is the pluginManagement in the parent POM, and it can be executed by child project that no plugins defined in child project, why??
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
But if I remove this plugin from parant POM then the child project can't be executed.

In your parent POM, you need to have something like:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Then in your project that extends this POM, you just need to do:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
The idea here is that you don't need to have the same code copied all over the place. You define the settings in one central place and then just extend them.
In your original code, you didn't have the <version/> defined. Usually, in parent POM-s you'd like to control the versions of <dependencies/> and <plugins/>. For dependencies you can also define things like <exclusions/> and <scope/>.
For plugins, you could have the same configuration defined centrally and then, if a case arises where you'd like it to be slightly different, you can just apply the necessary changes in the extending POM.
If in your parent POM you also have a definition of <plugins/> outside the <pluginManagement/> section, then those plugins will be invoked for any project extending this parent.

Related

understanding Spring Boot Maven Plugin in Multi-Module projects

I'm learning about Spring Boot multi module projects and I try to understand what exactly does spring boot maven plugin.
For example I have 2 different projects. In the first project I have the spring boot maven plugin in the web module:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.4.RELEASE</version>
<configuration>
<mainClass>dgs.web.DemoApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And in the second project I have this plugin in the data module:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And this is in the parent pom of the 2nd project:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Can somebody explain me what exaclty does this spring boot maven plugin especially in the second project? And why there is no reference about mainClass. I see this mainClass tag only in the first project.
The maven plugin is for packaging and executing via Maven.
You should read the docs:
https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html
https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/maven-plugin/

Struggling with Maven parent/child plugin configuration inheritance

I'm trying to write a parent pom, and I have a plugin defined, but I need to change the config for all inherited instances. So, I can put some configuration in the <pluginManagement> definition, and I can override it in the <plugin>, but how do I get the children to default back to the <pluginManagement> version?
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<executions...>
<configuration>
<configLocation>
(used by all children)
</configLocation>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>
(unique to the parent)
</configLocation>
</configuration>
</plugin>
</plugins>
<build>
So, what happens is the children continue to show the parent's config.
Ok, I think I have it. The answer, in my case, relates to what you specified - I did need the tag. However the solution was in the tag; by binding it to a non-phase, it does execute. This I knew. What I discovered is that the had to match in order for it to override. Thus, the config never gets parsed and doesn't matter.
<build>
<pluginManagement>
<plugins>
<plugin>
<!-- Main declaration of the plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<!--This must be named-->
<id>checkstyle</id>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration...>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<!-- Uses the default config -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<!--This matches and thus overrides-->
<id>checkstyle</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
You can explicitly specify in your parent pom that the plugin should not be inherited:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.9.1</version>
<executions...>
<configuration>
<configLocation>
(used by all children)
</configLocation>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<inherited>false</inherited> <!-- Add this line -->
<configuration>
<configLocation>
(unique to the parent)
</configLocation>
</configuration>
</plugin>
</plugins>
<build>
And in your child pom, you need to specify the plugin (the config will then come from the <pluginManagement> parent element.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
<build>

Disable a Maven plugin defined in a parent POM

I am using a parent POM that defines a plugin that I do not want to be run in a child POM. How can I disable the plugin in the child pom completely?
Constraint: I cannot change the parent POM itself.
The following works for me when disabling Findbugs in a child POM:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<executions>
<execution>
<id>ID_AS_IN_PARENT</id> <!-- id is necessary sometimes -->
<phase>none</phase>
</execution>
</executions>
</plugin>
Note: the full definition of the Findbugs plugin is in our parent/super POM, so it'll inherit the version and so-on.
In Maven 3, you'll need to use:
<configuration>
<skip>true</skip>
</configuration>
for the plugin.
See if the plugin has a 'skip' configuration parameter. Nearly all do. if it does, just add it to a declaration in the child:
<plugin>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
If not, then use:
<plugin>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<executions>
<execution>
<id>TheNameOfTheRelevantExecution</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
The thread is old, but maybe someone is still interested.
The shortest form I found is further improvement on the example from λlex and bmargulies. The execution tag will look like:
<execution>
<id>TheNameOfTheRelevantExecution</id>
<phase/>
</execution>
2 points I want to highlight:
phase is set to nothing, which looks less hacky than 'none', though still a hack.
id must be the same as execution you want to override. If you don't specify id for execution, Maven will do it implicitly (in a way not expected intuitively by you).
After posting found it is already in stackoverflow:
In a Maven multi-module project, how can I disable a plugin in one child?
I know this thread is really old but the solution from #Ivan Bondarenko helped me in my situation.
I had the following in my pom.xml.
<build>
...
<plugins>
<plugin>
<groupId>com.consol.citrus</groupId>
<artifactId>citrus-remote-maven-plugin</artifactId>
<version>${citrus.version}</version>
<executions>
<execution>
<id>generate-citrus-war</id>
<goals>
<goal>test-war</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
What I wanted, was to disable the execution of generate-citrus-war for a specific profile and this was the solution:
<profile>
<id>it</id>
<build>
<plugins>
<plugin>
<groupId>com.consol.citrus</groupId>
<artifactId>citrus-remote-maven-plugin</artifactId>
<version>${citrus.version}</version>
<executions>
<!-- disable generating the war for this profile -->
<execution>
<id>generate-citrus-war</id>
<phase/>
</execution>
<!-- do something else -->
<execution>
...
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

maven exclude plugin defined in parent pom

I have a parent pom with a few plugins. In my child pom, I want to exclude one plugin.
How can I do this?
I had a similar requirement to run some plugins in the child but not the parent POM. i achieved this by stating <skip>true</skip> in the parent POM.
the parent pom entry is below
<plugin>
<groupId>eviware</groupId>
<artifactId>maven-soapui-plugin</artifactId>
<version>4.0.0</version>
<inherited>false</inherited>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
The child project pom entry is below
<plugins>
<plugin>
<groupId>eviware</groupId>
<artifactId>maven-soapui-plugin</artifactId>
<version>4.0.0</version>
<configuration>
<settingsFile>site-service-web/src/test/soapui/soapui-settings.xml</settingsFile>
<projectFile>site-service-web/src/test/soapui/PodifiSite-soapui-project.xml</projectFile>
<outputFolder>site-service-web/target/surefire-reports</outputFolder>
<junitReport>true</junitReport>
<exportwAll>true</exportwAll>
<printReport>true</printReport>
</configuration>
</plugin>
</plugins>
You can define all plugins in parent pom in pluginManagement section
<pluginManagement>
<plugins>
...
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeTypes>tar.bz2</includeTypes>
<outputDirectory>${project.basedir}/target/dependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</pluginManagement>
And after in parent and child poms you can control the execution phase of this plugin
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
If you paste this in parent pom do not forget option <inherited>false</inherited>, its disable execution inheritance in child pom.
You can declare all the plugins in your parent pom within <pluginManagement>. In each child, you can declare the plugins which are used by that child. This way, you can include or exclude plugins as appropriate.
You can look at this related SO discussion as well.
You could use a profile activated by a property to determine whether the plugin is used or not. By default the property could be activated and the one project you do not want to use it in could include the property value to exclude the plugin.

Automatically activate parent plugin in Maven

Is it possible to have a plugin defined in the parent POM which is deactivated, and when the child inherits this plugin it gets automatically activated?
I guess you want to configure the plugin in your parent pom, but use it only in the inherited projects. Maven has a section for this - configure your plugins in pluginManagement, but bind them to a phase just when you needed it, e.g. omit the phase tag in pluginManagement, but specify it under in you inherited pom.
So 'siddhadev' is exactly correct. You can define the plugin configuration in the parent pom with a given id:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>child-caller</id>
<!-- 'phase' omitted -->
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="called from child!" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
And, in the child POM, you can explicitly list the phase where this should be called:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>child-caller</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
I've used this for targeting various JREs. Unfortunately, because you can't use the maven-compiler-plugin with different destination directories (which I consider a bug in the plugin), you must use Ant.
This isn't exactly what you're after, but I think it will work well enough for you.
If you declare the plugin in a pluginManagement tag in the parent, the configuration will be inherited by any child projects that declare that plugin.
For example, in the parent declare that the compile plugin uses Java 5 for test compilation.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
Then in a child, you simple declare the compiler plugin and the configuration from the parent will be inherited:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
You can declare a plugin at the top level pom and tell it to be skipped and then tell it to not be skipped at the child level. It's not quite automatic, but very minimal in the override verbosity.
Parent Pom, disabling the plugin, but declaring all the config:
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<configuration>
<skip>true</skip>
...lots more config...
...lots more config...
...lots more config...
</configuration>
</plugin>
Child Pom, enabling the plugin:
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
I went with the following solution:
Configure the plugin in the parent-pom in the pluginManagement-section. Bind the plugin to an existing phase.
Deactivate the plugin for the parent-pom by binding it to a nonexistent phase: Override the phase in the plugins-section.
Activate the plugin in each child-pom by including the plugin in the plugins section.
Example parent-pom:
<defaultGoal>install</defaultGoal>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-ejb-client</id>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${ejb-client-file}</file>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<classifier>client</classifier>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</pluginManagement>
<plugins>
<plugin>
<!-- deactivate the plugin for this project, only child-projects do generate ejb-clients -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<id>install-ejb-client</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
Example child-pom:
<build>
<plugins>
...
<plugin>
<!-- Install the generated client-jar. Property 'ejb-client-file' has to be set! Plugin configuration is in the parent pom -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
</plugin>
...
</plugins>
</build>
As far as I know, there is no generic solution for this. At least for the moment...
One idea (I didn't try it, but it may work) is to define, in the parent pom.xml an execution goal that does not exist, for example:
<executions>
<execution>
<goals>
<goal>noGoal</goal>
</goals>
</execution>
</executions>
and in every child, you redefine a correct goal.
The problem of this solution (if it works, of course ;) ) is that you must redefine the plugin configuration for every child. Otherwise, it will not be executed.

Resources