1

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?

René Winkler
  • 6,508
  • 7
  • 42
  • 69
  • This part working has actually nothing to do with the profile itself. It is just that the `skip` for `dockerfile-maven-plugin` is controlled by the same property (`skip-docker-build`) that also controls the activation of the `skip-docker-build`-profile. – Turing85 May 27 '23 at 11:17
  • Not sure if this fixes the issue, but you could try moving the `build`-block into the `skip-docker-build`-profile, and remove the `skip`-part in the plugin's `` (or set it to `true`). – Turing85 May 27 '23 at 11:19
  • as not all my child-pom have a docker build, I tried to solve it via a property and not via moving the build-block into the profile. – René Winkler May 27 '23 at 11:55
  • you can always set it in `pluginManagement` and then include the plugin in the child-modules that should actually use this plugin. – Turing85 May 27 '23 at 11:59

2 Answers2

0

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>
René Winkler
  • 6,508
  • 7
  • 42
  • 69
0

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

Andrey B. Panfilov
  • 4,324
  • 2
  • 12
  • 18