【发布时间】:2011-07-27 18:39:07
【问题描述】:
我正在使用 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”失败,因为我设置的强制插件要求我定义一个“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>
【问题讨论】:
-
不清楚你想要什么。您只想为“env”属性提供默认值吗?
-
是的,我想为 env 属性提供一个默认值。
-
请发布您的执行器配置。实际上,您还没有定义“env”属性。如果这样的属性碰巧存在并且具有特定值,您只需设置要运行的配置文件。您可以使用其他值将默认 env 属性添加到 properties 元素。
-
嗨,我添加了我的执行器配置。不过,我不明白你的其余评论。如果上述方法不可行,我如何默认激活配置文件?
-
你为什么要使用enforcer?只需将其设置为“activeByDefault”并带走执行者。