1

我想从另一个 groovy 脚本中调用一个 groovy 脚本......任何人都可以尽快帮助我......

例子 :

A类有一些代码,它应该从B调用

 class A{
   static main(args){

     println "Hello.. calling A Class"
   }
 }

我想创建一个像 B.groovy 这样的新类

class B{
  static main(args){

    // I need code for this to call A.groovy
  }
}
4

2 回答 2

3

将以下内容放在脚本顶部将加载 groovy 文件的内容。

evaluate(new File("/path/to/script/MyScript.groovy"))

如果你需要经常做这样的事情,你也可以将它添加到 groovy 类路径中。希望这可以帮助。

此外,如果您需要从脚本运行其他脚本,您可以执行以下操作...

def script = new GroovyShell();
def args = ['Hello World'];
script.run(new File("/path/to/script/MyScript.groovy"), args as String[]);
于 2013-05-17T10:14:23.950 回答
0

Too late for the party (any beer for me?) but here I´ll show you 2 more flavors:

1) Remember the concept of Java´s CLASSPATH? That aplies to Groovy (because Groovy is Java!):

"The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes.”</p>

In order to run the script B.groovy you have to inform the location of the A.groovy (A class):

groovy –cp c:\groovy\yourscripts c:\groovy\scripts\B.groovy

The command above is telling the runtime to look in the c:\groovy\yourscripts folder because there is where we have our classes and we need them to run the B.groovy script successfully.

2) Use the GroovyClassLoader to load your scripts at runtime and using the code!

Both ways solve your needs. Now the next question is when to use each?

于 2013-11-07T00:27:47.440 回答