maven -> profile -> activation - all conditions are required or just one? - java

Configuration:
- Maven: 3.0.5
- Java: 1.6.0_45
Description:
Let's say we have profile configuration like below:
<profiles>
<profile>
<id>profile-1</id>
<activation>
<jdk>1.6</jdk>
<property>
<name>name</name>
<value>Hubert</value>
</property>
</activation>
</profile>
<profile>
<id>profile-2</id>
<activation>
<jdk>1.6</jdk>
<property>
<name>name</name>
<value>Wiktoria</value>
</property>
</activation>
</profile>
</profiles>
We have two profiles: profile-1 and profile-2.
Profile profile-1 should be active when two requirements are met:
- jdk is version 1.6
- property name has value Hubert
Question:
Let's check this configuration:
mvn -Dname=Hubert help:active-profiles
As a result I get that there are two active profiles: profile-1 and profile-2.
Hmm...
Profile profile-2 should not be active since property name has value different from expected Wiktoria.
Could someone explain me why this work like this? Is it a normal behavior?
Thanks.

The problem here is that the activation list with your trigger conditions is connected with OR. They do have a ticket to provide multiple activation triggers, but it's still open.
That means, that it matches your sdk rule which is true and therefore active.
<profile>
<id>profile-1</id>
<activation> <!-- true || true = true -->
<jdk>1.6</jdk> <!-- true -->
<property> <!-- true -->
<name>name</name>
<value>Hubert</value>
</property>
</activation>
</profile>
<profile>
<id>profile-2</id>
<activation> <!-- true || false = true -->
<jdk>1.6</jdk> <!-- true -->
<property> <!-- false -->
<name>name</name>
<value>Wiktoria</value>
</property>
</activation>
</profile>

NOTE: This is only a suplement to Chasmo's correct answer.
There is a book from sonatype describing Maven. In section Sonatype book (section 5.3.1) we can find:
A profile is activated when all activation criteria has been satisfied.
This is not true. Truth is that one condition is enought to activate profile, which is of course equal to OR logical condition. This behavior is described in Maven docs:
Activation occurs when one or more of the specified criteria have been met. When the first positive result is encountered, processing stops and the profile is marked as active.
This is for me neither intiutive nor very useful. But thats how maven works at the time of writing that.
There is a ticket MNG-4565 for AND conjunction. This is marked as Bug, but acording to Maven doc it is not, so this ticket has been opened for almost 4 years.
The most useful part is last comment to this ticket written by Ronny Pscheidl. His comment points to this source: and-activation-profile-selector. This changes default maven OR condition to AND condition. Tested. Works. But of course if you decide to use this, you have one more thing to remember of.

Related

Disable maven build for particular project

Is it possible to disable settings.xml for particular project build using pom.xml?
I want to avoid using repositories specified in settings.xml file but only for one project.
Repositories are defined inside profiles in settings.xml file. You can activate and deactivate profiles based on some criteria. See documentation regarding profile activation.
You could:
deactivate given profile per project by passing -P !profile-name
you could define the profile as
<activation>
<property>
<name>skipThisProfile</name>
<value>!true</value>
</property>
</activation>
and then run your project with -DskipThisProfile
define the profile as
<activation>
<file>
<missing>.doNotRunProfile</missing>
</file>
</activation>
and will not run for any project where there is .doNotRunProfile file

Why use -D instead of -P to activate a maven profile?

e.g.
<profiles>
<profile>
<id>rofileX</id>
</profile>
</profiles>
The profile could have been activated using -ProfileX, why am I seeing people bother to add in an activation property, e.g.
<profiles>
<profile>
<id>rofileX</id>
<activation>
<property>
<name>oX</name>
</property>
</activation>
</profile>
</profiles>
to allow activation using -DoX?
Is there any reason to use -D insteal of -P? Or you can achieve more things with -D as oppose to -P?
Thank you
One usage of -Dmyproperty instead of -Pmyprofile is to activate several profiles using only one parameter.
Moreover, you can reuse the -Dmyproperty in other parts of your POM, using ${myproperty}.
The property does not necessarily has to be set via the -D maven option, it may also be a system property. So it is possible to switch between profiles without changing the invocation.
[edit]
from theMaven documentation:
-D, --define <arg>
Defines a system property
...
Properties defined on the command line are also available as properties to be used in a Maven POM or Maven Plugin

Configure Java EE 6 for dev/QA/prod

I have a Java EE 6 app that I build with Maven, code in NetBeans 7 and deploy on GlassFish 3.1.2. As I near completion, I find myself deploying demo builds.
The problem is that I don't have any dead easy way to build for different environment such as dev, QA, demo, prod, etc. For some stuff, I've been using a Java class with a bunch of static getters that return values based on the value of an environment constant. But this doesn't help me with conditionally setting
javax.faces.PROJECT_STAGE (web.xml)
database credentials (glassfish-resources.xml)
mail servers (glassfish-resources.xml)
JPA logging level (persistence.xml)
and probably a number of other things I can't think about now that are scattered across XML files.
Is there any way to define multiple versions of these configuration files and just set a flag at build time to select the environment, while defaulting to dev when no environment is specified? Is there a way I could make Maven work for me in this instance?
You can use maven to achieve that. Especially using resource filtering.
First, you can define list of profiles:
<profiles>
<profile>
<id>dev</id>
<properties>
<env>development</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault> <!-- use dev profile by default -->
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<env>production</env>
</properties>
</profile>
</profiles>
Then the resources that you need to filter:
<build>
<outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes</outputDirectory>
<filters>
<filter>src/main/filters/filter-${env}.properties</filter> <!-- ${env} default to "development" -->
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
And then your custom properties based on profiles in src/main/filters directory:
filter-development.properties
# profile for developer
db.driver=org.hsqldb.jdbcDriver
db.url=jdbc:hsqldb:mem:web
and
filter-production.properties
# profile for production
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/web?createDatabaseIfNotExist=true
to use production profile, you can package war using mvn clean package -Pprod command.
Here you can see the sample project that use profile in maven.
This is not direct response to question. This explain diff strategy to manage env properties
One other way to manage properties for diff env is using the database to store the properties. This way you have only need to manage the config of the DB. Based on which DB you are pointing you can load the properties from that DB. If you are using spring than spring provides PropertyPlaceholderConfigurer which can initialize the properties from DB. This approach allows you to change the property value without doing a build.
This approach is useful if you want to promote the artifact tested by QA\Testing team. In this case DB configuration will not be part of artifact generated by build process.
If you need to configure web.xml check this how-to:
https://community.jboss.org/docs/DOC-19076
It uses same method (resource filtering) as described in another answers.

Maven/Netbeans Have different testing profiles and environment variables

We are trying to do Selenium tests for our java web application. We would like to be able to quickly configure our tests with a combination of the java function "System.getProperty" and modifying the profiles available for the test.
The reason for doing this is so that we can test different servers with a simple change of the dropdown box in netbeans (from < default config > to "server name/details"). Our servers run things like a Snapshot, RC, and hotfix branch so this would be very helpful in tracking down when bugs are introduced.
Our current method for doing this is modifying the test variables before each run to target the server we want (yuck!).
Any thoughts would be helpful
The simplest mechanism might well be to create profiles in your POM that are activated by a system property or environment variable.
An example from the Maven documentation (with small modifications for clarity):
<profiles>
<profile>
<activation>
<property>
<name>myProperty</name>
<value>test</value>
</property>
</activation>
...
</profile>
</profiles>
Which you'd activate manually with:
mvn groupId:artifactId:goal -DmyProperty=test
You'd enable this based on an environment variable by using the fact that Maven maps environment vars into properties with the name env.… (with case normalization on Windows):
<profiles>
<profile>
<activation>
<property>
<name>env.ENVIRONMENT</name>
<value>test</value>
</property>
</activation>
...
</profile>
</profiles>
Which you'd activate with:
export ENVIRONMENT=test # Does not need to be done every build, just per session
mvn groupId:artifactId:goal

Can I make one maven profile activate another?

I have 2 maven2 profiles, selenium and jspc. Now for "selenium" id'd like to have an implicit activation of "jspc", so that I don't have to write mvn -Pselenium,jspc from the command line. Is this possible ?
You can't "chain" profile activations (maven reference) but you can activate them both through the same property:
<activation>
<property>
<name>profile.selenium</name>
</property>
</activation>
And the run mvn -Dprofile.selenium

Categories

Resources