簡體   English   中英

如何使用系統屬性設置Maven POM文件以輸入Java中的參數?

[英]How to setup Maven POM file with System Properties to inpurt parameters in Java?

我試圖從我在Maven POM文件中設置的系統屬性中獲取輸入參數。

我的POM看起來像

<systemProperties>
   <property>
      <name>number 1</name>
      <value>${number 1}</value>
   </property>
   <property>
      <name>number 2</name>
      <value>${number 2}</value>
   </property>
</systemProperties>

當我進行測試時,這將是我的主要目標

clean install -Dtest=RunTest test -Dnumber1=2 -Dnumber1

現在如何編寫我的Java測試代碼以從系統屬性中獲取number1和number2?

public void addNumbers () {
    System.out.println(number1+number2);

}

**我需要在項目中使用surefire插件才能使其正常工作嗎?

如果您想將系統屬性傳遞給測試用例(由surefire插件調用),則需要配置surefire插件以傳遞諸如

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <systemPropertyVariables>
            <number1>1</number1>
            <number2>2</number2>

          </systemPropertyVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

在pom.xml中,我們可以這樣配置:

<project>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <argLine>-DaddProp=true</argLine>
                <argLine>-DmultProp=false</argLine>
            </configuration>
        </plugin>
    </plugins>
</build>

在Java代碼中獲取系統屬性:

System.getProperty("addProp");
System.getProperty("multProp");

暫無
暫無

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

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