I have a Spring-JPA project in Gradle where the main/java/resources folder 
contains orm.xml, persistence.xml, and persistenceContext.xml (Spring config 
file) as you might expect. However, I have a test version of persistence.xml in 
main/test/resources as well as a test version of the Spring config file called 
persistenceContext-test.xml.

When I build the project, I want what is found in main/java/resources to be 
packaged in the jar obviously.

But when I run the tests, I want orm.xml in the classpath along with the test 
versions of the other files. Peter said the default behavior is for main 
resources files to be in the test classpath, but I would like to override that 
behavior for the java version of persistence.xml and the persistenceContext.xml 
file.

I thought this would be pretty easy, but I have had more trouble than I 
expected. I tried a processTestResources closure, but that seems inappropriate 
since I'm not copying anything anywhere. So here is my test closure:

test {
  mainTree = fileTree {
    from sourceSets.main.classes
    exclude "**/persistence.xml"
    exclude "**/persistenceContext.xml"
  }
  testTree = fileTree {
    from sourceSets.test.classes
    exclude "**/*.sql"                                                     
//These excludes are irrelevant to the question
    exclude "**/suites/**"                                             //These 
excludes are irrelevant to the question
  }
  runtimeClasspath = mainTree + testTree
}

When I run my test, I find my classpath is unchanged: Using application 
classpath [C:\myapp\persistence\build\classes\test, 
C:\myapp\persistence\build\classes\main, ...and all my jars]

And my test fails.

I notice something curious in the logs:

17:12:54.265 [INFO] 
[org.gradle.api.internal.changedetection.DefaultTaskArtifactStateRepository] 
Executing task ':persistence:processTestResources' due to:
Output file C:\myapp\persistence\build\classes\test for task 
':persistence:processTestResources' has changed.
Output file C:\myapp\persistence\build\classes\test\persistenceContext-test.xml 
has been removed for task ':persistence:processTestResources'.
Output file C:\myapp\persistence\build\classes\test\META-INF\persistence.xml 
has been removed for task ':persistence:processTestResources'.

These files are precisely what I want to keep; it is their equivalents in the 
main tree that I want to eliminate from testing.

Sorry for the long message, but I appreciate any insight.

Thanks.



Reply via email to