有没有办法(我的意思是如何)在 Maven 项目中设置系统属性?
我想从我的测试和我的 web 应用程序(本地运行)访问属性,并且我知道我可以使用 java 系统属性。
我应该把它放在 ./settings.xml 或类似的东西中吗?
背景
我采用了一个开源项目并设法更改数据库配置以使用 JavaDB
现在,在 JavaDB 的 jdbc url 中,可以将数据库的位置指定为完整路径(请参阅:另一个问题)
或者系统属性:
derby.system.home
我的代码已经可以运行,但目前它全部硬编码为:
jdbc:derby:/Users/oscarreyes/javadbs/mydb
我想删除完整路径,将其保留为:
jdbc:derby:mydb
为此,我需要为 Maven 指定系统属性(
derby.system.home
),但我不知道如何做。
测试是使用 junit 执行的(我在 pom.xml 中没有看到任何插件),并且 Web 应用程序使用 jetty 插件运行。
在命令行中指定系统属性似乎适用于 jetty,但我不确定这是否实用(授予其他一些用户可以从 eclipse/idea/what 运行它)
有没有办法(我的意思是如何)在 Maven 项目中设置系统属性?我想从我的测试中访问一个属性 [...]
您可以在 Maven Surefire Plugin 配置中设置系统属性(这是有道理的,因为测试默认是分叉的)。来自使用系统属性:
<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>
和我的网络应用程序(本地运行)
不确定你的意思,但我假设 webapp 容器是由 Maven 启动的。您可以使用以下命令在命令行上传递系统属性:
mvn -DargLine="-DpropertyName=propertyValue"
更新:好的,现在明白了。对于 Jetty,您还应该能够在 Maven Jetty 插件配置中设置系统属性。来自设置系统属性:
<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>
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>
我了解到,如果你正在做一个“独立”的java应用程序,也可以使用exec-maven-plugin来做到这一点。
<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>
如果你的测试和webapp在同一个Maven项目中,你可以使用项目POM中的一个属性。然后您可以过滤某些文件,这将允许 Maven 在这些文件中设置属性。过滤的方法有多种,但最常见的是在资源阶段 - http://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-description.html
如果测试和 webapp 位于不同的 Maven 项目中,您可以将该属性放在 settings.xml 中,该属性位于 Windows 上的 Maven 存储库文件夹 (C:\Documents and Settings\username.m2) 中。您仍然需要使用过滤或其他一些方法将属性读入您的测试和 Web 应用程序。
我只是想提供解决我的问题的方法。
问题: 我想在运行
mvn spring-boot:run
时复制系统属性的传递,就像我们使用 java -DpropertyName=propertyValue -jar jarFileName.jar
运行 jar 文件时一样
解决方案:首先添加这个插件
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>com.example.MainClass</mainClass>
</configuration>
</plugin>
</plugins>
</build>
接下来只需运行此命令
mvn exec:java -DpropertyName=propertyValue
的工作方式与
mvn spring-boot:run
相同