Saturday, 8 June 2024

Executing Groovy Scripts from Java: A Comprehensive Guide

In this post, you are going to learn, how to run Groovy scripts in Java.

Groovy

Groovy is an object-oriented programming language for the Java platform. It's known for its ease of use, conciseness, and powerful features. Groovy can easily integrated with Java and allow you to leverage existing Java libraries and frameworks.

 

Use cases of running Groovy Script in Java

1.   Add dynamic scripting capabilities to your Java application.

2.   Use Groovy for tasks that require scripting flexibility and ease.

3.   Leverage Groovy’s concise syntax for rapid prototyping and development.

 

How to run Groovy Script in Java?

Below snippet execute the groovy script from Java.

GroovyShell shell = new GroovyShell();
String script = "println 'Hello, Groovy from Java!'";
shell.evaluate(script);

 

GroovyShell: A simple scripting engine to run Groovy scripts.

evaluate(): A method to execute a script passed as a string.

 

Find the below working application.

 

Step 1: Create new maven project “run-groovy-in-java”.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>run-groovy-in-java</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<jdk.version>17</jdk.version>
		<release.version>17</release.version>
	</properties>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
		<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy-all</artifactId>
			<version>3.0.21</version>
			<type>pom</type>
		</dependency>

	</dependencies>
	
	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.sample.app.RunGroovyScript</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Step 3: Define RunGroovyScript class.

 

RunGroovyScript.java

package com.sample.app;

import groovy.lang.GroovyShell;

public class RunGroovyScript {
    public static void main(String[] args) {
        GroovyShell shell = new GroovyShell();
        String script = "println 'Hello, Groovy from Java!'";
        shell.evaluate(script);
    }
}

Total project structure looks like below.

 



Build the Artifact

Go to the folder, where pom.xml is located and execute the command ‘mvn package’.

 

Upon successful execution of ‘mvn package’ command, you can see a jar file run-groovy-in-java-0.0.1-SNAPSHOT.jar in the target folder.

 

Run the Application

Execute below command from the terminal to run the application.

java -jar ./target/run-groovy-in-java-0.0.1-SNAPSHOT.jar

$ java -jar ./target/run-groovy-in-java-0.0.1-SNAPSHOT.jar 
Hello, Groovy from Java!

You can download this application from this link.

Previous                                                    Next                                                    Home

No comments:

Post a Comment