How do I use Gradle bootRun with --args in Intelli

2020-08-18 05:16发布

问题:

Gradle 4.9 introduced the --args parameter to bootRun, which I can use easily from the command-line, but how do I use this from a Run/Debug configuration in Intellij 2018?

With a Gradle build set to run the bootRun task on my project in Intellij, I've tried the following arguments in the Run/Debug Configurations screen without any success:

  • --args 'foo'
  • --args='foo'
  • --args=foo

Output from Intellij:

9:18:56 AM: Executing task 'bootRun --args='foo''...

Unknown command-line option '--args'.
9:18:57 AM: Task execution finished 'bootRun --args='foo''.

A similar question documents the older syntax for doing this.

回答1:

Maybe you can add your args to Tasks as bootRun --args='foo' in IDEA's Run/Debug Configurations.

My task is run --args='-h' and it works for me



回答2:

Unfortunately, @Linsama's workaround will not work with multiple arguments. For example, run --args='--arg1 --arg2' will not work.

For multiple arguments, you have to move the entire thing in the Arguments field and leave the Tasks field blank.

This will work:

Tasks:

Arguments: run --args='--arg1 --arg2'



回答3:

As a workaround you can use gradle properties.

In intellij Arguments field add -Pargs=--myArg=value

Then in your build.gradle add

bootRun {
    if (project.hasProperty('args')) {
        args project.args.split(',')
    }
}

They should be now accesible using ApplicationArguments inside your application.