Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activating a profile by default

Tags:

maven-2

maven

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>
like image 275
Dave Avatar asked Jul 27 '11 18:07

Dave


People also ask

Why profile is used in Maven?

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.

What is the highest level of portability?

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.

What is goal and profile in Maven build?

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.


1 Answers

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>
like image 63
vhtc Avatar answered Jan 01 '23 05:01

vhtc