2011-07-27 52 views
17

我正在使用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编译”失败,因为执法者的插件我设置需要我定义“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> 
+0

目前尚不清楚你想要什么。你想只提供'env'属性的默认值吗? – bmargulies

+0

是的,我想为env属性提供默认值。 – Dave

+1

请发布您的执行者配置。实际上,您没有定义'env'属性。如果这样的属性恰好存在并具有一定的价值,您只需设置一个配置文件即可运行。您可以使用其他值将默认env属性添加到properties元素。 – bmargulies

回答

0

什么我想你想要的是http://maven.apache.org/pom.html#Activation

+0

嗨,我编辑我的问题,包括我试图(基本上添加“ true”),但我的构建仍然失败,如果我离开“-P”指令(因为执行插件不找到“env”属性)。如果您可以提供更多信息和示例,则可以使用 - Dave – Dave

+1

@Dave您可以键入a:mvn help:active-profiles查看默认情况下哪些配置文件处于活动状态。 – Stephane

41

我知道那是很久以前的文件,但我现在只是有这个问题,所以......你应该做的:

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

另请注意:如果通过其他激活规则或-P命令行更积极地选择了任何其他配置文件,则不会选择activeByDefault配置文件。 –