Java Reference
In-Depth Information
8 Re-inventing the flat tire is what happens when you try to re-invent the wheel and get it wrong.
Compile with groovyc
Whenever you mix Java and Groovy, compile everything with groovyc . Let groovyc
handle all the cross-compiler issues.
In the next chapter I'll look at some of the ways Groovy improves Java.
Don't separate Groovy and Java classes
The natural tendency when using two different languages is to separate the two codebases
and compile them independently. With Groovy and Java that can lead to all sorts of prob-
lems, especially when cyclic dependencies are involved (in other words, Java class A uses
Groovy class B , which invokes another method from Java class A , and so on). Maven pro-
jects in particular lead you down this path, because their default layouts naturally suggest
putting Java code under src/main/java and Groovy code under src/main/groovy. The idea
then is to use javac to compile the Java code and groovyc to compile the Groovy code.
Although you probably can get that to work, it makes life much more difficult than it needs
tobe.ThedevelopersofGroovyhaveworkedhardonthecross-compilationissueforyears.
It's better for us, as users of both languages, to take advantage of their progress.
The simplest way to compile Groovy and Java in the same project is to let the groovyc
compilerhandlebothcodebases.GroovyknowsallaboutJavaandisquitecapableofhand-
ling it. Any compiler flags you would normally send to javac work just fine in groovyc
as well. This is actually a good general principle.
In the projects in this topic I'll let groovyc do all the work. I'll show specific examples
of this in chapter 5 , but you can safely assume I'm using groovyc throughout.
 
Search WWH ::




Custom Search