Skip maven plugin with property from parent pom?

333 Views Asked by At

I like to skip a maven plugin by setting the skip-docker-build property via a profile.

<profiles>
  <profile>
    <id>skip-docker-build</id>
    <activation>
      <activeByDefault>false</activeByDefault>
    </activation>
    <properties>
      <skip-docker-build>true</skip-docker-build>
    </properties>
  </profile>
</profiles>

<build>
  <plugins>
    <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>dockerfile-maven-plugin</artifactId>
      <configuration>
        <skip>${skip-docker-build}</skip>
      </configuration>
    </plugin>
  </plugins>
</build>

This all works well, when I activate the profile, the docker build is skipped. However, when I put the profiles section in the parent pom it's no longer working. How to make this work when the profile is defined in the parent pom?

2

There are 2 best solutions below

0
René Winkler On BEST ANSWER

I solved the problem by swapping the logic. I don't have a skip-docker-build, but a docker-build profile. The plugin is wrapped in this profile and if I don't want to run it then I disable it as follows

mvn clean install -P !docker-build

<profiles>
    <profile>
      <id>docker-build</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
0
Andrey B. Panfilov On

Maven profiles are not getting inherited from paren pom, that is clearly stated in documentation:

Elements in the POM that are merged are the following:

  • dependencies
  • developers and contributors
  • plugin lists (including reports)
  • plugin executions with matching ids
  • plugin configuration
  • resources

if you are observing that enabling/disabling profile defined in parent pom is somehow influencing on child pom, that happens due to following merging strategy:

  • maven parses child pom, applies profiles defined in child pom and settings.xml
  • after that maven merges child pom with parent pom, before doing that it applies profiles defined in parent pom and settings.xml to parent pom

In your particular case I believe the initial idea was following:

  • you would like to setup dockerfile-maven-plugin in parent pom and enable/disable it's execution via specifying skip-docker-build property in child modules, just because not all child modules are actually "applications"
  • after that you have realised that not every mvn install should trigger execution of dockerfile-maven-plugin - the actual behaviour should depend on environment and developer's preferences, so, you decided to take advantage of using maven profiles

The "proper" way of doing that is to define profile in ~/.m2/settings.xml