foxundermon
foxundermon

Reputation: 580

gradle:how to run task in a method

i want run a task in a method or function,how to write code,can you give me a demo? thanks.

for example ...

this is a task.

task releaseJar(type: Copy) {
    from('build/intermediates/bundles/release')
    into('build/output/jar')
    include('classes.jar')
    rename('calsses.jar', 'core' + '0.0.1' + '.jar')
}

i want run it in

artifacts {
   ...same as  call releaseJar
    def myreleasejar = file 'build/output/jar/core0.0.1.jar'
    archives myreleasejar
    archives sourcesJar
}

Upvotes: 3

Views: 1559

Answers (1)

Mark Vieira
Mark Vieira

Reputation: 13486

You should never explicitly call tasks. Gradle will determine what tasks are needed to run based on a dependency graph. Therefore, you should instead declare task dependencies and Gradle will take care of the rest. In this case, you can tell Gradle what tasks are responsible for building an artifact.

artifacts {
    archives(file("${buildDir}/output/jar/core0.0.1.jar")) {
        builtBy releaseJar
    }
}

Upvotes: 3

Related Questions