3

I want to use the Gradle Tooling API to invoke Gradle from an Eclipse plugin through the Buildship plug-ins. I am able to run basic tasks without problems.

One of my use cases is to execute the gradle init task in a new project folder, but to work non-interactively I have to pass the --type command line argument (or set the type property) on the init task. I can't find any way in the tooling API to set the properties of a task or to pass a task-specific command line argument.

I have tried BuildLauncher.addArgument("--type", "plain") but this is interpreted as an argument to Gradle itself, which is invalid.

How can I pass the --type plain argument to the init task?

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77

1 Answers1

3

After reading the docs here, I discovered you can set the task arguments via the Gradle command line build arguments. The important bit in the docs is:

Also, the task names configured by BuildLauncher.forTasks(String...) can be overridden if you happen to specify other tasks via the build arguments.

In my case I wanted to run gradle tasks --all via the tooling api. To get this working, I don't specify the task to run via forTasks(), I simply do not call that method. I set the task to run as part of the arguments via withArguments(). (In this case the arguments will be tasks --all).

I assume this should work the same for the init task.

badsyntax
  • 9,394
  • 3
  • 49
  • 67
  • 2
    It also works for command line options of custom tasks: `withArguments("customTask", "--myArg", "myValue")` is the way to go (remove the call to `forTasks()` entirely). – user1241663 Sep 24 '22 at 00:51