什么是Maven的pom.xml中的pluginManagement?

问题描述 投票:225回答:4

这是我的pom文件的片段。

....
        <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>
...

我在命令中成功使用它

mvn install

但是,当我尝试将其封装到“pluginManagement”标签中时,maven-dependency-plugin在我启动install目标时停止工作。为什么“pluginManagement”标签会改变构建行为?或者我应该使用其他目标或选项?

java maven build pom.xml maven-dependency-plugin
4个回答
270
投票

你还需要添加

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

在你的构建中,因为pluginManagement只是一种在所有项目模块中共享相同插件配置的方法。

来自Maven文档:

pluginManagement:是一个沿侧插件看到的元素。插件管理以大致相同的方式包含插件元素,除了不是为这个特定的项目构建配置插件信息,它旨在配置从这个继承的项目构建。但是,这仅配置在子节点的plugins元素中实际引用的插件。孩子们有权重写pluginManagement定义。


225
投票

<pluginManagement/><plugins/>之间的区别在于:<plugin/>

  • <pluginManagement/>定义了构建中模块将继承的插件的设置。这适用于有父pom文件的情况。
  • <plugins/>是插件的实际调用。它可能会也可能不会继承自<pluginManagement/>

如果您的项目不是父POM,则不需要在项目中使用<pluginManagement/>。但是,如果它是父pom,那么在孩子的pom中,你需要有一个声明,例如:

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

请注意您没有定义任何配置。您可以从父级继承它,除非您需要根据子项目的需要进一步调整您的调用。

有关更具体的信息,您可以检查:


34
投票

您可以在父pom中使用pluginManagement来配置它,以防任何子pom想要使用它,但并非每个子插件都想使用它。一个例子可以是你的超级pom为maven Javadoc插件定义了一些选项。并非每个子pom都可能想要使用Javadoc,因此您可以在pluginManagement部分中定义这些默认值。想要使用Javadoc插件的子pom,只定义了一个插件部分,并将继承父pom中pluginManagement定义的配置。


3
投票

pluginManagement:是一个沿侧插件看到的元素。插件管理以大致相同的方式包含插件元素,除了不是为这个特定的项目构建配置插件信息,它旨在配置从这个继承的项目构建。但是,这仅配置在子节点的plugins元素中实际引用的插件。孩子们有权重写pluginManagement定义。

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

复制自:

Maven2 - problem with pluginManagement and parent-child relationship

© www.soinside.com 2019 - 2024. All rights reserved.