【问题标题】:How to identify and set a missing environment property in Maven?如何在 Maven 中识别和设置缺少的环境属性?
【发布时间】:2011-11-07 22:09:34
【问题描述】:

我有我的构建设置,以便我通过命令行传递我的变量:

mvn clean install -DsomeVariable=data

在我的 pom 中我有:

<someTag>${someVariable}</someTag>

这很好用,但我想确定是否在命令行上未指定 someVariable,然后将其默认设置,以便我的脚本可以继续。

这可以在 Maven 中完成吗?

【问题讨论】:

    标签: maven environment-variables


    【解决方案1】:

    您可以在 POM 文件的 properties 部分中指定默认属性值:

    <properties>
      <someVariable>myVariable</someVariable>
    </properties>
    

    如果您想确保在命令行中始终提供属性值,则可以使用 maven-enforcer-plugin。

    这是一个显示如何强制系统属性存在的链接 -> http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html

    我将在此处逐字复制 XML,以防上面的链接出错。

    <project>
      [...]
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.0.1</version>
            <executions>
              <execution>
                <id>enforce-property</id>
                <goals>
                  <goal>enforce</goal>
                </goals>
                <configuration>
                  <rules>
                    <requireProperty>
                      <property>basedir</property>
                      <message>You must have a basedir!</message>
                      <regex>\d</regex>
                      <regexMessage>You must have a digit in your baseDir!</regexMessage>
                    </requireProperty>
                    <requireProperty>
                      <property>project.version</property>
                      <message>"Project version must be specified."</message>
                      <regex>(\d|-SNAPSHOT)$</regex>
                      <regexMessage>"Project version must end in a number or -SNAPSHOT."</regexMessage>
                    </requireProperty>
                  </rules>
                  <fail>true</fail>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      [...]
    </project>
    

    【讨论】:

    • 感谢 Alex 的深刻回应。虽然我希望默认属性而不是强制执行它。因此,如果未指定,则脚本将使用默认值。
    • @TERACytE。是的,properties 部分中的值是默认值,您可以随时从命令行覆盖它。
    【解决方案2】:

    您可以将默认值指定为

    <properties>
          <someTag>defaultValue</someTag>
    </properties>
    

    当你运行 maven 命令时,你可以像这样覆盖那个值

    mvn clean package -DsomeTag=newSpecificValue
    

    【讨论】:

    • 我需要在 Jenkins 构建中使用的简单解决方案
    【解决方案3】:

    您可以改用配置文件,但您需要为每个配置文件创建一个配置文件 变量。

     <profile>
        <id>default-value-1</id>
        <activation>
              <activeByDefault>false</activeByDefault>
              <property>
                 <name>!someVariable</name>
              </property>
        </activation>
        <properties>
            <someVariable>DEFAULT-VALUE</someVariable>
        </properties>
    </profile>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2012-12-11
      • 1970-01-01
      相关资源
      最近更新 更多