Is it possible to use Groovy with Spring Roo? - groovy

Is there a way to use Groovy in a Spring Roo project?
I'm using STS (Eclipse) and I'd like to be able to write some code in Groovy
and use Spock framework for unit testing.
But it seems, there are some problems with AspectJ and Groovy intergation.
Has anyone successfully used Roo and Groovy together?
Thanks.

Unfortunately you cannot mix those two languages in the same project.
When you installed Groovy-Eclipse you install a patched eclipse java compiler that is then able to build java and groovy code. When you install AspectJ you get an alternative compiler that can build java and AspectJ source (your Roo projects are AspectJ projects).
In a Roo project it will be using the AspectJ compiler and not the patched java compiler installed via Groovy-Eclipse, so it won't understand groovy code.
The proper solution is to patch AspectJ in the same way Groovy-Eclipse is done so AspectJ can build groovy+java+aspectj but we still haven't gotten around to that.
The easiest thing for you to do is split the tests into a separate Groovy test project that depends on the Roo project containing the main sources. That should work.

Related

How to use Mockito in a simple java project without Maven and Gradle

Can we use the Mockito framework in a simple Java project without maven and Gradle? I saw many youtube videos but all of them use either Maven or Gradle. I do not understand why they do not use it in a simple java project.
Can we use the Mockito framework in a simple Java project without maven and Gradle? I saw many youtube videos but all of them use either Maven or Gradle. I do not understand why they do not use it in a simple java project.
Yes. People do this every day as this. It's the 'normal' way to use Mockito.
Here is an example project that uses an older version of mockito, but the concept is still the same: https://java2blog.com/mockito-junit/

Running Spring Boot application with Jersey and Groovy in IntellJ

I am using template Spring Boot application with servlets. When I add to the application latest Jersey (for jax-rs) and then add Groovy framework to the application (only for testing) I start having problems running the application from within IntelliJ.
It seems that some libraries on the Groovy classpath collide with the libraries coming from Jersey (servlet-api and ASM) but I am not yet 100% sure this is the problem. For sure the classpath IntelliJ builds is different from maven's
seems to be connected to the tricks that the spring-boot maven plug-in does, but that's only a suspicion. running via java -jar fails, while running via mvn spring-boot:run works (from the command line as well from IntelliJ).
I get this exception:
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
looks like part of the problem was how I set-up my gmaven.
when I set it up like in this post https://stackoverflow.com/a/19539452/672738 the problem is gone and my application happily boots with java -jar in IntelliJ
I just solve this problem.
Groovy also includes the servlet-api lib which is conflict with the lib imported by spring boot.
You can just exclude the servlet-api from groovy.

worklight adapters and Groovy

Has anyone had success implementing IBM Worklight 5.0.6 Adapters using the Groovy language? Ie. Instead of calling a Java class from the JS adapter call a Groovy class. We have tried this and it seems to work most of the time but randomly we will get strange errors when invoking an adapter procedure. Ie.
Failed to create DGM method proxy : java.lang.NoSuchMethodException: org.codehaus.groovy.runtime.dgm$24.<init>(java.lang.String, org.codehaus.groovy.reflection.CachedClass, java.lang.Class, [Ljava.lang.Class;)
FWLSE0101E: Caused by: java.lang.NoSuchMethodException: org.codehaus.groovy.runtime.dgm$24.<init>(java.lang.String, org.codehaus.groovy.reflection.CachedClass, java.lang.Class, [Ljava.lang.Class;)
Unit tests executing the Groovy code run fine within Eclipse and the Groovy code seems to be compiling fine down to .class files and included in the adapter package. We're wondering if this is an issue with Groovy itself or invoking Groovy within the Worklight server container. I believe Worklight is using Rhino behind the scenes which may also be a culprit?
We are using Groovy 2.1.5 and the Eclipse plugin 2.8
did you open the .war and check if all compiled classes and libraries are actually in there?
We had the case quite often that the WL Eclipse Plugin/Build did not compile and package our Java Classes for the Adapter in the "server" folder ... they were missing in the .war file that we deployed and we got all sorts of Rhino, ClassNotFound etc. errors.
In addition to previous answer. You can always force a rebuild by selecting the project in the Enterprise Explorer or Navigator views, then select menu Project -> Clean -> Clean Projects Selected Below (the only choice should be your project).
This will clean the binaries, then rebuild your entire project.

Can a groovy code be compiled to run in JRE?

I am new to groovy and I cannot understand, if it is possible to compile a groovy program, so it runs at all computers, were the JRE is installed.
The application I am developing has to run on any computer with JRE 1.5. Is it possible to start using groovy and maintain this flexibility? With JRE 1.6?
I have heard about the library groovy-all-VERSION.jar. Is this the one required library to be shipped with my application?
The answer is yes. In fact, all groovy code compiles down to Java classes that run on the JRE. All you need is JRE 1.4 or higher and the groovy-all-*.jar on the classpath of your application.
Since you are looking to support JRE 1.5 or higher, make sure your source compatibility is set on your compiler to this level.
There are a few options for compiling your groovy code. Groovyc (Ant Task), GMaven (Maven) and Gradle are all options.
Another option you have is to 'not' compile your groovy code. The groovy distribution only requires the JRE to be installed. You can ship your application as a set of scripts that can simply be run using the groovy install. It depends on how sensitive your source code is.
The short answer is yes. How you do this depends on your build system. I do all my development in eclipse, right click my project, select export, select runnable jar file, and all the required librarys are exported in the jar file. I can then run this file on a machine with out Groovy installed. I know build systems like Maven support Groovy but don't know the details on how they do it or how good there support is. According to this question Java 1.4 or above is fine. When looking at the "Setting up your Java environment" section of the initial tutorial it looks like you need Java 1.5 installed.

How to use groovy inside a Eclipse RCP project?

I have a Eclipse RCP project, and I want to use Groovy inside it. what I try to do is :
1) add "Groovy Nature"
2) create Groovy files under the "src" folder
3) call the groovy class from the "View"'s createPartControl() method.
But I got the “java.lang.ClassNotFoundException: fly.island.jface.Test”, and the "fly.island.jface.Test" is a groovy class.
What should I do to use groovy inside the Eclipse RCP project?
Use the latest version of Groovy-Eclipse 2.1.0 (or one of the dev builds). Update site is here:
http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.6/
Additionally, there are some steps you need to do to compile the groovy plugins when you the PDE build. I'd recommend reading:
http://contraptionsforprogramming.blogspot.com/2010/08/groovy-pde-redux.html
Essentially, you need to include the org.codehaus.groovy bundle in your rcp app, and then you need to make some changes to the build.properties file of your Groovy plugins (explained in detail on the blog post).
Are you using the latest Version of eclipse (3.6) and the groovy plugin? I recall occasionally having problems with the Java and Groovy compilers not finding each other's classes (in the first version of the plugin, it took a lot of fiddling and luck to get it to work at all). The most recent version seems to be pretty good in that regard, though I've been using it only for a few days myself.

Resources