How do I add resources to sourceSet with gradle?

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 🙂

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)