作为快速集成测试的一部分,我正在尝试通过Gradle运行一个命令行Java应用程序。我正在从Maven移植我的构建脚本,这很容易通过exec-maven-plugin
完成。我的两大要求是:
请注意,我并不是要在构建脚本中读取这些属性,而是要在脚本构建和执行的Java程序中读取它们。
我还发现了另外两篇关于通过Gradle执行Java程序的文章:one with an answer that advocates using apply plugin: "application"
in the build file and gradle run
at the command line和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。我已经尝试了这两种方法,但都没有成功。
我有两个问题:
(1)我无法让Java可执行文件读取系统属性
无论我是否这样做:
build.gradle
apply plugin: 'application'
mainClassName = "com.mycompany.MyMain"
命令行
gradle run -Dmyproperty=myvalue
或者这样:
build.gradle
task execute (type:JavaExec) {
main = "com.mycompany.MyMain"
classpath = sourceSets.main.runtimeClasspath
}
命令行
gradle execute -Dmyproperty=myvalue
在任何一种情况下,myproperty
都无法通过。从MyMain.main (...)
开始运行的代码将myproperty
系统属性读取为null/missing。
(2)我无法传递命令行参数
这可能与第一个问题有关。例如,在exec-maven-plugin
中,命令行参数本身是通过系统属性传入的。Gradle是这样的吗,还是有其他方法可以传递命令行参数?
我如何让这些变量通过?另外,使用apply plugin: 'application'
和task execute (type:JavaExec)
哪个更好?
https://stackoverflow.com/questions/23689054
复制相似问题