BAEL-2899: project structure and example classes
This commit is contained in:
parent
1e562ed97c
commit
7c3082a093
132
java-groovy-joint/pom.xml
Normal file
132
java-groovy-joint/pom.xml
Normal file
@ -0,0 +1,132 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>java-groovy-joint</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>java-groovy-joint</name>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<commons-lang3.version>3.9</commons-lang3.version>
|
||||
<java.version>1.8</java.version>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<logback.version>1.2.3</logback.version>
|
||||
<groovy.version>2.5.7</groovy.version>
|
||||
</properties>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>bintray</id>
|
||||
<name>Groovy Bintray</name>
|
||||
<url>https://dl.bintray.com/groovy/maven</url>
|
||||
<releases>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<!-- Build an executable JAR -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.1.2</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.App</mainClass>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<configuration>
|
||||
<compilerId>groovy-eclipse-compiler</compilerId>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-eclipse-compiler</artifactId>
|
||||
<version>3.3.0-01</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-eclipse-batch</artifactId>
|
||||
<version>${groovy.version}-01</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<!-- Maven Assembly Plugin: needed to run the jar through command line -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<configuration>
|
||||
<!-- get all project dependencies -->
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<!-- MainClass in mainfest make a executable jar -->
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<!-- bind to the packaging phase -->
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung
|
||||
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class CalcMath {
|
||||
def log = LoggerFactory.getLogger(this.getClass())
|
||||
|
||||
def calcSum(x, y) {
|
||||
log.info "Executing $x + $y"
|
||||
x + y
|
||||
}
|
||||
|
||||
def calcSum2(x, y) {
|
||||
log.info "Executing $x + $y"
|
||||
// DANGER! This won't throw a compilation issue and fail only at runtime!!!
|
||||
calcSum3()
|
||||
log.info("Logging an undefined variable: $z")
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung
|
||||
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
abstract class CalcScript extends Script {
|
||||
def log = LoggerFactory.getLogger(this.getClass())
|
||||
|
||||
def calcSum(x, y) {
|
||||
log.info "Executing $x + $y"
|
||||
x + y
|
||||
}
|
||||
|
||||
def calcSum2(x, y) {
|
||||
log.info "Executing $x + $y"
|
||||
// DANGER! This won't throw a compilation issue and fail only at runtime!!!
|
||||
calcSum3()
|
||||
log.info("Logging an undefined variable: $z")
|
||||
}
|
||||
}
|
54
java-groovy-joint/src/main/java/com/baeldung/App.java
Normal file
54
java-groovy-joint/src/main/java/com/baeldung/App.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.baeldung;
|
||||
|
||||
import groovy.lang.Binding;
|
||||
import groovy.lang.GroovyClassLoader;
|
||||
import groovy.lang.GroovyShell;
|
||||
import org.codehaus.groovy.control.CompilerConfiguration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(App.class);
|
||||
private final GroovyClassLoader loader;
|
||||
private final GroovyShell shell;
|
||||
|
||||
private App() {
|
||||
loader = new GroovyClassLoader(this.getClass().getClassLoader());
|
||||
CompilerConfiguration config = new CompilerConfiguration();
|
||||
config.setScriptBaseClass("com.baeldung.CalcScript");
|
||||
shell = new GroovyShell(loader, new Binding(), config);
|
||||
}
|
||||
|
||||
private void runScript(int x, int y) {
|
||||
Object script = shell.parse(String.format("calcSum(%d,%d)", x, y));
|
||||
assert script instanceof CalcScript;
|
||||
Object result = ((CalcScript) script).run();
|
||||
LOG.info("Result of run() method is {}", result);
|
||||
|
||||
Object script2 = shell.parse("CalcScript");
|
||||
Object result2 = ((CalcScript) script2).calcSum(x + 7, y + 7);
|
||||
LOG.info("Result of calcSum() method is {}", result2);
|
||||
|
||||
}
|
||||
|
||||
private void runClass(int x, int y) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
|
||||
Class calcClass = loader.loadClass("com.baeldung.CalcMath");
|
||||
Object calc = calcClass.newInstance();
|
||||
assert calc instanceof CalcMath;
|
||||
|
||||
Object result = ((CalcMath) calc).calcSum(x, y);
|
||||
LOG.info("Result is {}", result);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
|
||||
App app = new App();
|
||||
LOG.info("Running a groovy script...");
|
||||
app.runScript(5, 10);
|
||||
LOG.info("Running a groovy class...");
|
||||
app.runClass(1, 3);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user