ChatGPT解决这个技术问题 Extra ChatGPT

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

这是我的 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”标签会改变构建行为?或者我应该使用另一个目标或选项?


m
messivanio

您仍然需要添加

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

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

来自 Maven 文档:

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


c
carlspring

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

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

是插件实际调用的部分。它可能会或可能不会从 继承。

如果项目不是父 POM,则不需要在项目中包含 <pluginManagement/>。但是,如果它是父 pom,那么在孩子的 pom 中,您需要有如下声明:

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

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

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

Maven pom.xml 参考:插件

Maven pom.xml 参考:插件管理


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

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

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


谢谢你。我只是想在同一个 pom 文件中混合 pluginManagement 和插件标签,因为我需要绕过 Eclipse 的 M2E 插件的一个小错误。请参阅stackoverflow.com/questions/8706017/…
C
Community

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

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

复制自:

Maven2 - problem with pluginManagement and parent-child relationship


v
velocity

因此,如果我理解得很好,我会说 <pluginManagement> 就像 <dependencyManagement> 都用于仅在父模块及其子模块之间共享配置。

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

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

那正确吗 ?