我试图通过Gradle运行命令行Java应用程序作为快速集成测试的一部分.我正在从Maven移植我的构建脚本,这很容易通过exec-maven-plugin.我的两大要求是:
请注意,我不是试图在构建脚本中读取这些属性,我试图在脚本构建和执行的Java程序中读取它们.
我找到了另外两个SO帖子,它们通过Gradle处理Java程序执行:一个答案主张apply plugin: "application"在构建文件和gradle run命令行中使用,另一个答案提倡该方法以及task execute(type:JavaExec)在构建文件和构建文件gradle execute中使用命令行.我尝试了两种方法,但没有成功.
我有两个问题:
(1)我无法获取Java可执行文件来读取系统属性
我是否这样做:
build.gradle:
apply plugin: 'application'
mainClassName = "com.mycompany.MyMain"
Run Code Online (Sandbox Code Playgroud)
命令行:
gradle run -Dmyproperty=myvalue
Run Code Online (Sandbox Code Playgroud)
或这个:
build.gradle:
task execute (type:JavaExec) {
main = "com.mycompany.MyMain"
classpath = sourceSets.main.runtimeClasspath
}
Run Code Online (Sandbox Code Playgroud)
命令行:
gradle execute -Dmyproperty=myvalue
Run Code Online (Sandbox Code Playgroud)
无论哪种情况,myproperty都无法完成.从开始运行的代码MyMain.main (...)将myproperty系统属性读取为null/missing.
(2)我无法传递命令行参数
这可能与第一个问题有关.在exec-maven-plugin,例如,命令行参数本身也是经由系统属性传递英寸 这是Gradle的情况,还是有另一种传递命令行参数的方法?
我如何通过这些变量?另外,使用apply plugin: 'application'或更好task execute (type:JavaExec)吗?
spa*_*ead 117
弄清楚了.主要问题是,当Gradle分支新的Java进程时,它不会自动将环境变量值传递给新环境.必须通过systemProperties任务或插件的属性显式传递这些变量.
另一个问题是了解如何传递命令行参数; 这些是通过args任务或插件上的属性.与Maven一样exec-maven-plugin,它们应该通过另一个系统属性在命令行上传递,作为空格分隔的列表,然后需要split()在设置之前args接受List对象.我已将该属性命名为exec.argsMaven的旧名称.
似乎javaExec和应用程序插件方法都是有效的.如果想要使用其他一些功能(自动组合分发等),人们可能会喜欢应用程序插件方法
以下是解决方案:
命令行:
gradle execute -Dmyvariable=myvalue -Dexec.args="arg1 arg2 arg3"
Run Code Online (Sandbox Code Playgroud)
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()
}
Run Code Online (Sandbox Code Playgroud)
命令行:
gradle run -Dmyvariable=myvalue -Dexec.args="arg1 arg2 arg3"
Run Code Online (Sandbox Code Playgroud)
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()
}
Run Code Online (Sandbox Code Playgroud)
geg*_*geg 12
对于那些可能不想通过传递不相关的Gradle道具来污染应用程序的系统属性的人,我建议命名空间参数.
tasks.withType(JavaExec) {
System.properties.each { k,v->
if (k.startsWith("prefix.")) {
systemProperty k - "prefix.", v
}
}
}
Run Code Online (Sandbox Code Playgroud)
java ... -Dprefix.my.prop=true 将传递 my.prop
| 归档时间: |
|
| 查看次数: |
55931 次 |
| 最近记录: |