Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add resources to sourceSet with gradle?

Currently I have the following build.gradle file:

apply plugin: 'java'  repositories {     mavenCentral() }  sourceSets {     main {         java {             srcDir 'src/model'         }         resources {             srcDir 'images/model'          }     }      test {         java {             srcDir 'tests/model'         }         resources {             srcDir 'images/model' // <=== NOT WORKING         }     } }  dependencies {     compile files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')     runtime fileTree(dir: 'libs', include: '*.jar')      testCompile group: 'junit', name: 'junit', version: '4.+' } 

My repository if here: https://github.com/quinnliu/WalnutiQ

and 4 out of my 49 tests are failing because the tests in folder "tests/model" need a file within the folder "images/model". How do I add the resources correctly? Thanks!

like image 257
Wang-Zhao-Liu Q Avatar asked Dec 22 '13 17:12

Wang-Zhao-Liu Q


People also ask

What does SourceSet do in Gradle?

1.1 What is a Gradle SourceSet ? A SourceSet is a collection of java source files and additional resource files that are compiled and assembled together to be executed. The main idea of sourcesets is to group files with a common meaning for the project, with no need of separate them in another project.

What is Runtimeclasspath in Gradle?

Runtime classpath for running tests. Used by task test . archives. Artifacts (e.g. jars) produced by this project. Used by Gradle to determine "default" tasks to execute when building.


2 Answers

I had a closer look at your build.gradle and it seems that paths are a little bit off.

You specify source as src/model, yet your project structure and Java source suggest that model is your package name, which means the source declaration should be:

main {     java {         srcDir 'src'     } } 

Same for tests:

test {     java {         srcDir 'tests'     } } 

Now, with missing resources. In your code you are using ImageIO.read(getClass().getResource(BMPFileName))
getClass().getResource() is using relative path to the resource. To keep the resources on the same level, you should update declaration for the resources and remove model:

test {     java {         srcDir 'tests'     }     resources {         srcDir 'images'     } } 

You might also need to run

./gradlew clean 

before it works.

Here's the result with the updated build.gradle:

enter image description here

Hope it helps :)

like image 101
kukido Avatar answered Sep 29 '22 03:09

kukido


The syntax used in your build script is correct. It's not clear to me why you add the same resource directory to both source sets, and why you claim that it isn't working in one case.

srcDir "foo" adds another directory. If you instead want to replace the default directory, use srcDirs = [ "foo" ] instead. However, this won't solve the problem at hand.

It would be good to see the code that loads the resources, to rule out any problems with that.

like image 21
Peter Niederwieser Avatar answered Sep 29 '22 02:09

Peter Niederwieser