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

325

这是我的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"标签中时,当我启动"install"目标时,maven-dependency-plugin停止工作。 为什么"pluginManagement"标签会改变构建行为?或者我应该使用另一个目标或选项吗?

5个回答

349
你仍然需要添加。
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
    </plugin>
</plugins>

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

来自Maven文档:

pluginManagement:是与插件一起看到的元素。 Plugin Management包含插件元素,其方式基本相同,只是与为该特定项目构建配置插件信息不同,它旨在配置继承自此构建的项目构建。然而,这仅会配置实际上在子代中的插件元素中引用的插件。子元素有权覆盖pluginManagement定义。


你甚至可以省略 <groupId>org.apache.maven.plugins</groupId> (对于核心插件的特殊情况)。 - Christian Ullenboom

339
"<pluginManagement/>" 和 "<plugins/>" 的区别在于,"<pluginManagement/>" 下的 "<plugin/>" 定义了插件的设置,这些设置会被构建中的模块继承。这对于你有一个父POM文件并且想要避免将相同的插件配置代码复制到每个模块中的情况非常有用。
而 "<plugins/>" 是实际调用插件的部分,它可能会从 "<pluginManagement/>" 继承,也可能不继承。
如果你的项目不是父POM,则不需要在其中包含 "<pluginManagement/>"。但是,如果它是父POM,则在子POM中,你需要像这样声明:"<pluginManagement/>"。
<plugins>
    <plugin>
        <groupId>com.foo</groupId>
        <artifactId>bar-plugin</artifactId>
    </plugin>
</plugins>

请注意,您不需要定义任何配置。您可以从父级继承它,除非您需要根据子项目的需求进一步调整调用。
要获取更具体的信息,请查看:
- Maven pom.xml 参考文档: 插件 - Maven pom.xml 参考文档: 插件管理

感谢您的回复。我需要在同一个pom文件中混合使用pluginManagement和plugin标签(针对maven-dependency-plugin),因为我需要绕过M2E Eclipse IDE插件的一个小错误。请参见https://dev59.com/Mmoy5IYBdhLWcg3wN7UX=_. - Andrea Borgogelli Avveduti
9
对于 <dependency/><dependencyManagement/> 都是一样的。您可以在 <dependencyManagement/> 中定义依赖项(包括它们的版本和作用域,如果需要),然后在 <dependencies/> 中只需定义 groupIdartifactId 即可。 - carlspring
1
如果我需要执行插件两次,我应该使用插件管理吗? - Kalpesh Soni
@KalpeshSoni:这取决于情况——您可能希望在两次执行之间具有共同的配置,而不必重新定义它们。 - carlspring

46

在一个父pom中使用pluginManagement对其进行配置,以便任何子pom想要使用它时可以使用它,但并不是每个子插件都想使用它。例如,您的超级pom为Maven Javadoc插件定义了一些选项。

并非每个子pom都想使用Javadoc,因此您在pluginManagement部分中定义了这些默认值。只需要在想要使用Javadoc插件的子pom中定义一个插件部分,就会从父pom中的pluginManagement定义继承配置。


谢谢。我只是想在同一个pom文件中混合使用pluginManagement和plugin标签,因为我需要绕过Eclipse的M2E插件的一个小错误。 请参见https://dev59.com/Mmoy5IYBdhLWcg3wN7UX=_. - Andrea Borgogelli Avveduti

4

pluginManagement是一个与插件一起出现的元素。Plugin Management以类似的方式包含插件元素,但不同之处在于,它旨在为继承自此项目构建的项目构建配置插件信息,而不是为此特定项目构建配置插件信息。但是,这仅配置实际在子级中插件元素中引用的插件。子元素有权覆盖pluginManagement定义。

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

摘自:Maven2 - problem with pluginManagement and parent-child relationship


1

<pluginManagement> 就像 <dependencyManagement> 一样,都用于在父项目和其子模块之间共享配置。

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

相比之下,<dependencies><plugins> 会连同其配置一起继承,并且不应在子模块中重新声明,否则会发生冲突。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接