Merge pull request #9973 from srzamfir/feature/BAEL-4371_Passing_cmd_line_args_in_gradle

BAEL-4371: Passing cmd line args in gradle
This commit is contained in:
Eric Martin 2020-09-13 10:42:56 -05:00 committed by GitHub
commit 0c72ddaf97
3 changed files with 50 additions and 1 deletions

View File

@ -0,0 +1,38 @@
apply plugin: "java"
apply plugin: "application"
description = "Gradle Command Line Arguments examples"
ext.javaMainClass = "com.baeldung.cmd.MainClass"
application {
mainClassName = javaMainClass
}
task propertyTypes(){
doLast{
if (project.hasProperty("args")) {
println "Our input argument with project property ["+project.getProperty("args")+"]"
}
println "Our input argument with system property ["+System.getProperty("args")+"]"
}
}
if (project.hasProperty("args")) {
ext.cmdargs = project.getProperty("args")
} else {
ext.cmdargs = "ls"
}
task cmdLineJavaExec(type: JavaExec) {
group = "Execution"
description = "Run the main class with JavaExecTask"
classpath = sourceSets.main.runtimeClasspath
main = javaMainClass
args cmdargs.split()
}
task cmdLineExec(type: Exec) {
group = "Execution"
description = "Run an external program with ExecTask"
commandLine cmdargs.split()
}

View File

@ -0,0 +1,10 @@
package com.baeldung.cmd;
public class MainClass {
public static void main(String[] args) {
System.out.println("Gradle command line arguments example");
for (String arg : args) {
System.out.println("Got argument [" + arg + "]");
}
}
}

View File

@ -2,3 +2,4 @@ rootProject.name='gradle-5-articles'
include 'java-exec'
include 'unused-dependencies'
include 'source-sets'
include 'cmd-line-args'