Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

build.gradle - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...