先进入到 Groovy 代码所在目录 执行
cd Y:\002_WorkSpace\003_IDEA\Groovy_Demo2\src\main\groovy
命令 ( 默认也在这个目录 ) , 然后执行
groovyc -d classes MyASTTransformation.groovy
命令 , 编译 ASTTransformation 实现类 ; 编译后会在 Y:\002_WorkSpace\003_IDEA\Groovy_Demo2\src\main\groovy\classes
目录生成 MyASTTransformation.class 文件 ;
将编译后的 Y:\002_WorkSpace\003_IDEA\Groovy_Demo2\src\main\groovy\classes\MyASTTransformation.class
文件进行打包 , 执行
jar -cf test.jar -C classes . -C resources .
命令 , 在 Y:\002_WorkSpace\003_IDEA\Groovy_Demo2\src\main\groovy
目录下生成了 test.jar 文件 ;
Groovy.groovy 脚本的内容如下 , 其中定义了一个 Student 类 ,
class Student{
def name
def hello(){
println "hello"
}
}
def student = new Student()
执行
groovy -classpath test.jar Groovy.groovy
命令 , 执行 Groovy.groovy
脚本 , 执行时先进行编译 , 然后再执行 , test.jar 中包含了 ASTTransformation , 会在编译时处理 Groovy.groovy
脚本中的相关类 ;
执行结果如下 :
[org.codehaus.groovy.ast.ModuleNode@2609b277]
org.codehaus.groovy.control.SourceUnit@1fd14d74