JJD
JJD

Reputation: 51804

How to wrap an existing Gradle task with parameters into a custom task?

I am trying to create a custom Gradle task which invokes an existing Gradle task with parameters which are specific to my project. Here is how I invoke the task from the command line:

./gradlew downloadJson \
    -Pendpoint=http://example.com/foo \
    -Pdestination=src/main/com/example/foo.json

I would like to create a downloadFoo task which I can invoke without specifying the parameters explicitely.

tasks.register("downloadFoo" /* type needed? */) {
    // What goes here?
}

Related

Upvotes: 2

Views: 1177

Answers (2)

Greg Barski
Greg Barski

Reputation: 21

Try this form:

tasks.register("downloadBackendSchema", com.apollographql.apollo.gradle.internal.ApolloDownloadSchemaTask) { task ->
    description("Downloads Apollo Schema")
    group("Apollo")
    task.schemaRelativeToProject.set("src/main/java/com/project/backend/grqphql/schema.json")
    task.endpoint.set("http://your-project.com/graphql")
}

Upvotes: 0

tim_yates
tim_yates

Reputation: 171084

There's no real concept of tasks wrapping other tasks in Gradle...

What you can do in this situation is to just create a new task of type ApolloDownloadSchemaTask, and then set the properties:

import com.apollographql.apollo.gradle.internal.ApolloDownloadSchemaTask

tasks.register("downloadFoo", ApolloDownloadSchemaTask) { task ->
    description("Downloads foo.")
    group("Apollo")
    task.schemaFilePath.set("src/main/com/example/foo.json")
    task.endpointUrl.set("http://example.com/foo")
}

Upvotes: 2

Related Questions