Compile Groovy code in Java application using Groovy Eclipse Maven plugin

Sometimes when working with Java applications, you will need to use Groovy code to solve your problems. In these cases, you’ll need to compile Groovy code so it can run with the application’s Java code. In this tutorial, I will show you how to install and use the Groovy Eclipse Maven plugin to do this.

First, I will create a new Maven project as an example:

Compile Groovy code in Java application using Groovy Eclipse Maven plugin

To write Groovy code in a Java application, you should create 2 new directories, src/main/groovy for the application’s code and src/test/groovy to test Groovy code:

Compile Groovy code in Java application using Groovy Eclipse Maven plugin

and install Groovy Development Tools from Eclipse Marketplace as follow:

Compile Groovy code in Java application using Groovy Eclipse Maven plugin

To compile Groovy code, we need to change the default compiler that the Maven Compiler plugin uses (javac) to the Groovy Eclipse Compiler:

and declare Groovy dependency as follows:

Now, if you write a class with Groovy in src/main/groovy directory:

Compile Groovy code in Java application using Groovy Eclipse Maven plugin

Then you can use this HelloWorld class in the Java class:

Compile Groovy code in Java application using Groovy Eclipse Maven plugin

Run the program:

Compile Groovy code in Java application using Groovy Eclipse Maven plugin

Compile and build source code:

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment