How to set up a multi source gradle project


(Billy Buzzard) #1

This snippet shows my current project layout:

My “build.gradle” file contains the following:

apply plugin: 'java’
archivesBaseName = 'gradle-sample’
version = '1.0’
sourceCompatibility = JavaVersion.VERSION_1_6

def manifest = manifest {
attributes(
‘Implementation-Version’ : version,
‘Implementation-Title’ : ‘Gradle Sample’
)
}

jar.manifest.from manifest

repositories {
jcenter()
}

dependencies {
compile ‘org.slf4j:slf4j-api:1.7.21’

testCompile 'junit:junit:4.12'

}

sourceSets {
api
main {
compileClasspath = compileClasspath + files(api.output.classesDir)
}
}

classes.dependsOn apiClasses

I can run “classes”, but running “test” fails. What do I need to set in the build.gradle file to tell it that the “test” task needs to include the “messages.properties” file from “main” and the “ReadWelcomeMessage” interface from “api”? I assume it has something to do with the way the test sourceSet is initialized, but I don’t what what it would look like.


(Stefan Oehme) #2

The test sourceSet’s compileClasspath and runtimeClasspath need to be updated just like you did for the main one.

This can be simplified to

compileClasspath = compileClasspath + api.output

The output object knows which tasks need to be run to build it. You can now remove classes.dependsOn apiClasses.


(Billy Buzzard) #3

Thank you for that information. I was able to successfully run “classes” before, but your information made the build file simplier. But the “compileTestJava” still fails, What do I need to change so the test code can find the ReadWelcomeMessasge interface in the API sourceSet?


(Billy Buzzard) #4

Thank you again Stefan. One last problem. When I run “gradle test” I’m getting a MissingResourceException saying it “Can’t find bundle for base name message, locale es_US”. I have tried a few sourceSet properties, but I have not found the correct combination to get past this error. How do I include the “message.properties” bundle in the sourceSet?


(Stefan Oehme) #5

Your file is called ‘messages.properties’, so you have a typo in your code.


(Billy Buzzard) #6

Once again, thank you Stefan. I must have been very tired yesterday because I totally missed the misspelling.