[GRADLE-1804] Allow to pass command line arguments to a task Created: 22/Sep/11  Updated: 10/Feb/17  Resolved: 10/Feb/17

Status: Resolved
Project: Gradle
Affects Version/s: None
Fix Version/s: None

Type: Improvement
Reporter: Philippe Lhoste Assignee: Unassigned
Resolution: Won't Fix Votes: 10


 Description   

Sometime we need to pass a variable number of command line arguments to a task. One obvious example being JavaExec type tasks, as we might want to pass arguments to the underlying program to run, via the args parameter.
Currently, Gradle tries and process all arguments given on the command line as it was its own.

The current ways are:

  • Just edit the .gradle file to change the content of the args parameter. Bad because it adds noise to the version control.
  • Read a file with the arguments, parse it, and feed args with them. You need to know Groovy to do so, and it is tricky to handle the OS conventions: Windows accepts only double quotes, Unix-based systems are more flexible, etc.
  • Use -Pfoo=bar. Limited.
  • Use tasks.addRule and extract the arg from the task name. Too hackish and limited.

One idea is to have a flag indicating that beyond this point, the command line arguments belong to the task, no more to Gradle. Perhaps a -A flag (like "arguments"), or like in Unix shells, a double dash –

gradle -Pt=someClass run -A tvf -X -o="a dir" "file name with spaces"
^ Gradle arg ^ task name
^ end of Gradle args

One advantage to this form is that the shell will interpret itself the arguments with their own quoting (and escape) rules, Gradle will see them as a bunch of arguments to be fed into the task as an array (or whatever suitable collection) via some predefined name.



 Comments   
Comment by ronan michaux [ 21/Feb/13 ]

A little tip to pass command line ending args to a JavaExec task in Linux & bash environment.

  • Concept
    create an alias/function gr where first params are passed to gradle and all params after --endParams are passed to javaExec task.

JavaExec params are passed by a small temp file.

  • In ~/.bash_alias
    gr() {
    
     export argFile=$(mktemp)
    
     unset gradleParams
    
     while [ $# != 0 -a "$1" != "--endParams" ]
    
     do
    
        if [[ "$1" == *" "* ]]
    
        then
    
         export gradleParams="$gradleParams \"$1\""
    
        else
    
         export gradleParams="$gradleParams $1"
    
        fi
    
        shift
    
     done
    
     shift
    
     while [ $# != 0 ]
    
     do
    
        echo $1 >> $argFile
    
        shift
    
     done
    
     gradle $gradleParams -PjavaExecArgsFile=$argFile
    
    }
    
  • In build.gradle (or in subprojects {...}

    section of parent build.gradle)

    project.ext.javaExecParams = hasProperty("javaExecArgsFile") ? new File(javaExecArgsFile).readLines() : []
    
    
  • In build.gradle
    task myJavaExecTask(dependsOn: 'classes', type: JavaExec) {
    
       main = "com.company.MyMainClass"
    
       classpath = sourceSets.main.runtimeClasspath
    
       args project.ext.javaExecParams
    }
    
  • Launch :
    gr someOptionalGradleParams myJavaExecTask --endParams javaExecParam1 "java Exec Param with spaces"
    
Comment by Benjamin Muschko [ 15/Nov/16 ]

As announced on the Gradle blog we are planning to completely migrate issues from JIRA to GitHub.

We intend to prioritize issues that are actionable and impactful while working more closely with the community. Many of our JIRA issues are inactionable or irrelevant. We would like to request your help to ensure we can appropriately prioritize JIRA issues you’ve contributed to.

Please confirm that you still advocate for your JIRA issue before December 10th, 2016 by:

  • Checking that your issues contain requisite context, impact, behaviors, and examples as described in our published guidelines.
  • Leave a comment on the JIRA issue or open a new GitHub issue confirming that the above is complete.

We look forward to collaborating with you more closely on GitHub. Thank you for your contribution to Gradle!

Comment by Benjamin Muschko [ 10/Feb/17 ]

Thanks again for reporting this issue. We haven't heard back from you after our inquiry from November 15th. We are closing this issue now. Please create an issue on GitHub if you still feel passionate about getting it resolved.

Generated at Wed Jun 30 12:04:48 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.