Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven (Surefire): copy test resources from src/test/java

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?

like image 427
tpdi Avatar asked Nov 19 '10 01:11

tpdi


People also ask

How do I add resources to SRC test?

Right click on maven project --->Click on Build Path ----->Click on New Source Folder. New source folder window will open, give the name to your folder example - src/test/source. click on Finish.

How do you get the path of SRC test resources directory in Junit?

File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file. getAbsolutePath(); System.

Where do you put test resources?

what is a resource? a resource is a file in the class path folder structure for your project. this is important because your test resources will be put in your test-classes folder hierarchy and your main resources will be put in your classes folder hierarchy — both in your target folder.

What is Maven resources plugin?

The Resources Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources.


2 Answers

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> 
like image 170
3 revs, 3 users 84% Avatar answered Oct 05 '22 23:10

3 revs, 3 users 84%


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

like image 32
bmargulies Avatar answered Oct 06 '22 01:10

bmargulies