54

The Junits I have in my project need to load property files from the classpath. How can I specify the directory of those property files so that Maven will set that in the classpath before running the tests?

6 Answers6

58

You can also add new test resource folders.

<build>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
        </testResource>
        <testResource>
            <directory>${project.basedir}/src/test/something_else</directory>
        </testResource>
    </testResources>
</build>

The first path, src/test/resources, is the default. Assuming you still want the default path to be used, make sure it's included. (The testResources tag overwrites your defaults, so if you don't include the default path explicitly, it will stop being used.)

Michael Bosworth
  • 2,123
  • 18
  • 8
  • 1
    This worked for me. +1 for pointing out the need for the default. – Darren Parker Mar 13 '15 at 20:52
  • 5
    That's exactly what I was looking for, thanks! Much simpler than the accepted `build-helper-maven-plugin` solution. For anyone else looking for a good way to do this, the `...` construct can also be added to your maven profiles, e.g. in case you want to specify different resources for different environments (local/dev/test/prod). – Amos M. Carpenter Jan 20 '16 at 05:56
43

You can use the build-helper-maven-plugin to specify additional test-resource directories as follows. Using the configuration below, the contents of the test-resources directory will be copied to the target/test-classes directory during the generate-test-sources phase:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.12</version>
  <executions>
    <execution>
      <id>add-test-resource</id>
      <phase>generate-test-sources</phase>
      <goals>
        <goal>add-test-resource</goal>
      </goals>
      <configuration>
        <resources>
          <resource>
            <directory>path/to/additional/test/resources</directory>
            <excludes>
              <exclude>**/folder-to-exclude/**</exclude>
            </excludes>
          </resource>
        </resources>
      </configuration>
    </execution> 
  </executions>
</plugin>
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Rich Seller
  • 83,208
  • 23
  • 172
  • 177
  • 6
    It doesn't matter functionally, but for orderliness, shouldn't the phase be generate-test-resources instead of generate-test-sources? – thSoft Mar 13 '13 at 13:01
  • You seem to have a typo in the `phase` s/add-test-sources/add-test-resources/ It would still work otherwise, but just more semantically correct. – farthVader Dec 06 '19 at 11:07
28

If you just want to put your property files someplace on disk and don't want to copy those property files to target/test-classes during the build, you can do it this way

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <additionalClasspathElements>
      <additionalClasspathElement>/add/this/to/path</additionalClasspathElement>
    </additionalClasspathElements>
  </configuration>
</plugin>
sal
  • 23,373
  • 15
  • 66
  • 85
  • 2
    Unfortunately, m2e does not pick up Surefire's configuration when running tests from Eclipse. Too bad because this is a bit simpler than build-helper-maven-plugin's add-test-resource goal. – thSoft Mar 13 '13 at 12:58
8

Why not just use test/resources and place your properties in the classpath from that point. They'll only be there for the test phase.

Mike Cornell
  • 5,909
  • 4
  • 29
  • 38
  • 1
    One has only one such folder, but often more than one test environment. – 30thh Jun 22 '11 at 09:23
  • If you have a multi-module maven project you may need a common test data, which will be available on a classpath in more than one modules. – Filip Majernik Jun 20 '19 at 11:41
4

If you have multiple resource environment you can use maven profile and put your various resources according to the profile you are testing.

test/resources/uat
test/resources/prod
test/resources/dev

But usualy if you need that you are making integration test then you don't need the build-helper-maven-plugin.

Dori
  • 915
  • 1
  • 12
  • 20
Dante
  • 324
  • 3
  • 7
3

The maven-resources-plugin has a copy-resources goal that will allow you to copy resources. For example:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
      <execution>
        <id>additional-resources</id>
        <phase>process-test-resources</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
          <resources>
            <resource>
              <directory>${project.basedir}/conf</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>

This will copy the contents of the conf folder in the base of your project to the target/test-classes folder (unless you modified project.build.testOutputDirectory) which will be added to the classpath during your unit tests.

Lucas
  • 14,227
  • 9
  • 74
  • 124