I'm using Maven 3.0.3. I need to define a variable in my script ("env"). I have a <profiles>
section in my pom in which I define the variable per <profile>
element ...
<profile>
<id>qa</id>
<properties>
<env>qa</env>
...
</properties>
</profile>
In my pom.xml, how do I activate a profile only if none was specified via the "-P" command line option (and hence set the variable if it was not defined)? I tried the below,
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<url>http://localhost:8080/manager</url>
<server>nnadbmon-dev-tomcat</server>
</properties>
</profile>
but running the command "mvn compile" fails because the enforcer plugin I set up requires that I define an "env" variable. Here's the code I run for my enforcer plugin ...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>env</property>
<message>Environment missing. This should be either dev, qa, or prod, specified as part of the profile (pass this as a parameter after -P).</message>
<regex>^(dev|qa|production)$</regex>
<regexMessage>Incorrect environment. Expecting one of dev, qa, or prod.</regexMessage>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
A profile in Maven is an alternative set of configuration values which set or override default values. Using a profile, you can customize a build for different environments. Profiles are configured in the pom. xml and are given an identifier.
Wide (Universal) Portability This is the highest level of portability; anything less requires extra work for those who wish to build your project. This level of portability is especially important for open source projects, which depend on the ability for would-be contributors to easily download and build from source.
A Build profile is a set of configuration values, which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments.
I know it was a long time ago, but I had this problem just now, so... You should do that:
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
<url>http://localhost:8080/manager</url>
<server>nnadbmon-dev-tomcat</server>
</properties>
</profile>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With