默认情况下激活配置文件 [英] Activating a profile by default

查看:89
本文介绍了默认情况下激活配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

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

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>

但是运行命令"mvn compile"失败,因为我设置的强制实施器插件要求我定义"env"变量.这是我为执行程序插件运行的代码...

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>

推荐答案

我知道已经很久了,但是我现在遇到了这个问题,所以...您应该这样做:

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>

这篇关于默认情况下激活配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆