How to call application plugin with different arguments in different tasks?

(stefan.bunk) #1

Easy setup, yet I cannot find how to achieve it: I have an application with a main class, which can take several command line arguments, and depending on them does different things.

Now I simply want to create tasks which call the program with some specific arguments. E.g. a task doSomething, which calls the main class with “–something”, so that ‘’‘gradle doSomething’’’ executes my main class with some arguments.

I am using the application plugin, but can’t find how to call it with different parameters depending on the task.

(Peter Niederwieser) #2

You’ll have to declare your own ‘JavaExec’ tasks. For details, see ‘JavaExec’ in the Gradle Build Language Reference.

(stefan.bunk) #3

Alright, thanks to Peter’s tip I could find a solution:

class RunWithParam extends JavaExec {}
  task crawl(type: RunWithParam, dependsOn: [build]) {
 main = "package.MainClass"
 classpath = sourceSets.main.runtimeClasspath
 args = ['parameter']

(stefan.bunk) #4

Don’t know, if that’s idiomatic, though. Coming to think of it, I guess you don’t need the subclass, unless you want to share some common configuration.

(Peter Niederwieser) #5

Right, no need to subclass. And if you wanted to provide a custom API, you’d usually declare a task class that delegates to ‘project.javaexec’, rather than inheriting from ‘JavaExec’.