Question

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!

Was it helpful?

Solution

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 :)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top