BAEL-4371: Finished code samples

This commit is contained in:
Sorin Zamfir 2020-09-01 18:52:06 +03:00
parent 691a948c5f
commit d19ac51aaa
2 changed files with 45 additions and 5 deletions

View File

@ -1,16 +1,46 @@
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('input')){
println "This is my property ["+ project.getProperty('input')+"]"
} else {
println "No property was passed"
project.getProperties().values().each {
println "Project property ["+it+"]"
}
System.getProperties().each {
println "System property ["+it+"]"
}
}
}
if (project.hasProperty("args")) {
ext.cmdargs = project.getProperty("args")
ext.cmdargsarray = cmdargs.split()
} else {
ext.cmdargs = ""
}
task cmdLineJavaExec(type: JavaExec) {
group = "Execution"
description = "Run the main class with JavaExecTask"
classpath = sourceSets.main.runtimeClasspath
main = javaMainClass
args cmdargsarray
}
ext.cmdarray = ["java", "-classpath", sourceSets.main.runtimeClasspath.getAsPath(), javaMainClass]
cmdarray = (cmdarray << cmdargsarray).flatten()
task cmdLineExec(type: Exec) {
dependsOn build
group = "Execution"
description = "Run the main class with ExecTask"
commandLine cmdarray
}

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 + "]");
}
}
}