如何在脚本中的多个文件中使用多个类?

How to use multiple classes in multiple files in scripts?

我需要制作一个不需要编译的独立 Groovy 脚本和 运行 没有安装 Groovy 的脚本。它运行良好,但无法识别主脚本以外的任何其他脚本。

我的文件夹结构如下:

libs\
    groovy-all-2.4.3.jar
    ivy-2.4.0.jar
src\
    makeRelease.groovy
    ReleaseHelper.groovy

我从 src 文件夹以这种方式启动脚本:

java -cp "../libs/*" makeRelease.groovy

makeRelease 看起来像这样:

public class makeRelease {
    public static void main(String... args) {
         new ReleaseHelper()
         ...
    }
}

当运行这是输出:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
src\makeRelease.groovy: 5: unable to resolve class ReleaseHelper

如何在此类可移植脚本中包含其他 类(驻留在单独的文件中)?

我认为这比你想象的要容易:

libs\
    groovy-all-2.4.3.jar
src\
    main.groovy
    Greeter.groovy

其中main.groovy

public class Main {
    public static void main(args) {
        println 'Main script starting...'
        def greeter = new Greeter()
        greeter.sayHello()
    }
}

Greeter.groovy

class Greeter {
    def sayHello() {
        println 'Hello!'
    }
}

只需将 类 位于单独文件中的文件夹添加到类路径中:

java -cp .;..\libs\groovy-all-2.4.3.jar groovy.ui.GroovyMain main.groovy

以上结果:

Main script starting...
Hello!