From d19ac51aaa6e19426e1a43cd1c9eb88f7884850e Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Tue, 1 Sep 2020 18:52:06 +0300 Subject: [PATCH] BAEL-4371: Finished code samples --- gradle-5/cmd-line-args/build.gradle | 40 ++++++++++++++++--- .../main/java/com/baeldung/cmd/MainClass.java | 10 +++++ 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 gradle-5/cmd-line-args/src/main/java/com/baeldung/cmd/MainClass.java diff --git a/gradle-5/cmd-line-args/build.gradle b/gradle-5/cmd-line-args/build.gradle index aed57d775b..57c48e36c3 100644 --- a/gradle-5/cmd-line-args/build.gradle +++ b/gradle-5/cmd-line-args/build.gradle @@ -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 +} diff --git a/gradle-5/cmd-line-args/src/main/java/com/baeldung/cmd/MainClass.java b/gradle-5/cmd-line-args/src/main/java/com/baeldung/cmd/MainClass.java new file mode 100644 index 0000000000..f00aa07d72 --- /dev/null +++ b/gradle-5/cmd-line-args/src/main/java/com/baeldung/cmd/MainClass.java @@ -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 + "]"); + } + } +}