【问题标题】:Specify system property to Maven project为 Maven 项目指定系统属性
【发布时间】:2011-03-14 23:49:33
【问题描述】:

有没有办法(我的意思是如何)在 maven 项目中设置系统属性?

我想从我的测试和我的 web 应用程序(本地运行)中访问一个属性,并且我知道我可以使用 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/idea/whatever 运行它)

【问题讨论】:

  • 当我将测试移至集成测试时,我遇到了完全相同的问题(只是将它们重命名并添加了 maven-failsafe-plugin。此系统属性停止工作。我还不得不从 maven- 中移动 systemPropertyVariables surefire-plugin 到这个 maven-failsafe-plugin 中。在 maven-surefire-plugin 中配置的系统属性在集成测试中被忽略了。

标签: maven-2 system-properties


【解决方案1】:

如果您的测试和 webapp 在同一个 Maven 项目中,您可以使用项目 POM 中的属性。然后您可以过滤某些文件,这些文件将允许 Maven 在这些文件中设置属性。有不同的过滤方式,但最常见的是在资源阶段 - http://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-description.html

如果 test 和 webapp 位于不同的 Maven 项目中,您可以将属性放在 settings.xml 中,该属性位于 Windows 上的 maven 存储库文件夹 (C:\Documents and Settings\username.m2) 中。您仍然需要使用过滤或其他方法将属性读入您的测试和 web 应用程序。

【讨论】:

  • 我实际上需要它作为系统属性,所以我可以执行System.getProperty("myproperty");
【解决方案2】:

有没有办法(我的意思是如何)在 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>

和我的 webapp(在本地运行)

不确定您的意思,但我假设 webapp 容器是由 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>

【讨论】:

  • 我可以在命令行上指定,但我真的不知道这是否是最好的方法。我会更新我原来的问题。同时+1
  • @Oscar 我现在看到了。正如我所写的,JUnit 测试在分叉的 VM 中运行,因此您确实必须在安全配置中设置系统属性。 Maven Jetty Plugin 提供了类似的配置工具。我建议同时配置两者,这确实比命令行更好。
  • 我以为每个人都可以像我一样读心...(顺便说一句,别再这么想了!!!)...效果很好。谢谢!!
  • 2019 注意:已折旧。使用 systemPropertyVariables 而不是 systemProperties maven.apache.org/plugins-archives/maven-surefire-plugin-2.12.4/…
【解决方案3】:

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>

【讨论】:

  • 我不明白为什么提到 maven-surefire-plugin 用于与测试无关的东西。看起来它与正常的开发/部署完全不同,我不明白这种混淆。
  • 大部分人认为maven只有测试目的。
【解决方案4】:

我了解到,如果您正在做一个“独立”的 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>

【讨论】:

    猜你喜欢
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多