I have written a Groovy AST Transformation which only runs for me when Grails auto-reloads the class it is to be applied to. If I clean the project and start the application using run-app, the AST transformation does not run. Touching the class so that grails auto-reloads results in the transformation running.
The annotation and ASTTransformation implementation are groovy classes located in the src/groovy directory in my Grails application. The annotation is used on domain classes, written in groovy in the domain directory.
Is it possible this is caused by the order the groovy files get compiled or when they are loaded by the classloader? If so, how do I ensure my ast transforamtion is compiled/loaded before the domain classes?
The annotation:
@Target([ElementType.TYPE])
@Retention(RetentionPolicy.RUNTIME)
@GroovyASTTransformationClass(["com.abc.annotation.SecuredObjectASTTransformation"])
public @interface SecuredObject {
}
The ASTTransforamtion implementation:
@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
class SecuredObjectASTTransformation implements ASTTransformation {
@Override
public void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
// add some new properties...
}
}
The Grails version is 2.1.0.
The AST Transformations need to be compiled before your project code. The simplest way to do this is to hook into the grails compile event with a script. Check out this blog post for how to create a script with new ant task to precompile source in src/ast folder. http://reinhard-seiler.blogspot.com.au/2011/09/grails-with-ats-transformation-tutorial.html
If you only have a few AST Transformations then this is by far the best approach. Creating a plugin or separate project with compiled jar is too much work for my needs.