2010-07-12 78 views
44

有沒有一種方法(我的意思是我如何)在maven項目中設置系統屬性?指定系統屬性爲Maven項目

我想從我的測試和我的webapp(本地運行)訪問屬性,我知道我可以使用java系統屬性。

我應該把它放在./settings.xml或類似的東西嗎?

語境

我把一個開源項目,並設法改變數據庫配置使用JavaDB之外現在

,在JavaDB之外的JDBC URL,數據庫的位置可能被指定爲完整路徑(見:this other question

或系統屬性:derby.system.home

我的代碼已經在工作,但目前它是所有硬編碼到:

jdbc:derby:/Users/oscarreyes/javadbs/mydb 

而且我想刪除的完整路徑,離開它喜歡:

jdbc:derby:mydb 

要做到這一點我需要指定系統屬性(derby.system.home)到Maven,但我不不知道如何。

測試是使用junit(我在pom.xml中看不到任何插件)執行的,並且web應用程序與jetty插件一起運行。

指定在命令行的系統屬性似乎對碼頭工作,但我不知道這是一件實事(授予其他一些用戶可能會從Eclipse中運行它/主意/其他)

+0

我有完全相同的問題,當我提出我的測試,集成測試(只是給他們改名,並加入Maven的故障安全的插件。該系統屬性停止工作。我也不得不從Maven的surefire-移動systemPropertyVariables插件插入到這個maven-failsafe-plugin中。在maven-surefire-plugin中配置的系統屬性在集成測試中被忽略。 – Alexandr 2016-08-26 13:24:47

回答

51

有沒有一種方法(我的意思是如何)在Maven項目中設置系統屬性?我想從我的測試訪問屬性[...]

您可以設置在Maven Surefire Plugin配置系統屬性(這是有道理的,因爲測試是由默認的分支)。從Using System Properties

<project> 
    [...] 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.5</version> 
     <configuration> 
      <systemPropertyVariables> 
      <propertyName>propertyValue</propertyName> 
      <buildDirectory>${project.build.directory}</buildDirectory> 
      [...] 
      </systemPropertyVariables> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
    [...] 
</project> 

和我的web應用程序(本地運行)

不知道你的意思,但在這裏我假設web應用程序容器由Maven的開始。您可以使用命令行傳遞系統屬性:

mvn -DargLine="-DpropertyName=propertyValue" 

更新:好,我知道了。對於Jetty,您還應該能夠在Maven Jetty插件配置中設置系統屬性。從Setting System Properties

<project> 
    ... 
    <plugins> 
    ... 
     <plugin> 
     <groupId>org.mortbay.jetty</groupId> 
     <artifactId>maven-jetty-plugin</artifactId> 
     <configuration> 
     ... 
     <systemProperties> 
      <systemProperty> 
       <name>propertyName</name> 
       <value>propertyValue</value> 
      </systemProperty> 
      ... 
     </systemProperties> 
     </configuration> 
     </plugin> 
    </plugins> 
</project> 
+0

我可以在命令行中指定,但是我不知道這是否是最好的方法,我會更新我的原始問題。同時+1 – OscarRyz 2010-07-12 20:40:19

+1

@Oscar我現在看到了。正如我寫的,JUnit測試運行在分叉虛擬機中,所以你必須在sur中設置系統屬性efire配置。 Maven Jetty Plugin提供了類似的配置工具。我建議配置兩者,這確實比命令行更好。 – 2010-07-12 21:05:00

+0

我以爲每個人都可以像我一樣閱讀頭腦......(順便說一句,不要這樣想)!!!這很好。謝謝 !! – OscarRyz 2010-07-12 21:34:16

2

如果測試和webapp在同一個Maven項目中,您可以在項目POM中使用一個屬性。然後你可以過濾某些文件,這將允許Maven在這些文件中設置屬性。有不同的方法來過濾,但最常見的是在資源階段 - http://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-description.html

如果測試和webapp在不同的Maven項目中,你可以把屬性放在settings.xml中,它位於你的maven倉庫文件夾中(C:\ Documents and Settings \ username.m2)。您仍然需要使用過濾或其他方法將屬性讀入您的測試和Web應用程序。

+2

我實際上需要它作爲系統屬性,所以我可以執行'System.getProperty(「myproperty」);' – OscarRyz 2010-07-12 20:15:33

45

properties-maven-plugin插件可以幫助:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>properties-maven-plugin</artifactId> 
    <version>1.0.0</version> 
    <executions> 
     <execution> 
      <goals> 
       <goal>set-system-properties</goal> 
      </goals> 
      <configuration> 
       <properties> 
        <property> 
         <name>my.property.name</name> 
         <value>my.property.value</value> 
        </property> 
       </properties> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 
+4

我不明白爲什麼maven-surefire-plugin被提及的是與測試無關的事情。看起來它與正常的開發/部署完全分離,我不理解這種混合。 – jjpe 2017-05-26 17:45:28

7

我已經學會了,還可以,如果你正在做一個「獨立」的Java應用程序使用exec-Maven的插件來做到這一點。

  <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>exec-maven-plugin</artifactId> 
      <version>${maven.exec.plugin.version}</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>java</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <mainClass>${exec.main-class}</mainClass> 
       <systemProperties> 
        <systemProperty> 
         <key>myproperty</key> 
         <value>myvalue</value> 
        </systemProperty> 
       </systemProperties> 
      </configuration> 
     </plugin> 
相關問題