【问题标题】:System properties can't be resolved in Spring XML using Maven无法使用 Maven 在 Spring XML 中解析系统属性
【发布时间】:2009-10-28 15:16:17
【问题描述】:

我的应用程序需要知道可以存储其数据的目录的路径。我尝试设置 Java 系统属性并将该变量用作 Spring XML 中的占位符。

在 Eclipse 中,我将该属性添加到我的运行配置环境中,它工作得很好。 Spring 将 ${dataDir} 解析为正确的路径。

但是当我使用 Maven2 (mvn test -DdataDir=c:/data) 测试应用程序时,Spring 抱怨它无法解析占位符。

我的 Spring XML 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd">

 <!-- Allows me to use system properties in this file -->
 <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

 <bean id="xmlConfiguration" class="org.apache.commons.configuration.XMLConfiguration">
  <constructor-arg index="0">
   <value>${dataDir}/conf/config.xml</value>
  </constructor-arg>
 </bean>
</beans>

为什么不将该系统属性传递给 Spring?我究竟做错了什么?感谢您的任何建议!

编辑:当然,你是对的:${baseDir} 应该是 ${dataDir}。但这只是这个问题的一个错字,而不是真实的代码。

我之前尝试过MAVEN_OPTS,但也没有用...

【问题讨论】:

  • 应该是baseDir,而不是dataDir

标签: java spring maven-2 system-properties


【解决方案1】:

这是 Surefire 插件 2.4.3 中的一个已知错误。有关详细信息,请参阅 JIRA 问题“System properties set on the command line get clobbered”。改用以前的版本 2.4.2:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <!-- Use 2.4.2 because 2.4.3 has bug with system properties
           see http://jira.codehaus.org/browse/SUREFIRE-121 -->
      <version>2.4.2</version>
    </plugin>
  </plugins>
</build>

【讨论】:

    【解决方案2】:

    也许 mvn test 没有通过它运行到测试的系统属性?是否可以使用测试插件传递属性(它本身可能会从系统属性中提取它们)?

    另请参阅:http://maven.apache.org/maven-1.x/plugins/test/properties.html

    【讨论】:

    • 这是最好的解决方案 - 在您的 POM 文件中嵌入任何所需的属性。通过这种方式,它们与项目一起存储,以便所有团队成员都可以访问它们(并且不必单独设置他们的环境),因此它们是受版本控制的。
    【解决方案3】:

    如果您查看mvn 脚本,您可以看到通过命令行传递的所有参数都作为参数传递给启动类,而不是作为JVM 的参数。所以-D 不起作用。

    最快的解决方法是定义MAVEN_OPTS 环境变量,例如

    set MAVEN_OPTS="-DdataDir=c:/data"
    

    然后运行mvn test

    【讨论】:

    • 我认为这不再是真的。所选答案中链接的 JIRA 问题的最新补充说“通过 MAVEN_OPTS 直接传递到 JVM 的系统道具不会转发给测试。”
    【解决方案4】:

    也许你应该使用相同的变量名?我的意思是你传递 dataDir,但期望 baseDir。我错了吗?

    【讨论】:

      【解决方案5】:

      我认为 flicken 的 answer 是您问题的正确答案。

      但是,我建议将config.xml 文件移动到src/test/resources 中。这似乎是一个完美的位置(在 Maven 的世界中),这样,该文件将在类路径上可用(因此您不必使用绝对路径)。最重要的是,该文件将像项目的任何其他资源一样最终进入版本控制,这是一件好事。

      如果您真的想将其保留在项目结构之外,我会使用 resources filtering 过滤您的 Spring 配置文件并在构建时设置 ${dataDir}

      【讨论】:

      • 该文件应该存储在项目之外,因为当我想更新应用程序时不能覆盖它。但将其置于版本控制之下是一个好点。必须考虑一下
      猜你喜欢
      • 2014-12-11
      • 2019-09-14
      • 1970-01-01
      • 2013-03-17
      • 2023-04-10
      • 2021-10-08
      • 2015-03-18
      • 2016-08-17
      • 2021-09-16
      相关资源
      最近更新 更多