Issue
I have been using maven for years now but I bumped into an issue.
As always I create a repo to illustrate my issue:
git clone https://github.com/clembo590/issues.git --branch mavenProfiles
I just created a multi module maven where I expect the QA profile to be active but it is not.
Here is how the parent pom look like:
<profiles>
<!-- build mdr-commons ONLY IF PRESENT -->
<profile>
<id>mdr-commons</id>
<activation>
<file>
<exists>mdr-commons/pom.xml</exists>
</file>
</activation>
<modules>
<module>mdr-commons</module>
</modules>
</profile>
<profile>
<id>QA</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<!-- this is to check that the coded is formatted : you can use 'apply' instead of 'check' to actually format the code -->
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
if you run mvn clean install
you will get
those logs:
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] mdr-parent [pom]
[INFO] mdr-commons [pom]
[INFO]
[INFO] -----------------------< com.example:mdr-parent >-----------------------
[INFO] Building mdr-parent 1.0.0 [1/2]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mdr-parent ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mdr-parent ---
[INFO] Installing ...
[INFO]
[INFO] ----------------------< com.example:mdr-commons >-----------------------
[INFO] Building mdr-commons 1.0.0 [2/2]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mdr-commons ---
[INFO]
[INFO] --- spotless-maven-plugin:2.11.1:check (default) @ mdr-commons ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mdr-commons ---
[INFO] Installing ...
[INFO] ------------------------------------------------------------------------
spotless-maven-plugin is not executed when building mdr-parent which means that QA profile is not active when build mdr-parent. (it is active when building mdr-commons)
question 1: why is QA profile not active when building mdr-parent?
question 2:
just delete completely the folder called mdr-commons and re run mvn clean install
--> why is QA profile active this time ? (you will see in the logs that the spotless-maven-plugin is executed)
thanks in advance maven experts
Solution
The answer can be found in Details on profile activation of the Maven Guide.
TLDR; Profiles which are activeByDefault are deactivated whenever another profile was activated by any other method.
The mdr-commons profile is activated if file mdr-commons/pom.xml
exists. In that case, the QA profile is deactivated. If this file does not exist, the QA profile is "active by default".
Answered By - Mihe
Answer Checked By - Marilyn (JavaFixing Volunteer)