如何从另一个Java进程中的Gradle任务运行Groovy脚本?

huangapple go评论74阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月20日 21:55:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定