Round hill of the apples - Groovy, Grails, Griffon and a hint of Java

  • Blog
  • About
  • Contact us
 

Adding Groovy to a Spring MVC Maven project

04/04/2012

10 Comments

 
I've been working on a Spring MVC project for the past year or so. Our stack basically goes as follows: Spring MVC, Hibernate, MySql. We also use JQuery, JMS and Solr.

Whilst working with Spring is great, Java was just becoming too verbose, so I decided to make an effort to integrate Groovy.

Turns out its pretty easy..

First add the following dependency to your Maven pom.xml:


<!-- Groovy -->
            <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                <version>1.8.6</version>
            </dependency>



Next modify the maven compiler plugin:


                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <compilerId>groovy-eclipse-compiler</compilerId>
                        <verbose>false</verbose>
                    </configuration>
                    <dependencies>
                      <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>2.6.0-01</version>
                      </dependency>
                      <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>1.8.6-01</version>
                      </dependency>
                    </dependencies>
                </plugin>

At that's basically it. If you are working on a Spring MVC project like I am, you can now create .groovy files in src/main/java or src/main/groovy. In addition you can also annotate them with @Service etc and @Autowire them into other Java or Groovy classes.

One problem I did have was autowiring a groovy bean that was annotated with @Transactional into another. It turns out the probject was because the class did not have an interface and this causes proxy problems for Spring. The following change to the bean solved the problem:

@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class UserNotificationsScheduled {
..
}



 


Comments

Phoenix2life link
04/05/2012 15:50

Good Blog post. I too ran in the problem with autowiring a groovy bean that was annotated with @Transactional. Referring to your blog post, I have used proxyMode scope and could resolve the issue. Thanks.

BTW with Spring Tool Suite, I can compile groovy with gmaven

<!-- Setup Groovy Compiler Plug In -->
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>${gmaven.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<providerSelection>1.7</providerSelection>
</configuration>
</execution>
</executions>
</plugin>

Wondering what would be advantageous to use maven groovy eclipse compiler or native gMaven plugin ? :D)

Reply
Ralph Schaer
04/12/2012 06:35

Here is a blog post about benefits and limitations of gmaven and groovy eclipse compiler

http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven#Groovy-EclipsecompilerpluginforMaven-WhyanotherGroovycompilerforMavenWhataboutGMaven

Reply
Mark
04/14/2012 07:50

Why do you have eclipse compiler? Does this only work if you are using Eclipse? Can you build from the command line with the Pom changes?

Reply
Round Hill
04/14/2012 09:12

actually I'm using IntelliJ, the groovy eclipse compiler allows you to mix java and groovy in same directories in a simple fashion and then build with maven

so yes, it builds from command line via a maven build

http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven

Reply
Tool Balancing link
06/12/2012 08:40

Nice post. I read your post. It’s very simple and informatics. Thank you for sharing..............

Reply
Delila Newton link
06/23/2012 08:43

I thoroughly enjoyed this blog and created a Weebly account too.

Reply
Minecraft link
07/14/2012 07:31

This really is such a great post, and was thinking significantly exactly the same myself. Yet another excellent update.

Reply
Serwis laptopów wrocław link
07/17/2012 16:31

I adore gathering useful information, this post has got me even more information! .

Reply
look here link
04/05/2013 05:07

This post looks quite interesting! I was searching to find how to mix java and groovy in same directories and now everything is clear. It looks simple but interesting. Hope you will add more contents regarding innovative ideas regarding websites!

Reply
we
04/05/2013 05:42

According to Spring 4 roadmap, all Spring MVC webapps will use Groovy from then on

fyi - If you are using Gradle gradle has its own plugin for doing this although java and groovy go in different folders.

Reply



Leave a Reply

    Author

    Groovy & Java developer at FunnelScope.com

    Archives

    April 2012

    Categories

    All
    Groovy Collections
    Spring Groovy Dependency Injection

    RSS Feed


Create a free website with Weebly