如何在 maven 配置文件的子集之间共享插件配置

duh*_*koh 2 maven-plugin maven-3 maven

假设我有三个 Maven 配置文件。

  • local-dev
  • ci
  • prod

现在我拥有ciprod使用相同的插件配置。我根本不想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>
Run Code Online (Sandbox Code Playgroud)

事实证明,A-PLUGIN它有很多配置,而且由于它在多个配置文件中完全相同,我很想将它定义在一个地方,然后从所需的配置文件中引用它。

Ger*_*ica 5

让我从Maven 论坛中引用:

我的结论是,如果一个人只是跳上配置文件作为每种情况的解决方案,那么构建将像糟糕的恐怖电影中的九头蛇一样长出第二个头,你真的会后悔的。

关键是配置文件通常是正确使用 Maven 的权宜之计。它们应始终被视为“最后的手段”,除非您知道自己在做什么。

在我的情况下,[...]。配置文件对此非常有用,但我现在可以确切地看到九头蛇头部适合野兽的位置,除非绝对必要,否则不打算对它们做更多事情。

我已经通过配置文件获得了经验。我赞同这个观点。

从概念的角度来看,您有三个项目,它们有很多甚至大部分的共同点。这就是 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/...
Run Code Online (Sandbox Code Playgroud)

该BaseBuild元素集资源构建元素集POM参考的重定向所有的编译目录。

另请参阅Maven 包含另一个用于插件配置的 pom

唯一正确的答案是使用继承。

我也赞同这一点。