maven - 如何在 Maven 中识别和设置缺少的环境属性?

标签 maven environment-variables

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

mvn clean install -DsomeVariable=data

在我的 pom 中,我有:
<someTag>${someVariable}</someTag>

这工作正常,但我想确定 someVariable 是否未在命令行中指定,然后默认它以便我的脚本可以继续。

这可以在Maven中完成吗?

最佳答案

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

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

关于maven - 如何在 Maven 中识别和设置缺少的环境属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8043313/

相关文章:

php - $_SERVER ['_' ] 在 Windows 上等效

java - Eclipse 中的 Maven 项目

java - Maven 多模块 Spring MVC 应用程序

javascript - 调试代码时不要缩小 js minify-maven-plugin samaxes

python - 在 python 脚本中更改环境

bash - 尝试将 bash 变量从 .sh 文件导出到运行它的另一个文件

Java GRPC TLS 客户端无法执行

java - 保存用户从 Maven Clean 上传的文件

eclipse - 为整个项目设置环境变量

iphone - 如何通过命令行知道XCode的默认安装目录?