【问题标题】:Activating a profile by default默认激活配置文件
【发布时间】: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”并带走执行者。

标签: maven-2 maven


【解决方案1】:

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

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

【讨论】:

  • 另请注意:如果通过其他激活规则或 -P 命令行更主动地选择了任何其他配置文件,则不会选择 activeByDefault 配置文件。
  • @GregDomjan 确实,在我的情况下,我添加了 &lt;activation&gt; &lt;property&gt; &lt;!-- Just pass -Dto-disable to disable the profile --&gt; &lt;name&gt;to-disable&lt;/name&gt; &lt;value&gt;!true&lt;/value&gt; &lt;/property&gt; &lt;/activation&gt; 然后当我运行 mvn help:active-profiles 时,我可以看到配置文件默认激活,即使我在命令行上传递其他配置文件选项-Pmyprofile
【解决方案2】:

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

【讨论】:

  • 嗨,我编辑了我的问题以包含我尝试过的内容(基本上添加了“true”),但如果我不使用“-P”指令,我的构建仍然会失败(因为执行器插件没有找到“env”属性)。如果您可以提供其他信息和示例,我们将不胜感激,- Dave
  • @Dave 您可以输入:mvn help:active-profiles 以查看默认情况下哪些配置文件处于活动状态。
猜你喜欢
  • 2019-05-17
  • 2017-02-26
  • 2021-02-01
  • 2021-09-11
  • 2011-11-12
  • 2014-10-29
  • 2011-07-17
  • 2012-06-15
  • 2016-05-20
相关资源
最近更新 更多