Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kenneth A. Kousen - Making Java Groovy - 2014.pdf
Скачиваний:
50
Добавлен:
19.03.2016
Размер:
15.36 Mб
Скачать

Summary

63

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 problems, 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 projects 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 to be. The developers of Groovy have worked hard on the cross-compilation issue for years. 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 compiler handle both codebases. Groovy knows all about Java and is quite capable of handling 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 book 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.

3.3Summary

This chapter is about basic Groovy / Java integration, regardless of use case. After reviewing all the different ways to call Groovy from Java, from the JSR-223 ScriptEngine to the GroovyShell and Eval classes in Groovy, I switched to the easy way, which is to put Groovy in a class and use it like any other library class. This easy blend of Java and Groovy will be used from now on.

Next I reviewed many ways that Groovy can help Java at the basic level, from POJO enhancements to AST transformations to building XML and more. I’ll use these techniques in future chapters wherever they can help. I’ll also review other helpful techniques along the way, though these are most of the major ones.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]