Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating Groovy with Java

Tags:

java

xml

groovy

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!

like image 545
Ken Avatar asked Feb 25 '11 22:02

Ken


People also ask

Can I run Groovy with Java?

With the GroovyClassLoader we can load Groovy scripts and run them in Java code. First we must create a new GroovyClassLoader and then parse a Groovy script. The script can be in a file, string or inputstream. Once the script is parsed we have a Class and we can make a new instance of this class.

Does Groovy need JDK or JRE?

The answer is yes. In fact, all groovy code compiles down to Java classes that run on the JRE. All you need is JRE 1.4 or higher and the groovy-all-*.

Which IDE is good for Groovy?

IntelliJ IDEA supports the latest stable version of Groovy and Groovy 4 syntax.


2 Answers

Generally speaking, there are two approaches for integrating Groovy with Java:

  1. Make the Java code compile and execute Groovy code at runtime
  2. 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.)

like image 103
Peter Niederwieser Avatar answered Sep 20 '22 04:09

Peter Niederwieser


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!

like image 22
TheKaptain Avatar answered Sep 21 '22 04:09

TheKaptain