2010-07-12 53 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> 
相关问题