Creating exclusive profiles in maven - maven

I currently have a pom with platform specific profiles (e.g. linux 32bit, windows 64 bit, etc...). Additionally, I have set it up to automatically choose the invoker's platform as a default.
Now, assume I am in a linux 32 machine: I also want to build for win64 by invoking mvn -Pwin64 pakage but in doing so, both the linux32 and win64 profiles get activated. I have tried activating the local platform profile with activeProfiles and using ativation tags. The trouble is that -P does not disable all other profiles as explained in the documentation:
This option takes an argument that is a comma-delimited list of
profile-ids to use. When this option is specified, no profiles other
than those specified in the option argument will be activated.
Am I understanding this wrong? How would you handle this?
Note: I know I could run mvn -P-linux32,win64 but that is only valid on linux32 platforms, and any mistakes may result in a bloated build with duplicate classes.
Thanks!

This statement from the profile docs:
As of Maven 3.0, profiles in the POM can also be activated based on properties from active profiles from the settings.xml.
Would lead me to try the solution below. Each developer defines his default platform as a property in his settings.xml file and overrides it on the cmdline if needed.
Developer's settings.xml
<profile>
<id>platform-config</id>
<property>
<name>build.platform</name>
<value>win32</value>
</property>
</profile>
....
<activeProfiles>
<activeProfile>platform-config</activeProfile>
</activeProfiles>
Project's pom.xml
<project>
...
<profiles>
<profile>
<id>win32</id>
<activation>
<property>
<name>build.platform</name>
<value>win32</value>
</property>
</activation>
...
</profile>
<profile>
<id>linux32</id>
<activation>
<property>
<name>build.platform</name>
<value>linux32</value>
</property>
</activation>
...
</profile>
</profiles>
Then, mvn install should activate the win32 profile because the default value for the build.platform property is win32, while mvn install -Dbuild.platform=linux32 will override the default property setting and use the Linux profile instead.

Why don't you use the profile activation by plattform like this:
<project>
...
<profiles>
<profile>
<id>win32</id>
<activation>
<activeByDefault>false</activeByDefault>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
...
</profile>
</profiles>
</project>

Related

Activating a Child Profile from a Parent Profile

I have the following parent pom.xml file:
<profile>
<id>build_full</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>mymodule_interface</module>
<module>mymodule_switch</module>
<module>mymodule_switch_simulator</module>
<module>mymodule_switch_controller</module>
<module>mymodule_server</module>
</modules>
</profile>
and in my child pom for mymodule_server, I have the following:
<profile>
<id>subprofile</id>
<modules>
<module>...various modules...</module>
</modules>
</profile>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>...various modules...</module>
</modules>
</profile>
How, when I invoke maven: mvn -P build_full, can I force the child module (mymodule_server) to use profile subprofile rather than default?
No, you can't activate a child profile from a parent profile. More generally, you can't activate or deactivate any profile from any other profile (there is a JIRA feature-request MNG-3309). What you can do is activate two profiles based on the same property.
First of all, using a profile that is activated by default is generally not a good idea. What you want is to activate a profile based on some condition (OS version, system property...). To solve your problem, you can activate build_full profile when a certain system property is present, and make sure that subprofile is also activated when that same property is present.
A sample configuration would be the following, where both profiles are activated when the fullBuild system property is set to true. Invoking Maven with mvn -DfullBuild=true ... will thus activate both profiles.
<profile>
<id>build_full</id>
<activation>
<property>
<name>fullBuild</name>
<value>true</value>
</property>
</activation>
<modules>
<module>mymodule_interface</module>
<module>mymodule_switch</module>
<module>mymodule_switch_simulator</module>
<module>mymodule_switch_controller</module>
<module>mymodule_server</module>
</modules>
</profile>
<profile>
<id>subprofile</id>
<activation>
<property>
<name>fullBuild</name>
<value>true</value>
</property>
</activation>
<modules>
<module>...various modules...</module>
</modules>
</profile>
In your case, from the top parent/aggregator folder, you could just run:
mvn clean install -Pbuild_full,!default,subprofile
It will disable any profile having name default (and hence disable the profile in the concerned sub-module) and enable any profile having name subprofile (and hence enable the profile you wanted).
Alternatively, you could configure subprofile as such:
<profiles>
<profile>
<id>subprofile</id>
<activation>
<property>
<name>subprofile</name>
<value>true</value>
</property>
</activation>
....
and then run as following:
mvn clean install -Dsubprofile=true -Pbuild_full
It will have the same effect. You can even avoid the value element and simply specify -Dsubprofile, its existence would be enough to activate the profile (in that case a more meaningful name is suggested, like -DactivateSubprofile). Since you active a different profile, automatically Maven will deactivate the default one.

Triggering Maven profiles from Heroku configured Environment Variables

I am trying to activate a profile in my maven pom as follows:-
<profile>
<id>test</id>
</properties>
<activation>
<property>
<name>env.SITE</name>
<value>test</value>
</property>
</activation>
</profile>
Now i am configuring heroku to have the environment variable SITE as follows:-
heroku config:add SITE=test.
I expect the environment variable to trigger the profile when code is pushed. However this is not happening.
You can do this without a custom build pack.
Use this snippet in your pom.xml to display all properties available on Heroku and pick one that is not in your local: http://florianlr.wordpress.com/2012/04/24/16/
I used env.DYNO
<profile>
<id>heroku</id>
<activation>
<property>
<name>env.DYNO</name>
</property>
</activation>
...
</profile>
...
Works like a charm:)
You can specify the maven profile using the environment variable MAVEN_CUSTOM_OPTS.
In your pom define maven profile activation to be based off the presence of a property. The below example uses the development profile if you specify it and defaults to production if you don't. reference
<profiles>
<profile>
<id>production</id>
<activation>
<property>
<name>!profile-development</name>
</property>
</activation>
</profile>
<profile>
<id>development</id>
<activation>
<property>
<name>profile-development</name>
</property>
</activation>
</profile>
</profiles>
In Heroku set the MAVEN_CUSTOM_OPTS environment variable to use the profile. Note that this variable overrides the default option -DskipTests. reference
MAVEN_CUSTOM_OPTS=-DskipTests -Dprofile-development
In this example the production profile would not need MAVEN_CUSTOM_OPTS declared.
Or you can introduce your own customized Maven settings.xml file, e.g. heroku-settings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- activate by setting the MAVEN_SETTINGS_PATH config var to heroku-settings.xml in Heroku project settings tab.
See https://devcenter.heroku.com/articles/using-a-custom-maven-settings-xml for more details.
-->
<activeProfiles>
<activeProfile>production</activeProfile>
</activeProfiles>
</settings>
Then activate the settings by setting the MAVEN_SETTINGS_PATH config var to heroku-settings.xml in Heroku project settings tab
The config vars aren't currently available at compile time. To change the Maven profile you will need to fork the Heroku Java Buildpack.
I solved my problem by adding Heroku Environment like below:
MAVEN_CUSTOM_OPTS=-P<profile_id>
and cut out "activation" tag from pom.xml.
Btw I don't know why this doesn't work (in Heroku I have ENVIRONMENT=test)
<profile>
<id>test</id>
<activation>
<property>
<name>env.ENVIRONMENT</name>
<value>test</value>
</property>
</activation>
...
</profile>

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.

Confused that order matters when defining conditional profiles in maven

I have a configurable property line.ending that I used during the assembly phase of the building of my project to specify the line ending type of my application property files. For that I have created two profiles LF_DOS and LF_UNIX, so that when I launch :
mvn install
or
mvn install -P LF_DOS
line.ending equals 'dos', and when I launch :
mvn install -P LF_UNIX
line.ending equals 'unix'.
My first attempt to do this was simply :
<profile>
<id>LF_UNIX</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<line.ending>unix</line.ending>
</properties>
</profile>
<profile>
<id>LF_DOS</id>
<activation>
<property>
<name>!line.ending</name>
</property>
</activation>
<properties>
<line.ending>dos</line.ending>
</properties>
</profile>
Unfortunately, this always gave me line.ending=dos, whatever LF_UNIX is set or not. Weird... But, the more confusing to me, is that I solved the problem just by changing the profile declaration order, like this :
<profile>
<id>LF_DOS</id>
<activation>
<property>
<name>!line.ending</name>
</property>
</activation>
<properties>
<line.ending>dos</line.ending>
</properties>
</profile>
<profile>
<id>LF_UNIX</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<line.ending>unix</line.ending>
</properties>
</profile>
This works exactly like I want.
My questions is : is this a bug ? Or is it something to know about maven profiles, a kind of limitation that makes profiles order declaration particularly matter in such a case ?
The confusion lies in your understanding of how profile activation works.
You think that this:
<activation>
<property>
<name>!line.ending</name>
</property>
</activation>
means if I don't have a maven property named "line.ending" set, activate this profile. What it really means if I didn't specify -Dline.ending=X on the command line, activate this profile. So unless you run something like this:
mvn clean install -Dline.ending=unix
You are activating this profile and thus having the value set to dos.

Maven: Only activate profile A if profile B is not activated?

I have two Maven profiles profile-A and profile-B. "B" should only be activated if "A" is not activated.
So if I would call
mvn install
profile-B is executed (but not profile-A).
But if I would call
mvn install -Pprofile-A
then only profile-A is executed (but not profile-B).
Any hints how I need to write my pom.xml to achieve this?
I already tried this, but it doesn't work:
<profiles>
<profile>
<id>profile-A</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
...
</profile>
<profile>
<id>profile-B</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>!profile-A</name>
</property>
...
</activation>
...
</profile>
</profiles>
I think for your example command line to work as expected, all you need is the <activeByDefault>true</activeByDefault> for profile B.
http://maven.apache.org/guides/introduction/introduction-to-profiles.html states:
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.

Resources