How can I make a custom task that does 2 things?

plugins

(Dave Syer) #1

I want to make a plugin with a custom task that does the same as this, but allows users to configure the inputs and outputs (an outline of code which works in build.gradle):

task(prepareResolve, type: Copy) {
    dependsOn = [bootRepackage]
    from jar.archivePath
    into 'build/tmp'
}

task(resolve, type: Exec) {
    dependsOn = [prepareResolve]
    workingDir 'build/tmp'
    commandLine Jvm.current().javaExecutable
    args ..., "-jar", jar.archiveName
}

but I only want one task (there’s only one thing for the user to do, which is “resolve”, so there’s no point having additional tasks).

I can’t figure out how to nest the Copy and Exec into a single custom task. I can add a custom task to a plugin and see it running, but I can’t work out how to instantiate and execute the nested bits.


(Stefan Oehme) #2

You could use Project.copy and Project.exec. But in general splitting things up is a good idea, because when something fails, Gradle can resume at the last successful task instead of doing everything again.

It’s quite normal to have tasks that are just implementation details.


(Dave Syer) #3

Sorry you lost me. I’m doing this in Java, so maybe Project.copy is a Groovy DSL? I don’t see it in the API.


(Stefan Oehme) #4

Does your Java IDE have no auto completion? :slight_smile: Here’s the JavaDoc link.


(Dave Syer) #5

Still lost. I can only pass in a Closure. I don’t think this is helping much.


(Stefan Oehme) #6

You can pass in an Action, see the JavaDoc I just linked. Make sure you are using a recent Gradle version.