Gradle task - pass arguments to Java application

后端 未结 7 2177
春和景丽
春和景丽 2020-12-04 14:40

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         


        
相关标签:
7条回答
  • 2020-12-04 15:19

    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

    0 讨论(0)
  • 2020-12-04 15:22

    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

    0 讨论(0)
  • 2020-12-04 15:23

    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:

    1. Process the command line and remove --- and all other arguments following '---'
    2. 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:

    1. I was forced to use '---' and not '--'
    2. 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
    
    0 讨论(0)
  • 2020-12-04 15:24

    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
    
    0 讨论(0)
  • 2020-12-04 15:36

    If you want to use the same set of arguments all the time, the following is all you need.

    run {
        args = ["--myarg1", "--myarg2"]
    }
    
    0 讨论(0)
  • 2020-12-04 15:41

    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']"

    0 讨论(0)
提交回复
热议问题