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

怎甘沉沦 提交于 2019-12-12 07:32:29

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/34875637/how-to-pass-multiple-parameters-in-command-line-when-running-gradle-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!