55

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

60

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

2
  • 1
    This worked for me. +1 for pointing out the need for the default. Mar 13, 2015 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 <build><testResources>... 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). Jan 20, 2016 at 5: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>
2
  • 7
    It doesn't matter functionally, but for orderliness, shouldn't the phase be generate-test-resources instead of generate-test-sources?
    – thSoft
    Mar 13, 2013 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 6, 2019 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>
1
  • 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, 2013 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.

2
  • 1
    One has only one such folder, but often more than one test environment.
    – 30thh
    Jun 22, 2011 at 9: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. Jun 20, 2019 at 11:41
5

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.