41

Maven's Surefire (testing) pluginmvn test-compile copies files in src/test/resources to target/test-classes. It compiles .java in src/test/java, and copies the compiled .class files to target/test-classes.

But it doesn't copy resources from src/test/java, and it's more convenient to be able to put test resources in the same directory as the .java classes they are resources for, than in a parallel hierarchy in src/test/resources.

Is it possible to get Maven to copy resources from src/test/java?

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
tpdi
  • 34,554
  • 11
  • 80
  • 120
  • 4
    You can configure Maven to do that, but it is in general "more convenient" to follow the conventions and put the resource files where Maven thinks they should be. – Thilo Nov 19 '10 at 02:03
  • But the unitils convention is to put the dbunit xml files in the same directory as the test that uses them. And frankly, that's going to be easier for my team; the whole point of this is to reduce the pain of writing dbunit tests, which unitils does admirably. – tpdi Nov 19 '10 at 04:40
  • 1
    It's not "_Maven's Surefire (testing) plugin_" that does all this but `mvn test-compile` with `maven-resources-plugin:testResources` bound to the `process-test-resources` phase and `maven-compiler-plugin:testCompile` bound to the `test-compile` phase. I adapted the question acordingly. – Gerold Broser Jan 24 '19 at 11:01

5 Answers5

48

bmargulies gave the answer, but let me fill in some details.

<testresources> can be added to the <build> node of the project's POM, like this:

 <testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
  </testResource>
 </testResources>

That copies everything in src/test/java -- including the .java source code, which we don't want.

It also (as bmargulies only hinted at) overrides and replaces the default <testResources> setting in the standard parent POM that all other POM inherit from (unless that inheritance is changed). The standard parent copies src/test/resources, so by overriding that, we don't get that copied as usual, which we don't want. (In particular, my whole reason for doing this is to use unitils, which wants the unitils.properties file copied -- and that's (for me, anyway) in src/test/resources.

So we re-add src/test/resources:

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

That copies in the order listed, so that for files that exist in both /src/test/java (and subdirectories) and in /src/test/resources (and subdirectories), the src/test/resources version is the one that ends up in test-classes.

Now we just need to not copy the .java files:

<testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
    <excludes>
        <exclude>**/*.java</exclude>
    </excludes>
  </testResource>
  <testResource>
    <directory>${project.basedir}/src/test/resources</directory>
  </testResource>
</testResources>
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
assylias
  • 321,522
  • 82
  • 660
  • 783
20

The resource copying is all done by the maven-resource-plugin, and if you read the doc thereof you will see how to add copying of resources from src/test/java.

See http://maven.apache.org/plugins/maven-resources-plugin/testResources-mojo.html for the test-resources goal, which is included in the default lifecycle.

And then see http://maven.apache.org/pom.html, and look for <testResources>.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • 1
    Thanks. But I'm still a bit unclear. I see that I can include a tag in , but to copy *test* resources, I need to add something to plugins? – tpdi Nov 19 '10 at 01:39
3

The only way it worked for me when I put my test configuration into src/test/resources folder (analogue of src/test/java for source files). Files from this folder is copied to the target/test-classes folder which is on the classpath during the tests execution. I don't know why, but the next config didn't work for me:

<testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
    <excludes>
        <exclude>**/*.java</exclude>
    </excludes>
  </testResource>
  <testResource>
    <directory>${project.basedir}/src/test/resources</directory>
  </testResource>
</testResources>
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Alex Barysevich
  • 544
  • 1
  • 7
  • 18
0

Is it possible to get maven to copy resources from src/test/java?

Yes, this is easily achieved with the antrun plugin.

javamonkey79
  • 17,443
  • 36
  • 114
  • 172
0

I ran into the same issue. None of the above fixed it for me. My Pom had the following:

<build>
  <outputDirectory>${basedir}/build</outputDirectory>
  <directory>${basedir}/dist</directory>
  ...

I had to remove both, and then the plugin worked. Hope this helps. Apparently the pluggin does NOT like when you change the output directory.

wrgoff
  • 1
  • 1