can't deactivate activeBydefault profile on maven build - maven

I use maven 3.8.5 in a web application with different layer. The parent pom is something similar to the follow:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>6.4.0</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>other</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
Now if I manually change the activationByDefault of dev profile from true to false and activationByDefault of other profile from false to true when I build (mvn clean package) everything goes fine.
Instead If I perform a build command like follow to obtain the same result:
mvn clean package -P other
doesn't work.. and I still find flyway library in my project. What's wrong?

Related

Activate 2 build profile on maven via command [duplicate]

I have two profiles for different environments in pom.xml, I have to run mvn -PTest1 install and mvn -PTest2 install command to get these profiles in use. Can we integrate two separate maven commands in a single one (like mvn clean install)?
Here is my Pom entry
<profiles>
<profile>
<id>Test1</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.5</jdk>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>sparrow-type</name>
<value>African</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>
com.endeca
</groupId>
<artifactId>
endeca_navigation_Test1
</artifactId>
<version>
6.1
</version>
<!--<version>stable</version> -->
<scope>
compile
</scope>
</dependency>
</profile>
<profile>
<id>Test2</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.5</jdk>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>sparrow-type</name>
<value>African</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>
com.endeca
</groupId>
<artifactId>
endeca_navigation_Test2
</artifactId>
<version>
6.1
</version>
<!--<version>stable</version> -->
<scope>
compile
</scope>
</dependency>
</dependencies>
</profile>
</profiles>
It will helpfull to manage hudson job using single command
Based on the documentation and discussion here, try separating profile names with a comma:
mvn install -P Test1,Test2
Mifeet's answer is correct, but in Windows PowerShell you should quote parameters, otherwise you'll get "unknown lifecycle phase" error.
mvn install -P 'Test1,Test2'
For me Mifeet's answer isn't working. I get "unknown lifecycle phase Test2". For me this is working:
mvn install -PTest1 -PTest2
Based on the maven help command
-P,--activate-profiles <arg> Comma-delimited list of profiles to activate
So you can run mvn package -Pp1,p2 to run profile id with p1 and p2

Maven - Profile Depending on Property?

I have the following profile below, instead of using activeByDefault I would like to active it through a property instead. I saw this comment (https://stackoverflow.com/a/18890159/1959534) in passing about being able to do so through maven assembly plugin but I couldn't figure out how to specify that. Any input on how to accomplish this?
<project ...>
...
<profiles>
<profile>
<id>myProfile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<!-- Extra dependency just for this profile -->
</dependency>
</dependencies>
</profile>
</profiles>
</project>
Figured it out!
Defined a system variable in the project's /.mvn/jvm.config and then changed the activation step to be through property instead of activeByDefault. Then used this tips to throw config out to my .properties file: https://stackoverflow.com/a/14386303/1959534
jvm.config
-Dmy.property=true
pom.xml
<profiles>
<profile>
...
<activation>
<property>
<name>my.property</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>

maven3 detect maven version for plugin - fails to activate other default profiles

I have a need to detect if a user is using mvn2 or mvn3 in my parent pom in order to load the proper plugin version. I followed the recommendation from here : http://maven.apache.org/plugins/maven-site-plugin/maven-3.html#Using_maven-site-plugin_2.x_with_Maven_2_and_maven-site-plugin_3.x_with_Maven_3
The detection mechanism works great - however, my other profiles that are activatedByDefaul do not get picked up anymore.
Super pom look like below:
<profile>
<id>profile-1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>profile-2</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>maven-3</id>
<activation>
<file>
<!-- This employs that the basedir expression is only recognized by Maven 3.x (see MNG-2363) -->
<exists>${basedir}</exists>
</file>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.company.plugins</groupId>
<artifactId>my-super-plugin</artifactId>
<version>1.0-123</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
when I run mvn help:active-profiles with mvn3 --> only maven-3 profile get listed. If I use mvn2, profile-1 is rightfully listed.
*Edit * : as it turns out, its actually well documented here : http://maven.apache.org/guides/introduction/introduction-to-profiles.html
This profile will automatically be active for all builds unless another profile in the same POM is activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in the POM is activated on the command line or through its activation config.
My question is now then : what work around would you recommend to have profile1 activated by default and profile 2 activated if -P profile2, while maven-3 profile activated if maven3 is used?
So far I haven't found anything better than just:
<activation>
<property>
<name>!dummy</name>
</property>
</activation>
where dummy is some kind of stupid variable name that you won't use for sure.

Change maven dependency's version by using different maven profiles

I have two projects, project A is depending on project B, so normally, I'd have the following section in my projectA/pom.xml:
<dependency>
<artifactId>projectB</artifactId>
<groupId>blabla</groupId>
<version>version1</version>
</dependency>
What I am trying to achieve is very straight forward, does maven profile allow me to do anything like:
if(profileA) {
<version>version1</version>
}
else {
<version>version2</version>
}
Yes, this can be done (put activeByDefault to whichever profile you need to be default).
<dependency>
<artifactId>projectB</artifactId>
<groupId>blabla</groupId>
<version>${dependency.version}</version>
</dependency>
...
<profiles>
<profile>
<id>first</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<dependency.version>1.2.3</dependency.version>
</properties>
</profile>
<profile>
<id>second</id>
<properties>
<dependency.version>2.3.4</dependency.version>
</properties>
</profile>
</profiles>

How to generate two sonar reports from the same project?

I would like to create two sets of Sonar reports from the same project. One would have everything covered and the other one would have some packages excluded.
Is this possible and if so, how to do such?
Edit: Setting exclusions is not a problem but having two reports is.
Create new profile in maven and add call sonar with new branch for each profile: mvn clean install -Pprofile1 sonar:sonar -Dsonar.branch=BRANCH1
<properties>
<sonar.branch>
DEFAULT_BRANCH
</sonar.branch>
</properties>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://localhost:9000
</sonar.host.url>
</properties>
</profile>
<profile>
<id>profile1</id>
<properties>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>
http://myserver:9000
</sonar.host.url>
</properties>
</profile>
</profiles>

Resources