如何在 Maven 配置文件的子集之间共享插件配置
How can I share a plugin configuration among a subset of maven profiles
假设我有三个 Maven 配置文件。
local-dev
ci
prod
现在我有 ci
和 prod
使用完全相同的插件配置。我根本不想 local-dev
使用它。所以它看起来像这样。
<profile>
<id>local-dev</id>
</profile>
<profile>
<id>ci</id>
<build>
<plugins>
<plugin>
<artifactId>A-PLUGIN</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>A-PLUGIN</artifactId>
</plugin>
</plugins>
</build>
</profile>
事实证明 A-PLUGIN
有很多配置,并且由于它在多个配置文件中完全相同,我喜欢将它定义在一个地方,然后从需要的配置文件中引用它。
让我引用 the Maven forum:
My takeaway was that if one just jumps on profiles as the solution to every conditional situation, the build will grow a second head like a hydra in a bad horror flick and you'll really regret it.
The point is that profiles are often an expedient substitute for using Maven correctly. They should always be considered "last resort" unless you know what you are getting yourself into.
In my situation, [...]. Profiles work great for that, but I can see exactly where the hydra head fits on the beast now and don't intend on doing anything more with them unless absolutely necessary.
我对配置文件进行了体验。我支持这个观点。
从概念的角度来看,您有三个项目,它们有很多甚至大部分的共同点。这就是 Maven 的 POM 层次结构及其继承发挥作用的地方:
main-parent
+- pom.xml ... containing declarations common to dev, ci & prod
+- dev
| +- pom.xml ... almost empty since everything's inherited from parent
| +- ... sources, resources, etc. ...
+- ci-prod-parent
+- pom.xml ... containing A-PLUGIN and other declarations specific to ci & prod
+- ci
| +- pom.xml ... almost empty since everything's inherited from parent(s)
| redirecting <build>/<[[test]source|resource>/]|
| [[test]output|testresource>/]directory> to ../../dev/...
+- prod
+- pom.xml ... almost empty since everything's inherited from parent(s)
redirecting <build>/<[[test]source|resource>/]|
[[test]output|testresource>/]directory> to ../../dev/...
有关要重定向的所有构建目录,请参阅 POM 参考 中的 The BaseBuild Element Set, Resources and The Build Element Set。
另见 Maven include another pom for plugin configuration:
The only correct answer is to use inheritance.
我也支持。
假设我有三个 Maven 配置文件。
local-dev
ci
prod
现在我有 ci
和 prod
使用完全相同的插件配置。我根本不想 local-dev
使用它。所以它看起来像这样。
<profile>
<id>local-dev</id>
</profile>
<profile>
<id>ci</id>
<build>
<plugins>
<plugin>
<artifactId>A-PLUGIN</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>A-PLUGIN</artifactId>
</plugin>
</plugins>
</build>
</profile>
事实证明 A-PLUGIN
有很多配置,并且由于它在多个配置文件中完全相同,我喜欢将它定义在一个地方,然后从需要的配置文件中引用它。
让我引用 the Maven forum:
My takeaway was that if one just jumps on profiles as the solution to every conditional situation, the build will grow a second head like a hydra in a bad horror flick and you'll really regret it.
The point is that profiles are often an expedient substitute for using Maven correctly. They should always be considered "last resort" unless you know what you are getting yourself into.
In my situation, [...]. Profiles work great for that, but I can see exactly where the hydra head fits on the beast now and don't intend on doing anything more with them unless absolutely necessary.
我对配置文件进行了体验。我支持这个观点。
从概念的角度来看,您有三个项目,它们有很多甚至大部分的共同点。这就是 Maven 的 POM 层次结构及其继承发挥作用的地方:
main-parent
+- pom.xml ... containing declarations common to dev, ci & prod
+- dev
| +- pom.xml ... almost empty since everything's inherited from parent
| +- ... sources, resources, etc. ...
+- ci-prod-parent
+- pom.xml ... containing A-PLUGIN and other declarations specific to ci & prod
+- ci
| +- pom.xml ... almost empty since everything's inherited from parent(s)
| redirecting <build>/<[[test]source|resource>/]|
| [[test]output|testresource>/]directory> to ../../dev/...
+- prod
+- pom.xml ... almost empty since everything's inherited from parent(s)
redirecting <build>/<[[test]source|resource>/]|
[[test]output|testresource>/]directory> to ../../dev/...
有关要重定向的所有构建目录,请参阅 POM 参考 中的 The BaseBuild Element Set, Resources and The Build Element Set。
另见 Maven include another pom for plugin configuration:
The only correct answer is to use inheritance.
我也支持。