Gradle:从任务调用Groovy静态方法

Gradle:从任务调用Groovy静态方法,gradle,groovy,Gradle,Groovy,我有一个使用Groovy插件的Gradle项目 gradle项目的文件夹结构为: src main groovy com acme runner resources build.gradle 我有一个任务,它使用JavaExec调用Simple.groovy中定义的Simple类的main方法,如下所示: task runScript(type: JavaExec) { description 'Run Groovy script'

我有一个使用Groovy插件的Gradle项目

gradle项目的文件夹结构为:

src
 main
  groovy
    com
      acme
       runner
  resources
build.gradle
我有一个任务,它使用
JavaExec
调用
Simple.groovy
中定义的
Simple类的main方法,如下所示:

task runScript(type: JavaExec) {
    description 'Run Groovy script'     
    // Set main property to name of Groovy script class.
    main = 'com.acme.runner.Simple'
    // Set classpath for running the Groovy script.
    classpath = sourceSets.main.runtimeClasspath
}

我想在
简单类
中定义一个任务调用另一个方法这可能吗?它是否具有静态方法的特性?谢谢。

一个稍微不直接的解决方案是根据任务的
参数值从
main
方法或脚本主体控制方法调用:

简单的

package com.playground

switch( args ? args[ 0 ] : null ) {
    case 'a':
        hello()
        break
    case 'b':
        bye()
        break
    default:
        println 'Don\'t know what you mean'
}

void hello() {
    println 'Hello a!'
}

void bye() {
    println 'Goodbye a!'
}
梯度任务:

task runScript(type: JavaExec) {
    description 'Run Groovy script'
    main = 'com.playground.Simple'
    classpath = sourceSets.main.runtimeClasspath
    args 'a' // 'b'
}
产生

> Task :compileJava NO-SOURCE
> Task :compileGroovy UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes

> Task :runScript
Hello a!

一个稍微不直接的解决方案是根据任务的
args
值从
main
方法或脚本主体控制方法调用:

简单的

package com.playground

switch( args ? args[ 0 ] : null ) {
    case 'a':
        hello()
        break
    case 'b':
        bye()
        break
    default:
        println 'Don\'t know what you mean'
}

void hello() {
    println 'Hello a!'
}

void bye() {
    println 'Goodbye a!'
}
梯度任务:

task runScript(type: JavaExec) {
    description 'Run Groovy script'
    main = 'com.playground.Simple'
    classpath = sourceSets.main.runtimeClasspath
    args 'a' // 'b'
}
产生

> Task :compileJava NO-SOURCE
> Task :compileGroovy UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes

> Task :runScript
Hello a!

您可以使用类加载器和反射在GradleJVM中运行该类

task invokeCompiledClass {
   dependsOn compileGroovy
   doLast {
      // invoke new Simple().foo("bar") 
      def urls = sourceSets.main.runtimeClasspath.collect { it.toURI().toURL() } 
      ClassLoader cl = new URLClassLoader(urls as URL[]) 
      Class type = cl.loadClass('com.playground.Simple') 
      def instance = type.newInstance()
      Method method = type.getMethod("foo", String.class)
      Object result = method.invoke(instance, "bar") 
   } 
} 

相关答案

您可以使用类加载器和反射在Gradle jvm中运行该类

task invokeCompiledClass {
   dependsOn compileGroovy
   doLast {
      // invoke new Simple().foo("bar") 
      def urls = sourceSets.main.runtimeClasspath.collect { it.toURI().toURL() } 
      ClassLoader cl = new URLClassLoader(urls as URL[]) 
      Class type = cl.loadClass('com.playground.Simple') 
      def instance = type.newInstance()
      Method method = type.getMethod("foo", String.class)
      Object result = method.invoke(instance, "bar") 
   } 
} 

相关答案

您的意思是想从
build.gradle
文件中调用
Simple
类中的方法?调用的方法是什么?您的意思是要从
build.gradle
文件中调用
Simple
类中的方法?这个方法叫什么?