DirtyMikeAndTheBoys
DirtyMikeAndTheBoys

Reputation: 1077

Adding copy task as dependency to existing Gradle task

I have a Groovy app that has the following directory structure:

build.gradle
gradle.properties
settings.gradle
src/
    main/
        groovy/
            <Groovy code>
        resources/
            config.json
    test/

Currently, when I run gradle clean build shadowJar (uses the ShadowJar plugin although I don't think thats relevant here) I am left with a compiled, executable JAR located inside of build/distributions. I would like to add a task to my Gradle file that also copies the src/main/resources/config.json file to this build/distributions location, ideally without having to change my build invocation.

I tried this:

task copyJson(type: Copy) {
    from('src/main/resources')
    into('build/distributions')
    include('config.json')
}

However this forces me to use the following build invocation: gradle clean build shadowJar copyJson, which is not what I want.

So I ask: how can I add a task to my Gradle build such that running gradle clean build shadowJar not only produces my executable JAR under build/distributions, but also copies the src/main/resources/config.json file to that location as well?

Upvotes: 0

Views: 1534

Answers (1)

JB Nizet
JB Nizet

Reputation: 692231

Just add

build.dependsOn copyJson

to your build.gradle file. Note that if build should always create the shadow jar, you could also add

build.dependsOn shadowJar

and build with gradle build.

Upvotes: 2

Related Questions