<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>3.0.9</version>
</dependency>
</dependencies>
groovy
dependencies {
implementation 'org.codehaus.groovy:groovy-all:3.0.9'
}
groovy
def addNumbers(int a, int b) {
return a + b
}
def result = addNumbers(5, 3)
import groovy.lang.GroovyShell;
import groovy.lang.Script;
public class GroovyIntegration {
public static void main(String[] args) {
String groovyScript = "def addNumbers(int a, int b) {
" +
" return a + b
" +
"}
" +
"def result = addNumbers(5, 3)
" +
GroovyShell shell = new GroovyShell();
Script script = shell.parse(groovyScript);
script.run();
}
}
import groovy.lang.GroovyShell;
import groovy.lang.Script;
public class GroovyIntegration {
public static void main(String[] args) {
String groovyScript = "import com.example.MyClass
" +
"def myObject = new MyClass()
" +
"myObject.doSomething()";
GroovyShell shell = new GroovyShell();
shell.getClassLoader().addClasspath("path/to/your/library.jar");
Script script = shell.parse(groovyScript);
script.run();
}
}