在通过Gradle运行Java类时传递系统属性和参数的问题 [英] Problems passing system properties and parameters when running Java class via Gradle

查看:161
本文介绍了在通过Gradle运行Java类时传递系统属性和参数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过Gradle运行一个命令行Java应用程序,作为快速集成测试的一部分。我正在从Maven移植我的构建脚本,在这里很容易通过 exec-maven-plugin 完成。我的两大要求是:

$ ul

  • 能够将系统属性传递给可执行Java代码
  • 能够将命令行参数传递给可执行Java代码


    请注意,我并不想在构建中读取这些属性脚本,我试图在Java程序中读取脚本构建并执行的脚本。



    我发现了另外两个SO帖子,它们通过Gradle来实现Java程序的执行: 一个带有支持者的答案在构建文件中使用应用插件:application,在命令行中使用 gradle run >,以及另一个提倡这种方法的解答以及使用任务执行(类型:JavaExec)中构建文件和 gradle在命令行执行>。我已经尝试了两种方法,并没有成功。



    我有两个问题:

    (1 )我无法让Java可执行文件读取系统属性



    是否执行此操作:

    build.gradle

      apply plugin:'application'
    mainClassName =com .mycompany.MyMain

    命令行

      gradle run -Dmyproperty = myvalue 

    或者这个:
    $ b

    build.gradle

     任务执行(类型:JavaExec){
    main =com.mycompany.MyMain
    classpath = sourceSets.main.runtimeClasspath
    }

    命令行

      gradle execute -Dmyproperty = myvalue 

    在任何一种情况下, myproperty 不能通过。从 MyMain.main(...)开始运行的代码读取 myproperty 系统属性为null / missing。

    (2)我无法传递命令行参数
    $ b 这可能与到第一个问题。例如,在 exec-maven-plugin 中,命令行参数本身是通过系统属性传入的。这是Gradle的情况,还是有另一种方式来传递命令行参数?



    如何获取这些变量?另外,使用 apply plugin:'application'还是 task execute(type:JavaExec)

    解决方案

    想通了。主要问题是,当Gradle推出新的Java进程时,它不会自动将环境变量值传递到新环境。必须通过任务或插件的 systemProperties 属性显式传递这些变量。

    另一个问题是理解如何传递命令行参数;这些是通过任务或插件上的 args 属性。与Maven exec-maven-plugin 一样,它们应该通过另一个系统属性通过命令行传递,作为空格分隔的列表,然后需要 split(),然后设置 args ,它接受 List 对象。我已经命名属性 exec.args ,这是旧的Maven名称。



    code> javaExec 和应用程序插件方法是有效的。如果你想使用其他一些功能(自动将一个发行版放在一起等),那么可以使用应用程序插件方法。



    以下是解决方案:



    JavaExec方法



    命令行

      gradle execute -Dmyvariable = myvalue -Dexec.args =arg1 arg2 arg3



    build.gradle


     任务执行(类型:JavaExec) {

    main =com.myCompany.MyMain
    classpath = sourceSets.main.runtimeClasspath

    / *可以传递所有属性:* /
    systemProperties System.getProperties()

    / *或者只是每个名称:* /
    systemPropertymyvariable,System.getProperty(myvariable)

    / *需要分割exec.args * /
    中的空格分隔值System.getProperty(exec.args,).split()
    }



    应用程序插件方法

    命令行

      gradle run -Dmyvariable = myvalue -Dexec.args =arg1 arg2 arg3

    构建.gradle



      apply plugin:'application'
    mainClassName =com.mycompany.MyMain
    运行{
    / *可以传递所有的属性:* /
    systemProperties System.getProperties()

    / *或者只是每个名称:* /
    systemPropertymyvariable,System.getProperty(myvariable)

    / *需要在exec.args中分隔空格分隔的值* /
    args System.getProperty( exec.args,).split()
    }


    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()    
    }
    

    这篇关于在通过Gradle运行Java类时传递系统属性和参数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆