默认情况下激活配置文件

时间:2011-07-27 18:39:07

标签: maven-2 maven

我正在使用Maven 3.0.3。我需要在我的脚本中定义一个变量(“env”)。我的pom中有一个<profiles>部分,我在其中定义了每个<profile>元素的变量...

<profile>
  <id>qa</id>
  <properties>
    <env>qa</env>
    ...
  </properties>
</profile>

在我的pom.xml中,如果没有通过“-P”命令行选项指定任何配置文件,如何激活配置文件(因此如果未定义变量,则设置该变量)?我尝试了以下,

<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>

但运行命令“mvn compile”失败,因为我设置的enforcer插件要求我定义一个“env”变量。这是我为执行器插件运行的代码......

<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>

2 个答案:

答案 0 :(得分:42)

我知道这是很久以前的事了,但我刚才遇到了这个问题,所以......你应该这样做:

<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>

答案 1 :(得分:0)

我认为你想要的文件是http://maven.apache.org/pom.html#Activation

相关问题