What is pluginManagement in Maven's pom.xml?
Asked Answered
G

5

327

This is a snippet of my pom file.

...
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>                        
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            ......
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
...

I use it successfully with the command

mvn install

But, when I try to enclose it into the "pluginManagement" tag, the maven-dependency-plugin stops working when I launch the install goal. Why does the "pluginManagement" tag change the build behavior? Or should I use another goal or option?

Goffer answered 7/5, 2012 at 13:43 Comment(0)
G
352

You still need to add

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
    </plugin>
</plugins>

in your build, because pluginManagement is only a way to share the same plugin configuration across all your project modules.

From Maven documentation:

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.

Grantgranta answered 7/5, 2012 at 13:49 Comment(1)
You could even omit <groupId>org.apache.maven.plugins</groupId> (for this special case for core plugins)Oddball
O
343

The difference between <pluginManagement/> and <plugins/> is that a <plugin/> under:

  • <pluginManagement/> defines the settings for plugins that will be inherited by modules in your build. This is great for cases where you have a parent pom file and would like to avoid having to copy the same code for the configuration of the plugin over to each of these modules.

  • <plugins/> is a section for the actual invocation of the plugins. It may or may not be inherited from a <pluginManagement/>.

You don't need to have a <pluginManagement/> in your project, if it's not a parent POM. However, if it's a parent pom, then in the child's pom, you need to have a declaration like:

<plugins>
    <plugin>
        <groupId>com.foo</groupId>
        <artifactId>bar-plugin</artifactId>
    </plugin>
</plugins>

Notice how you aren't defining any configuration. You can inherit it from the parent, unless you need to further adjust your invocation as per the child project's needs.

For more specific information, you can check:

Objectivism answered 7/5, 2012 at 13:59 Comment(4)
Thank you for your reply. I need to mix the pluginManagement and plugin tag (for the maven-dependency-plugin) on the same pom file, because I need to bypass a little bug of M2E Eclipse IDE plugin. See #8706517Goffer
Thanks! :) This is the same for <dependency/> and <dependencyManagement/>. You define the dependencies (along with their versions and scopes, if you like) in the <dependencyManagement/> section and then in the <dependencies/> section you just define the groupId and artifactId.Objectivism
if i have to execute a plugin twice, should i use pluginmanagement?Volution
@KalpeshSoni: It depends -- you may want to have common configuration between the two executions which you don't want to have to redefine.Objectivism
B
46

You use pluginManagement in a parent pom to configure it in case any child pom wants to use it, but not every child plugin wants to use it. An example can be that your super pom defines some options for the maven Javadoc plugin.

Not each child pom might want to use Javadoc, so you define those defaults in a pluginManagement section. The child pom that wants to use the Javadoc plugin, just defines a plugin section and will inherit the configuration from the pluginManagement definition in the parent pom.

Balas answered 7/5, 2012 at 13:49 Comment(1)
Thank you. I just looking to mix pluginManagement and plugin tags on the same pom file because I need to bypass a little bug of the M2E plugin for Eclipse. See the #8706517Goffer
R
4

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.

From http://maven.apache.org/pom.html#Plugin%5FManagement

Copied from :

Maven2 - problem with pluginManagement and parent-child relationship

Rockwell answered 7/5, 2012 at 13:47 Comment(0)
T
1

<pluginManagement> just like <dependencyManagement> are both used to share only the configuration between a parent and it's sub-modules.

For that we define the dependencie's and plugin's common configurations in the parent project and then we only have to declare the dependency/plugin in the sub-modules to use it, without having to define a configuration for it (i.e version or execution, goals, etc). Though this does not prevent us from overriding the configuration in the submodule.

In contrast <dependencies> and <plugins> are inherited along with their configurations and should not be redeclared in the sub-modules, otherwise a conflict would occur.

Tilghman answered 16/12, 2022 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.