I am creating simple Java class and I would like to create out-of-the-box launcher by using gradle.
So I want to be able to run Java program via gradle:
gradlew clean run These are my command line arguments
where These are my command line arguments
are passed directly to my public static void main(String... args)
method.
I am using the apply plugin: "application"
which gives me the run
task. But when I am running this 'as is' I've got:
* What went wrong:
Task 'These' not found in root project 'question1'. Some candidates are: 'test'.
Like java command line, Gradle command also accepts parameters with -D option Here is an command to pass custom properties to command line. In build.gradle, you can configure in ext block. ext block holds all user defined properties related project , system and task arguments.
For Spring Boot 2.x, we can pass the arguments using -Dspring-boot.run.arguments: 3. Gradle Command-Line Arguments Next, let's discover how to pass arguments while running our application using Gradle Plugin. We'll need to configure our bootRun task in build.gradle file: Now, we can pass the command-line arguments as follows: 4.
Command-line arguments are given after the name of the program in command-line shell of Operating Systems. To pass command line arguments, we typically define main () with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. int main (int argc, char *argv []) { /* ... */ }
Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the args array, but it is the first element of the GetCommandLineArgs () method. The following list shows valid Main signatures: The preceding examples all use the public accessor modifier.
Gradle interprets each argument not starting with a hyphen (-
) as task name to define which tasks will be executed during the build. Since a task with the name These
does not exist, the build fails.
You are using the Gradle Application Plugin, which, as you already stated, provides a run
task. The docs show, that the run
task is of the type JavaExec
. So, to apply your arguments, add them to the run
task via args
:
run {
args 'These are my command line arguments'
}
To modify the arguments for each build without changing the build.gradle
file, simply use project properties:
run {
args findProperty('runArgs')
}
And call gradle clean run -PrunArgs='These are my command line arguments'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With