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:
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:
and install Groovy Development Tools from Eclipse Marketplace as follow:
To compile Groovy code, we need to change the default compiler that the Maven Compiler plugin uses (javac) to the Groovy Eclipse Compiler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <compilerId>groovy-eclipse-compiler</compilerId> <source>1.8</source> <target>1.8</target> </configuration> <dependencies> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-compiler</artifactId> <version>3.4.0-01</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-batch</artifactId> <version>2.5.8-01</version> </dependency> </dependencies> </plugin> |
and declare Groovy dependency as follows:
1 2 3 4 5 |
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy</artifactId> <version>2.5.8</version> </dependency> |
Now, if you write a class with Groovy in src/main/groovy directory:
Then you can use this HelloWorld class in the Java class:
Run the program:
Compile and build source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
[INFO] Scanning for projects... [INFO] [INFO] ---------------< com.huongdanjava:maven-groovy-eclipse >---------------- [INFO] Building maven-groovy-eclipse 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-groovy-eclipse --- [INFO] Deleting /Users/khanh/Documents/workspace-spring-tool-suite-4-4.3.0.RELEASE/maven-groovy-eclipse/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-groovy-eclipse --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-groovy-eclipse --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Using Groovy-Eclipse compiler to compile both Java and Groovy files WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/Users/khanh/.m2/repository/org/codehaus/groovy/groovy-eclipse-batch/2.5.8-01/groovy-eclipse-batch-2.5.8-01.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int) WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-groovy-eclipse --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-groovy-eclipse --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-groovy-eclipse --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-groovy-eclipse --- [INFO] Building jar: /Users/khanh/Documents/workspace-spring-tool-suite-4-4.3.0.RELEASE/maven-groovy-eclipse/target/maven-groovy-eclipse-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-groovy-eclipse --- [INFO] Installing /Users/khanh/Documents/workspace-spring-tool-suite-4-4.3.0.RELEASE/maven-groovy-eclipse/target/maven-groovy-eclipse-0.0.1-SNAPSHOT.jar to /Users/khanh/.m2/repository/com/huongdanjava/maven-groovy-eclipse/0.0.1-SNAPSHOT/maven-groovy-eclipse-0.0.1-SNAPSHOT.jar [INFO] Installing /Users/khanh/Documents/workspace-spring-tool-suite-4-4.3.0.RELEASE/maven-groovy-eclipse/pom.xml to /Users/khanh/.m2/repository/com/huongdanjava/maven-groovy-eclipse/0.0.1-SNAPSHOT/maven-groovy-eclipse-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.694 s [INFO] Finished at: 2019-08-29T22:25:32+07:00 [INFO] ------------------------------------------------------------------------ |
mandzur2
very good groovy builder tutorial
but try to run from cmd your jar
there is thrown
Exception in thread “main” java.lang.NoClassDefFoundError: