Activating a profile by default

2020-06-08 13:30发布

问题:

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>

回答1:

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>


回答2:

The documentation for what I think you want is http://maven.apache.org/pom.html#Activation



标签: maven-2 maven