views:

72

answers:

1

I'm using Spring 2.5.6 and building my project with Maven 2.2.1. We use PropertyPlaceholderConfigurer beans in Spring to load up properties for configuring things like the database. Pretty standard stuff. We also have two different sets of tests: unit tests and integration tests.

I would like to be able to use different property files to configure things like database url differently for the two different types of tests. For example, I want unit tests to use the localhost database and integration tests to use the mydatabase.example.com database.

I have tried several variations where I place the property files in separate subdirectories (one for unit tests and one for integration tests). From there, I've tried things like using the additionalClasspathElements tag for the maven-failsafe-plugin, but that didn't seem to work at all. I tried using the maven-antrun-plugin to copy the files into target/classes, but that didn't get triggered when I ran mvn verify -Dtest=sometest.

I also tried using systemPropertyVariables in maven to set a property called buildEnvironment, which I then tried to reference in my Spring bean definition:

<property name="locations">
  <value>classpath:${buildEnvironment}/my-test.properties</value>
</property>

But Spring refused to resolve ${buildEnvironment}. At this point I'm out of ideas. I'm sure there's a nice, straightforward way to do this, but I can't figure it out.

Any advice would be greatly appreciated.

+1  A: 

You could enable resource filtering and create maven properties:

<build>
  <resources>
    <resource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

<properties>
   <buildEnvironment>yourValue</buildEnvironment>
</properties>

${buildEnvironment} in your Spring config will then be resolved to "yourValue" (assuming it is under src/test/resources/)

Wilhelm Kleu
This kind of works, although you have to use <testResources> instead of <resources> if you want to work for the test phases.Unfortunately, there doesn't seem to be a way to set the property differently for integration tests vs unit tests without using profiles. That's acceptable, but not really what I was hoping for. :)
organicveggie