英文:
How to run a Groovy script from a Gradle task in another java process?
问题
以下是翻译好的部分:
"Let's say you have a Groovy script that ends with a System.exit(...)
call and you want this Groovy script to be executed in a Gradle task. As the System.exit
call will terminate the whole JVM you want to run this script in another Java process (as using GroovyShell
does not seem to be an option because to of the System.exit
call). How can you do this without using a separate Groovy installation's groovy
command)?
If something like this might work, how do I have to replace ???
and ????
?
classpath = ???
mainClass = ???
args = ['my-script.groovy']
}
[Edit] Okay, here's a "minimal reproducible example" as mentioned in the comment below, if that makes the question better understandable:
The Groovy script:
println 'Hello World'
System.exit(0)
The Gradle task (this might be completely wrong and I do not know how to replace the question marks):
classpath = ???
mainClass = ???
args = ['my-script.groovy']
}
英文:
Let's say you have a Groovy script that ends with a System.exit(...)
call and you want this Groovy script to be executed in a Gradle task. As the System.exit
call will terminate the whole JVM you want to run this script in another Java process (as using GroovyShell
does not seem to be an option because to of the System.exit
call). How can you do this without using a separate Groovy installation's groovy
command)?
If something like this might work, how do I have to replace ???
and ????
?
javaexec {
classpath = ???
mainClass = ????
args = ['my-script.groovy']
}
[Edit] Okay, here's a "minimal reproducible example" as mentioned in the comment below, if that makes the question better understandable:
The Groovy script:
// file: my-script.groovy
println 'Hello World'
System.exit(0)
The Gradle task (this might be completely wrong and I do not know how to replace the question marks):
task runMyScript(type: JavaExec) {
classpath = ???
mainClass = ????
args = ['my-script.groovy']
}
答案1
得分: 1
Here is the translated content:
如何使用Java命令行运行Groovy脚本,您可以在Groovy的shell/batch中查看:
https://github.com/apache/groovy/blob/master/src/bin/groovy.bat#L30
java -cp "$GROOVY_HOME/lib" groovy.ui.GroovyMain myscript.groovy
唯一的问题是找出通常位于gradle/lib文件夹中的groovy-all库的位置。
我可以建议这种简单的方法:
def groovyJar = new File(groovy.ui.GroovyMain.getProtectionDomain().getCodeSource()?.getLocation()?.toURI())
或者,当使用javaexec
如上所述时,可以这样做:
javaexec {
classpath = files(groovy.ui.GroovyMain.protectionDomain.codeSource.location)
mainClass = 'groovy.ui.GroovyMain'
args = ['my-script.groovy']
}
英文:
how to run groovy script using java command line you can check in groovy shells/batches:
https://github.com/apache/groovy/blob/master/src/bin/groovy.bat#L30
java -cp "$GROOVY_HOME/lib" groovy.ui.GroovyMain myscript.groovy
the only question find out gradle/lib folder where usually groovy-all is located
i can suggest this as straight forward:
def groovyJar = new File(groovy.ui.GroovyMain.getProtectionDomain().getCodeSource()?.getLocation()?.toURI())
Or when using javaexec
as mentioned above, it's:
javaexec {
classpath = files(groovy.ui.GroovyMain.protectionDomain.codeSource.location)
mainClass = 'groovy.ui.GroovyMain'
args = ['my-script.groovy']
}
答案2
得分: 0
An alternative solution seems to be:
将以下内容添加到 build.gradle
中:
configurations {
groovyScript
}
dependencies {
groovyScript localGroovy()
...
}
然后稍后使用:
javaexec {
classpath = configurations.groovyScript
mainClass = 'groovy.ui.GroovyMain'
args = ['my-script.groovy']
}
我刚刚在网上看到这个。我不太明白这实际上是在做什么。如果有人能解释一下,那就太好了。非常感谢!
英文:
An alterntive solution seems to be:
Add the following to build.gradle
:
configurations {
groovyScript
}
dependencies {
groovyScript localGroovy()
...
}
Then later use:
javaexec {
classpath = configurations.groovyScript
mainClass = 'groovy.ui.GroovyMain'
args = ['my-script.groovy']
}
I've just read this somewhere in the web. I do not really understand what this is exactly doing. If someone could explain, that would be great. TYVMIA
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论