How to pass multiple parameters in command line when running gradle task? - java

I've got a java and groovy classes that are being run by gradle task. I have managed to make it work but I do not like the way I have to pass the parameters in command line. Here is how I do it currently via command line: gradle runTask -Pmode"['doStuff','username','password']"
my build.gradle code which takes these parameters looks like this:
if (project.hasProperty("mode")) {
args Eval.me(mode)}
and then I use my arguments/parameters in my java code as follows:
String action = args[0]; //"doStuff"
String name = args[1]; .. //"username"
I was wondering is there a way to pass the parameters in a better way such as:
gradle runTask -Pmode=doStuff -Puser=username -Ppass=password
and how to use them in my java classes.

JavaExec may be the way to go. Just declare a task and pass project parameters to java app:
task myExecTask(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'com.project.MyApplicationMainClass'
args project.getProperty('userName') + ' ' + project.getProperty('password');
}
To run it, simply write gradle myExecTask -PuserName=john -Ppassword=secret

This is working for me:
task myExecTask(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'com.project.MyApplicationMainClass'
args(user, pass); // no need to access user and pass via project.getProperty()
}
args needs to be built as a List of Strings for java main to use.
args now should be in the form of : ['myusername', 'mypassword']

this is the task I had to create for passing arguments through gradle
task
task cucumber() {
dependsOn assemble, compileTestJava
doLast {
javaexec {
systemProperties = [
usr: project.getProperty('usr'),
pwd: project.getProperty('pwd')
]
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'pretty', '--glue', 'location to step def', 'location to feature files']
}
}
}
in order to execute test
$ gradle cucumber -Pusr=<username> -Ppwd=<password>
to access args in your code System.getProperty("usr"), System.getProperty("pwd")

Related

Gradle - Run Task for Multiple java programs within a project

Need: To create a run task each, for multiple programs within the same project
Based on the solution suggested in this LINK. I tried as shown below.
Working Code:
task runCustom1(type: JavaExec) {
group = 'Z_Custom_Run'
description = 'Testing for Gradle Run'
classpath sourceSets.main.runtimeClasspath
main = "pkg01.TestGradleRun"
}
task runCustom2(type: JavaExec) {
group = 'Z_Custom_Run'
description = 'Testing for Gradle Run'
classpath sourceSets.main.runtimeClasspath
main = "pkg01.TestGradleRun2"
}
But above method is cumbersome, as I have to generate for many programs & hence tried the below, to see if I can keep the code compact. But it gives an error as shown below.
Trial Code:
def customRunTask(String className, String packagePath){
return tasks.create("run${className}", JavaExec){
group = 'zCustomRun'
description = 'Testing for Gradle Run'
classpath sourceSets.main.runtimeClasspath
main = packagePath
}
}
artifacts {
archives customRunTask("Test1","pkg01.TestGradleRun"),
customRunTask("Test2","pkg01.TestGradleRun2")
}
Error:
A problem occurred evaluating root project 'testJavaFeatures'.
> Cannot convert the provided notation to an object of type ConfigurablePublishArtifact: task ':runTest1'.
The following types/formats are supported:
- Instances of ConfigurablePublishArtifact.
- Instances of PublishArtifact.
- Instances of AbstractArchiveTask, for example jar.
- Instances of Provider<RegularFile>.
- Instances of Provider<Directory>.
- Instances of Provider<File>.
- Instances of RegularFile.
- Instances of Directory.
- Instances of File.
- Maps with 'file' key
Since I am not too conversant with Gradle, seek guidance from the experts on how to fix the error & get it working
you were almost there ... the below should work
def customRunTask(String className, String packagePath){
return tasks.create("run${className}", JavaExec){
group = 'zCustomRun'
description = 'run ${packagePath}.${className}'
classpath sourceSets.main.runtimeClasspath
main = packagePath + '.' + className
}
}
customRunTask('ClassA', 'com.pkg1')
customRunTask('ClassB', 'com.pkg2')
(and remove the artifacts section, from your file)
Not sure what you try to do, but you can generate similar tasks en masse very easily:
List mainClassNames = [ 'pkg01.TestGradleRun', 'pkg01.TestGradleRun2' ]
mainClassNames.each{ name ->
task "runCustom-$name"(type: JavaExec) {
group = 'Z_Custom_Run'
description = "Testing for Gradle Run for $name"
classpath sourceSets.main.runtimeClasspath
main = name
}
}
artifacts {
archives mainClassNames.collect{ ":runCustom-$it" }
}

How to create Gradle BootRun Tasks with CLI-Args input

Context:
I am trying to create two tasks which activate a BootRun Task in gradle. I call these tasks from other modules) It is a SpringBootApp which accepts command-line arguments and takes the action based on the argument given as input. Apart from that it's code generator/ table generator,if that helps.
Gradle
task generateClass(dependsOn:":tools:codegen:clean") {
bootRun {
args = ["--class"]
}
}
task generateMetadata(dependsOn:":tools:codegen:clean") {
bootRun {
args = []
args = ["--metadata"]
}
}
generateClass.finalizedBy generateClass:bootRun
generateMetadata.finalizedBy generateMetadata:bootRun
Problem
The Problem with the above it is that whenever I call the BootRun from either GenerateClass or GenerateMetadata task, it calls a single BootRun without the arguments I have set for the task.
Should Work:
GenerateClass --> ClassArgs --> Run
GenerateMetadata --> MetadataArgs --> Run
But Works as:
Args = which ever task from above runs first, their arguments are set.
So sometimes it is
GenerateClass --> MetadataArgs --> Run
GenerateMetadata --> MetadataArgs --> Run
or
GenerateClass --> ClassArgs --> Run
GenerateMetadata --> ClassArgs --> Run
So my solution is either creating a custom bootRun Task with different arguments or passing CLI arguments effectively by running the task.
NOTE: Yes the SpringBoot CLI parsing is correct as I have tested it. Plus I call these tasks from other modules.
After reading the documentation linked by JB Nizet ! I created these BootRun task which take commandLineArguments and also use spring-profiles! Hope this is useful :)
task generateClass(type: org.springframework.boot.gradle.tasks.run.BootRun, dependsOn: 'build') {
doFirst() {
main = 'com.etcbase.uyap.generator.GeneratorApplication'
classpath = sourceSets.main.runtimeClasspath
args '--class'
systemProperty 'spring.profiles.active', 'dev'
}
}
task generateMetadata(type: org.springframework.boot.gradle.tasks.run.BootRun, dependsOn: 'build') {
doFirst() {
main = 'com.etcbase.uyap.generator.GeneratorApplication'
args '--metadata'
classpath = sourceSets.main.runtimeClasspath
systemProperty 'spring.profiles.active', 'test'
}
}

Gradle task - pass arguments to Java application

I have a Java application that runs with a custom gradle task and the application requires some arguments upon being invoked. These are:
programName ( string | -f filename | -d key | -h)
Options:
string Message to be used.
-d key Use default messages, key must be s[hort], m[edium] or l[ong].
-f filename Use specified file as input.
-h Help dialog.
Gradle task looks like:
task run (type: JavaExec){
description = "Secure algorythm testing"
main = 'main.Test'
classpath = sourceSets.main.runtimeClasspath
}
I've tried running gradle run -h and it does not work.
Gradle 4.9+
gradle run --args='arg1 arg2'
This assumes your build.gradle is configured with the Application plugin. Your build.gradle should look similar to this:
plugins {
// Implicitly applies Java plugin
id: 'application'
}
application {
// URI of your main class/application's entry point (required)
mainClassName = 'org.gradle.sample.Main'
}
Pre-Gradle 4.9
Include the following in your build.gradle:
run {
if (project.hasProperty("appArgs")) {
args Eval.me(appArgs)
}
}
Then to run: gradle run -PappArgs="['arg1', 'args2']"
Since Gradle 4.9, the command line arguments can be passed with --args. For example, if you want to launch the application with command line arguments foo --bar, you can use
gradle run --args='foo --bar'
See Also Gradle Application Plugin
How to upgrade Gradle wrapper
If you want to use the same set of arguments all the time, the following is all you need.
run {
args = ["--myarg1", "--myarg2"]
}
Sorry for answering so late.
I figured an answer alike to #xlm 's:
task run (type: JavaExec, dependsOn: classes){
if(project.hasProperty('myargs')){
args(myargs.split(','))
}
description = "Secure algorythm testing"
main = "main.Test"
classpath = sourceSets.main.runtimeClasspath
}
And invoke like:
gradle run -Pmyargs=-d,s
You can find the solution in Problems passing system properties and parameters when running Java class via Gradle . Both involve the use of the args property
Also you should read the difference between passing with -D or with -P that is explained in the Gradle documentation
Of course the answers above all do the job, but still i would like to use something like
gradle run path1 path2
well this can't be done, but what if we can:
gralde run --- path1 path2
If you think it is more elegant, then you can do it, the trick is to process the command line and modify it before gradle does, this can be done by using init scripts
The init script below:
Process the command line and remove --- and all other arguments following '---'
Add property 'appArgs' to gradle.ext
So in your run task (or JavaExec, Exec) you can:
if (project.gradle.hasProperty("appArgs")) {
List<String> appArgs = project.gradle.appArgs;
args appArgs
}
The init script is:
import org.gradle.api.invocation.Gradle
Gradle aGradle = gradle
StartParameter startParameter = aGradle.startParameter
List tasks = startParameter.getTaskRequests();
List<String> appArgs = new ArrayList<>()
tasks.forEach {
List<String> args = it.getArgs();
Iterator<String> argsI = args.iterator();
while (argsI.hasNext()) {
String arg = argsI.next();
// remove '---' and all that follow
if (arg == "---") {
argsI.remove();
while (argsI.hasNext()) {
arg = argsI.next();
// and add it to appArgs
appArgs.add(arg);
argsI.remove();
}
}
}
}
aGradle.ext.appArgs = appArgs
Limitations:
I was forced to use '---' and not '--'
You have to add some global init script
If you don't like global init script, you can specify it in command line
gradle -I init.gradle run --- f:/temp/x.xml
Or better add an alias to your shell:
gradleapp run --- f:/temp/x.xml
You need to pass them as args to the task using project properties, something like:
args = [project.property('h')]
added to your task definition (see the dsl docs)
Then you can run it as:
gradle -Ph run

Problems passing system properties and parameters when running Java class via Gradle

I am trying to run a command-line Java app via Gradle as part of a quick integration test. I am porting my build scripts from Maven, where this was easily done via exec-maven-plugin. My two big requirements are:
Being able to pass system properties to the executable Java code
Being able to pass command-line args to the executable Java code
Please note that I am not trying to read these properties in the build script, I'm trying to read them in the Java program that the script builds and executes.
I have found two other SO posts that address Java program execution via Gradle: one with an answer that advocates using apply plugin: "application" in the build file and gradle run at the command line, and another with answers advocating that approach as well as using task execute(type:JavaExec) in the build file and gradle execute at the command line. I have tried both approaches and have not succeeded.
I have two problems:
(1) I cannot get the Java executable to read the system properties
Whether I do this:
build.gradle:
apply plugin: 'application'
mainClassName = "com.mycompany.MyMain"
Command line:
gradle run -Dmyproperty=myvalue
Or this:
build.gradle:
task execute (type:JavaExec) {
main = "com.mycompany.MyMain"
classpath = sourceSets.main.runtimeClasspath
}
Command line:
gradle execute -Dmyproperty=myvalue
In either case, myproperty does not make it through. The code that begins running from MyMain.main (...) reads the myproperty system property as null/missing.
(2) I cannot pass command line arguments
This is probably related to the first problem. In exec-maven-plugin, for example, command line args were themselves passed in via a system property. Is that the case with Gradle, or is there another way to pass command line arguments?
How do I get these variables through? Also, is it better to use apply plugin: 'application' or task execute (type:JavaExec)?
Figured it out. The main issue is that when Gradle forks a new Java process, it does not automatically pass the environment variable values along to the new environment. One has to explicitly pass these variables via the systemProperties property of the task or plugin.
The other issue was understanding how to pass command-line args; these are via the args property on the task or plugin. As with the Maven exec-maven-plugin, they should be passed in on the command line via yet another system property, as a space-delimited list that then needs to be split() before setting args, which accepts List objects. I've named the property exec.args, which is the old Maven name.
It seems both the javaExec and application plugin approach are valid. One might favor the application plugin approach if one wants to use some of its other features (automatically putting together a distribution, etc.)
Here are the solutions:
JavaExec Approach
Command Line:
gradle execute -Dmyvariable=myvalue -Dexec.args="arg1 arg2 arg3"
build.gradle:
task execute (type:JavaExec) {
main = "com.myCompany.MyMain"
classpath = sourceSets.main.runtimeClasspath
/* Can pass all the properties: */
systemProperties System.getProperties()
/* Or just each by name: */
systemProperty "myvariable", System.getProperty("myvariable")
/* Need to split the space-delimited value in the exec.args */
args System.getProperty("exec.args", "").split()
}
Application Plugin Approach
Command Line:
gradle run -Dmyvariable=myvalue -Dexec.args="arg1 arg2 arg3"
build.gradle:
apply plugin: 'application'
mainClassName = "com.mycompany.MyMain"
run {
/* Can pass all the properties: */
systemProperties System.getProperties()
/* Or just each by name: */
systemProperty "myvariable", System.getProperty("myvariable")
/* Need to split the space-delimited value in the exec.args */
args System.getProperty("exec.args", "").split()
}
For those who might not want to pollute your application's system properties by passing unrelated Gradle props, I recommend namespacing your arguments.
tasks.withType(JavaExec) {
System.properties.each { k,v->
if (k.startsWith("prefix.")) {
systemProperty k - "prefix.", v
}
}
}
java ... -Dprefix.my.prop=true will pass my.prop
I'm new to gradle so I needed this and what is working for me with gradle 4.6 seems a little easier for the command line. Instead of parsing 1 arg string you can pass an array of args, and I found a way to pass in all property with one line as well. Combined below:
apply plugin: 'java'
apply plugin: 'org.springframework.boot' <- for my project
task runApp(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'testit.TomcatApp'
// arguments to pass to the application
// args 'myarg1 -rest' <- came in as 1 string
args = ["--myarg1 with spaces even", "--myarg2"]
// and to pass in all -D system property args:
systemProperties = System.properties
}
gradle run -Dwhatever=xxx -Dmyarg2=hey
// Java reading them:
public static void main(String[] args) {
for ( int i = 0; i < args.length; i++ )
{
logger.info( "** args [" + i + "] =" + args[i] + "=" );
}
logger.info( "** -Dwhatever =" + System.getProperty("whatever") + "=" );
logger.info( "** -Dmyarg2 =" + System.getProperty("myarg2") + "=" );
[main] INFO testit.TomcatApp - ** args [0] =--myarg1 with spaces even=
[main] INFO testit.TomcatApp - ** args [1] =--myarg2=
[main] INFO testit.TomcatApp - ** -Dwhatever =xxx=
[main] INFO testit.TomcatApp - ** -Dmyarg2 =hey=
Maybe I am late for the party, but has anyone tried with "set the prop before executing gradle"? I have tested and this works too, apparently.
myVar=myVal gradle test
For example, you can set the active profile like:
SPRING_PROFILES_ACTIVE=dev gradle test
These also work, apparently:(tested)
set myVar=myVal && gradle test # for windows
export myVar=myVal && gradle test # for linux and mac
Be wary that myVar cannot be period-separated; or else only the part before the first period will be taken as key.

How to pass parameters to main method using Gradle?

I have to pass two arguments to my main method. My build script is
// Apply the java plugin to add support for Java
apply plugin: 'java'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'maven central' for resolving your dependencies.
mavenCentral()
}
// In this section you declare the dependencies for your production and test code
dependencies {
compile 'com.example:example-core:1.7.6'
}
task main(type: JavaExec, dependsOn: classes) {
description = 'This task will start the main class of the example project'
group = 'Example'
main = 'com.example.core.Example'
classpath = sourceSets.main.runtimeClasspath
}
If I try:
gradlew main doc.json text.txt
Then an error occured.
org.gradle.execution.TaskSelectionException: Task 'doc.json' not found in root project
How can I pass arguments to my main method command line easily?
task run(type: JavaExec) {
main = "pkg.MainClass"
classpath = sourceSets.main.runtimeClasspath
args = ["arg1", "arg2"]
}
You should use use -P as listed in the Gradle command line documentation.
For example, the following will work:
gradlew main -Parg1=doc.json --project-prop arg2=text.txt
And you access them in your Gradle script like this:
println "$arg1 $arg2"
task run1(type: JavaExec) {
main = "pkg.mainclass"
classpath = sourceSets.main.runtimeClasspath
args = ["$arg1","$arg2",...]
}
//I have named as run1 it can be any task name
While invoking the gradle script:
c:\> gradle run1 -Parg1="test123" -Parg2="sss"

Categories

Resources