Integrating Groovy with Java
I am currently working on integrating Groovy with an existing Java project. The Groovy script basically parses XML into objects and then will use existing DAO's to save, update, etc. I have 2 projects. One groovy. One java. Using Maven (I used the gmaven plugin), I build Java and then I build the Groovy project.
I would like to be able to call Groovy scripts from the Java project and vice versa if the case need be. I am sure there is a way to do this, but I am unable to do this.
For example My Java classes are
State(1 Field) StateDAO
The Groovy script to be called basically parses an XML document and then populates a table using the DAO with the value,s and for now all I would like it to do is post it to a database.
Please let me know if you have any suggestions. Thanks!
Generally speaking, there are two approaches for integrating Groovy with Java:
- Make the Java code compile and execute Groovy code at runtime
- Compile and package Groovy code as part of your build, just like you do for Java code
Use 1. if you need a "scripting" solution and the Groovy code to be executed is only known at runtime. For example, the code could be loaded from a database, or entered in a GUI screen. If you simply want to make your life easier by writing some parts of your application in Groovy, go with 2. Two typical examples where this can quickly pay off are XML parsing and testing (of course there are many more).
Assuming you are more interested in 2., you can either compile Groovy and Java separately or together. If you compile them separately, static references can only go in one direction, just as if you had two Java modules. If you compile them together, you can mix Groovy and Java code arbitrarily, as if it was all Java code in the same module.
Compiling Java and Groovy code together is made possible by a Groovy compiler feature called joint compilation. Unfortunately, GMaven has serious problems with joint compilation, and there is no sign that this will change any time soon. For smaller projects you might get away with it, but for larger projects it will bite you (I've been there). There are solutions, but they don't come for free. If you are open to (G)Maven alternatives, consider switching to Gradle, which has much better Groovy support. (Disclaimer: I'm one of the developers of Gradle.)
If you decide to stick to GMaven, make sure to get its configuration right. Almost every project I see fails at this step, often without realizing it. The GMaven documentation is outdated, but the Groovy mailing list contains several posts on the topic. You can also copy the configuration from Spock (one of my own projects).
To enable GMaven joint compilation, add the generateStubs goal. As long as your project compiles without this goal, leave it out. There is also a generateTestStubs goal, but it is rarely needed.
This is the short story. For the long story, check out the upcoming Manning book Making Java Groovy. (I'm not affiliated with the author.)
For Peter's #1 suggestion above, I put together a collection of examples using Java to run Groovy scripts in a blog post awhile back that comes along with code samples on github. I can also back him up on his comments regarding GMaven and joint compilation - it works, but can end up causing more problems than it's worth. +1 on considering Gradle as well. While most of my experience with using it for production is with Java source and Groovy test code, it has so far worked flawlessly for me. Best of luck!