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:
commit
0c72ddaf97
|
@ -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()
|
||||||
|
}
|
|
@ -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 + "]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,3 +2,4 @@ rootProject.name='gradle-5-articles'
|
||||||
include 'java-exec'
|
include 'java-exec'
|
||||||
include 'unused-dependencies'
|
include 'unused-dependencies'
|
||||||
include 'source-sets'
|
include 'source-sets'
|
||||||
|
include 'cmd-line-args'
|
Loading…
Reference in New Issue