ChatGPT解决这个技术问题 Extra ChatGPT

What is pluginManagement in Maven's pom.xml?

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?


m
messivanio

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.


c
carlspring

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

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.

is a section for the actual invocation of the plugins. It may or may not be inherited from a .

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:

The Maven pom.xml reference: Plugins

The Maven pom.xml reference: Plugin Management


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 stackoverflow.com/questions/8706017/…
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.
if i have to execute a plugin twice, should i use pluginmanagement?
@KalpeshSoni: It depends -- you may want to have common configuration between the two executions which you don't want to have to redefine.
C
CodeSlave

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.


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 stackoverflow.com/questions/8706017/…
C
Community

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


v
velocity

So if i understood well, i would say that <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.

is that right ?