簡體   English   中英

如何識別和設置Maven中缺少的環境屬性?

[英]How to identify and set a missing environment property in Maven?

我有我的構建設置,以便通過命令行傳入我的變量:

mvn clean install -DsomeVariable=data

在我的pom我有:

<someTag>${someVariable}</someTag>

這工作正常,但我想確定是否在命令行上沒有指定someVariable,然后默認它以便我的腳本可以繼續。

這可以在Maven完成嗎?

您可以在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>

您可以將默認值指定為

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

運行maven命令時,可以像這樣覆蓋該值

mvn clean package -DsomeTag=newSpecificValue

您可以改用配置文件,但每個變量都需要一個配置文件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM