首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过Gradle运行Java类时传递系统属性和参数时出现问题

通过Gradle运行Java类时传递系统属性和参数时出现问题
EN

Stack Overflow用户
提问于 2014-05-16 05:31:13
回答 4查看 70.1K关注 0票数 87

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

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

请注意,我并不是要在构建脚本中读取这些属性,而是要在脚本构建和执行的Java程序中读取它们。

我还发现了另外两篇关于通过Gradle执行Java程序的文章:one with an answer that advocates using apply plugin: "application" in the build file and gradle run at the command lineanother with answers advocating that approach as well as using task execute(type:JavaExec) in the build file and gradle execute at the command line。我已经尝试了这两种方法,但都没有成功。

我有两个问题:

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

无论我是否这样做:

build.gradle

代码语言:javascript
复制
apply plugin: 'application'
mainClassName = "com.mycompany.MyMain"

命令行

代码语言:javascript
复制
gradle run -Dmyproperty=myvalue

或者这样:

build.gradle

代码语言:javascript
复制
task execute (type:JavaExec) {
    main = "com.mycompany.MyMain"
    classpath = sourceSets.main.runtimeClasspath 
}

命令行

代码语言:javascript
复制
gradle execute -Dmyproperty=myvalue

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

(2)我无法传递命令行参数

这可能与第一个问题有关。例如,在exec-maven-plugin中,命令行参数本身是通过系统属性传入的。Gradle是这样的吗,还是有其他方法可以传递命令行参数?

我如何让这些变量通过?另外,使用apply plugin: 'application'task execute (type:JavaExec)哪个更好?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23689054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档