System properties can't be resolved in Spring XML using Maven

Olvagor picture Olvagor · Oct 28, 2009 · Viewed 7.2k times · Source

My application needs to know the path to a directory where it can store its data. I tried to set a Java system property and use that variable as a placeholder in my Spring XML.

In Eclipse I added that property to the environment of my run configuration and it works just fine. Spring resolves ${dataDir} to the correct path.

But when I test the application using Maven2 (mvn test -DdataDir=c:/data), Spring complains that it can't resolve the placeholder.

My Spring XML looks like this:

<?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>

Why isn't that system property passed to Spring? What am I doing wrong? Thanks for any suggestions!

EDIT: Of course, you're right: ${baseDir} should be ${dataDir}. But that was just a typo in this question, not in the real code.

I tried MAVEN_OPTS before but it doesn't work either...

Answer

flicken picture flicken · Oct 29, 2009

This is a known bug in the Surefire plugin 2.4.3. For details, see JIRA issue "System properties set on the command line get clobbered". Use the previous version, 2.4.2 instead:

<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>