这是我的 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”标签会改变构建行为?或者我应该使用另一个目标或选项?
您仍然需要添加
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
在您的构建中,因为 pluginManagement
只是在所有项目模块之间共享相同插件配置的一种方式。
来自 Maven 文档:
pluginManagement:是一个在侧面插件中可见的元素。插件管理以几乎相同的方式包含插件元素,除了不是为这个特定的项目构建配置插件信息,它旨在配置从这个项目继承的项目构建。但是,这仅配置子级的 plugins 元素中实际引用的插件。孩子们有权覆盖 pluginManagement 定义。
<pluginManagement/>
和 <plugins/>
之间的区别在于 <plugin/>
位于:
如果项目不是父 POM,则不需要在项目中包含 <pluginManagement/>
。但是,如果它是父 pom,那么在孩子的 pom 中,您需要有如下声明:
<plugins>
<plugin>
<groupId>com.foo</groupId>
<artifactId>bar-plugin</artifactId>
</plugin>
</plugins>
请注意您没有定义任何配置。您可以从父项目继承它,除非您需要根据子项目的需要进一步调整您的调用。
有关更具体的信息,您可以查看:
Maven pom.xml 参考:插件
Maven pom.xml 参考:插件管理
您可以在 parent pom
中使用 pluginManagement
来配置它,以防任何 child pom
想要使用它,但不是每个子插件都想要使用它。例如,您的 super pom
为 maven Javadoc 插件定义了一些选项。
并非每个 child pom
都可能想要使用 Javadoc,因此您可以在 pluginManagement
部分中定义这些默认值。想要使用 Javadoc 插件的子 pom 只需定义一个插件部分并将继承来自 parent pom
中 pluginManagement
定义的配置。
pluginManagement:是一个在侧面插件中可见的元素。插件管理以几乎相同的方式包含插件元素,除了不是为这个特定的项目构建配置插件信息,它旨在配置从这个项目继承的项目构建。但是,这仅配置子级的 plugins 元素中实际引用的插件。孩子们有权覆盖 pluginManagement 定义。
来自http://maven.apache.org/pom.html#Plugin%5FManagement
复制自:
Maven2 - problem with pluginManagement and parent-child relationship
因此,如果我理解得很好,我会说 <pluginManagement>
就像 <dependencyManagement>
都用于仅在父模块及其子模块之间共享配置。
为此,我们在父项目中定义依赖项和插件的通用配置,然后我们只需在子模块中声明依赖项/插件即可使用它,而无需为其定义配置(即版本或执行、目标、 ETC)。虽然这并不妨碍我们覆盖子模块中的配置。
相比之下,<dependencies>
和 <plugins>
是与其配置一起继承的,不应在子模块中重新声明,否则会发生冲突。
那正确吗 ?
<dependency/>
和<dependencyManagement/>
是一样的。您在<dependencyManagement/>
部分定义依赖项(以及它们的版本和范围,如果您愿意),然后在<dependencies/>
部分中您只需定义groupId
和artifactId
。