Gradle does not copy test resources to build
gradle, java, resources
Solution
Copying of test resources is done by the processTestResources task from the Java plugin. The Java plugin also specifies this as a prerequisite of the test task, so that it happens before the tests are run.
You have told gradle that your fillDb task is also a prerequisite of the test task BUT you have not told it that processTestResources is a prerequisite of your createDb task.
So you could do something like
task createDb (type:Exec, dependsOn: 'processTestResources') {
...
BTW test resources are things that are made available to the tests via the classpath. I assume your tests don't need actually your sql available on the classpath at runtime, so do they even need to be there?
Problem
I have a multi project gradle project (spring web), with the following layout: ``` springweb | build.gradle | settings.gradle |_____ services | build.gradle | src/main/java | src/main/resources | src/test/java | src/test/resources | create-data.sql |_____ web | build.gradle | src/main/java | src/main/resources | src/main/webapp | src/test/java | src/test/resources ``` When I try to execute the contents under src/test/resources (init.sql) ``` commandLine 'psql', '-h', 'localhost', '-d', 'junit', '--username', 'junit', '-f' , sourceSets['test'].output.resourcesDir.toString() + '/create-data.sql' ``` it failse because the resource is not available in the build directory: ``` /Users/me/dev/springweb/services/build/resources/test/create-data.sql: No such file or directory ``` below my root build.gradle: ``` ext { springSecurityGroup = 'org.springframework.security' springSecurityVersion = '3.1.4.RELEASE' springGroup = 'org.springframework' springVersion = '3.2.5.RELEASE' hibernateGroup = 'org.hibernate' hibernateVersion = '4.2.7.SP1' } subprojects { apply plugin: 'java' apply plugin: 'eclipse' sourceSets.all{ println output.classesDir } repositories { mavenCentral() } } ``` and the 'services' build.gradle: ``` task createDb (type:Exec) { println sourceSets['test'].output.resourcesDir.toString() commandLine 'psql', '-h', 'localhost', '-d', 'junit', '--username', 'junit', '-f' ,'/Users/me/dev/sip/sip/sip04/src/main/resources/create-table.sql' } task fillDb (type:Exec, dependsOn: 'createDb') { commandLine 'psql', '-h', 'localhost', '-d', 'junit', '--username', 'junit', '-f' , sourceSets['test'].output.resourcesDir.toString() + '/create-data.sql' } test.dependsOn fillDb dependencies { /* zipped */ } /* Change context path (base url). otherwise defaults to name of project */ ``` I execute it with gradle build: ``` gradle build /Users/me/dev/springweb/services/build/classes/main /Users/me/dev/springweb/services/build/classes/test /Users/me/dev/springweb/web/build/classes/main /Users/me/dev/springweb/web/build/classes/test /Users/me/dev/springweb/services/build/resources/test :services:compileJava UP-TO-DATE :services:processResources UP-TO-DATE :services:classes UP-TO-DATE :services:jar UP-TO-DATE :services:assemble UP-TO-DATE :services:createDb DROP TABLE DROP SEQUENCE zipped :services:fillDb/Users/me/dev/springweb/services/build/resources/test/create-data.sql: No such file or directory ``` From the output I learn that the sourceSets are as I expect, but that the test resources are not copied. What is wrong?