指定Maven项目的系统属性 [英] Specify system property to Maven project
问题描述
是否有一种方法(我是说我该如何设置)在maven项目中?
Is there a way ( I mean how do I ) set a system property in a maven project?
我想从我的测试和我的webapp(在本地运行)中访问一个属性,并且我知道我可以使用java系统属性.
I want to access a property from my test and my webapp ( running locally ) and I know I can use a java system property.
我应该将其放在./settings.xml或类似的文件中吗?
Should I put that in ./settings.xml or something like that?
上下文
我参加了一个开源项目,并设法将数据库配置更改为使用JavaDB
I took an open source project and managed to change the db configuration to use JavaDB
现在,在JavaDB的jdbc URL中,可以将数据库的位置指定为完整路径(请参阅:
Now, in the jdbc url for JavaDB, the location of the database could be specified as the full path ( see: this other question )
或系统属性:derby.system.home
我的代码已经在工作,但是目前所有代码都被硬编码为:
I have the code working already, but currently it is all hardcoded to:
jdbc:derby:/Users/oscarreyes/javadbs/mydb
我想删除完整路径,使其保留为:
And I want to remove the full path, to leave it like:
jdbc:derby:mydb
为此,我需要指定系统属性(derby.system.home
)进行Maven操作,但是我不知道如何操作.
To do that I need to specify the system property ( derby.system.home
) to maven, but I don't know how.
该测试是使用junit进行的(我在pom.xml中看不到任何插件),并且该Web应用程序使用jetty插件运行.
The test are performed using junit ( I don't see any plugin in pom.xml ) and the web app runs with the jetty plugin.
在命令行中指定system属性似乎可以解决码头问题,但是我不确定这是否可行(允许其他用户通过eclipse/idea/来运行它)
Specifying the system property in the command line seems to work for jetty, but I'm not sure if that's something practical ( granted some other users may run it from eclipse/idea/ whatever )
推荐答案
是否有一种方法(我的意思是我)在Maven项目中设置系统属性?我想从测试中访问属性[...]
Is there a way ( I mean how do I ) set a system property in a maven project? I want to access a property from my test [...]
您可以在 Maven Surefire插件配置中设置系统属性(此很有意义,因为默认情况下测试是分叉的).来自使用系统属性:
You can set system properties in the Maven Surefire Plugin configuration (this makes sense since tests are forked by default). From 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>
和我的网络应用程序(在本地运行)
and my webapp ( running locally )
不知道您的意思是什么,但是我假设webapp容器是由Maven启动的.您可以使用以下命令在命令行上传递系统属性:
Not sure what you mean here but I'll assume the webapp container is started by Maven. You can pass system properties on the command line using:
mvn -DargLine="-DpropertyName=propertyValue"
更新:好的,现在就知道了.对于Jetty,您还应该能够在Maven Jetty插件配置中设置系统属性.来自设置系统属性:
Update: Ok, got it now. For Jetty, you should also be able to set system properties in the Maven Jetty Plugin configuration. From 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>
这篇关于指定Maven项目的系统属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!