Search code examples
mavenmaven-3maven-plugin

How can I share a plugin configuration among a subset of maven profiles


Let's say I have three maven profiles.

  • local-dev
  • ci
  • prod

Right now I have ci and prod using the same exact plugin configuration. I don't want local-dev using it at all. So it looks something like this.

<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>

It turns out A-PLUGIN has a lot of configuration and since its the exact same across multiple profiles, I'd love to define it one place and just reference it from the profiles its needed in.


Solution

  • Let me cite from 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.

    I have made my experiences with profiles. I second this view.

    From a conceptual point of view you have three projects that have many or even most of their things in common. This is where Maven's POM hierarchy with its inheritance comes into play:

    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/...
    

    See The BaseBuild Element Set, Resources and The Build Element Set in the POM Reference for all the build directories to be redirected.

    See also Maven include another pom for plugin configuration:

    The only correct answer is to use inheritance.

    I second that, as well.