Maven 的 pom.xml 中的 pluginManagement 是啥?

Posted

技术标签:

【中文标题】Maven 的 pom.xml 中的 pluginManagement 是啥?【英文标题】:What is pluginManagement in Maven's pom.xml?Maven 的 pom.xml 中的 pluginManagement 是什么? 【发布时间】:2012-05-16 00:37:06 【问题描述】:

这是我的 pom 文件的 sn-p。

....
        <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”标签会改变构建行为?或者我应该使用其他目标或选项?

【问题讨论】:

【参考方案1】:

你还需要添加

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

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

来自 Maven 文档:

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

【讨论】:

【参考方案2】:

&lt;pluginManagement/&gt;&lt;plugins/&gt; 的区别在于:&lt;plugin/&gt; 下:

&lt;pluginManagement/&gt; 定义将由构建中的模块继承的插件设置。这对于有父 pom 文件的情况非常有用。

&lt;plugins/&gt; 是插件实际调用的部分。它可能继承自 &lt;pluginManagement/&gt;,也可能不继承。

如果不是父 POM,您的项目中不需要有 &lt;pluginManagement/&gt;。但是,如果它是父 pom,那么在子 pom 中,您需要有如下声明:

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

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

更具体的信息可以查看:

Maven pom.xml 参考:Plugins

Maven pom.xml 参考:Plugin Management

【讨论】:

感谢您的回复。我需要在同一个 pom 文件中混合 pluginManagement 和插件标签(用于 maven-dependency-plugin),因为我需要绕过 M2E Eclipse IDE 插件的一个小错误。见***.com/questions/8706017/… 谢谢! :) 这对于&lt;dependency/&gt;&lt;dependencyManagement/&gt; 是一样的。您在&lt;dependencyManagement/&gt; 部分定义依赖项(以及它们的版本和范围,如果您愿意),然后在&lt;dependencies/&gt; 部分中您只需定义groupIdartifactId 如果我必须执行两次插件,我应该使用插件管理吗? @KalpeshSoni:这取决于——您可能希望在两个执行之间有一个您不想重新定义的通用配置。【参考方案3】:

您在parent pom 中使用pluginManagement 来配置它,以防任何child pom 想要使用它,但不是每个子插件都想要使用它。例如,您的 super pom 为 maven Javadoc 插件定义了一些选项。

并非每个child pom 都可能想要使用Javadoc,因此您可以在pluginManagement 部分中定义这些默认值。想要使用 Javadoc 插件的子 pom 只定义了一个插件部分,并将继承来自 parent pom 中的 pluginManagement 定义的配置。

【讨论】:

谢谢。我只是想在同一个 pom 文件中混合 pluginManagement 和插件标签,因为我需要绕过 Eclipse 的 M2E 插件的一个小错误。见***.com/questions/8706017/…【参考方案4】:

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

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

复制自:

Maven2 - problem with pluginManagement and parent-child relationship

【讨论】:

【参考方案5】:

所以如果我理解得很好,我会说&lt;pluginManagement&gt; 就像&lt;dependencyManagement&gt; 都用于仅在父模块和它的子模块之间共享配置。

为此,我们在父项目中定义了依赖和插件的通用配置,然后我们只需要在子模块中声明依赖/插件即可使用它,而无需为其定义配置(即版本或执行、目标等)。虽然这并不妨碍我们覆盖子模块中的配置。

相比之下&lt;dependencies&gt;&lt;plugins&gt;是随配置一起继承的,不应在子模块中重新声明,否则会发生冲突。

是吗?

【讨论】:

以上是关于Maven 的 pom.xml 中的 pluginManagement 是啥?的主要内容,如果未能解决你的问题,请参考以下文章

Maven 的 pom.xml 中的 pluginManagement 是啥?

maven-release-plugin:pom.xml 的 scm 部分中的文件夹是不是相关?

pom.xml中properties标签中的maven编译器和plugin标签中的maven编译器之间的区别[重复]

Maven pom.xml 中的 pluginManagement 是啥?

由于 pom.xml 中的错误,无法使用 Maven 运行构建

eclipse中的Maven pom.xml错误[重复]