Saturday, October 27, 2012

How to create a Spring MVC Project in Eclipse Juno with STS & Gradle


In Eclipse Juno with Gradle & STS Plugins installed, create a new Project starting with the
Spring Template Project



Then choose Spring MVC Project



Click Next and complete the project creation

Right-click on the project



Note that Gradle and Spring tooling are available and Spring Project Nature is enabled.
Also, the Project opens up in the Spring perspective by default.

Convert the project to a Gradle project



Now you can manage the project with Gradle



Now you need to put a build.gradle file at the root of the project directory and click on Refresh Dependencies for the Gradle magic to take effect!

Here's a working build.gradle that takes care of all the required dependencies for Spring MVC, Spring Security, Apache Tiles, slf4j, JEE, JSTL etc.


apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility=1.7

targetCompatibility=1.7

description='myproject'

group = 'com.myco'
version = ''

repositories {

    mavenRepo url: 'http://maven.springframework.org/release'
    mavenRepo url: 'http://maven.springframework.org/snapshot'
    mavenCentral()
    mavenRepo url: 'http://maven.springframework.org/milestone'
    mavenRepo url: 'https://repository.jboss.org/nexus/content/repositories/releases/'
    mavenRepo url: 'http://download.java.net/maven/glassfish/org/glassfish/'
    mavenRepo url: 'http://snapshots.repository.codehaus.org'
}

ext.springVersion='3.1.2.RELEASE'
ext.springSecurityVersion='3.1.2.RELEASE'
ext.tilesVersion='2.2.2'

dependencies() {

  compile("org.springframework:spring-orm:$springVersion")
  compile("org.springframework:spring-aop:$springVersion")
  compile("org.springframework:spring-webmvc:$springVersion")
  compile("org.springframework:spring-oxm:$springVersion")
 
compile "org.springframework.security:spring-security-core:$springSecurityVersion"
compile "org.springframework.security:spring-security-web:$springSecurityVersion"
compile "org.springframework.security:spring-security-config:$springSecurityVersion"
compile "org.springframework.security:spring-security-taglibs:$springSecurityVersion"

    compile 'org.slf4j:slf4j-api:1.7.1'

    compile 'ch.qos.logback:logback-classic:1.0.6'

   //prevent slf4j issues by exclusions - reason: tiles includes an older version of a slf4j binding

    compile("org.apache.tiles:tiles-template:$tilesVersion")
    {
    exclude group: 'org.slf4j', module: 'slf4j-jdk14'
    exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
    }
    compile("org.apache.tiles:tiles-servlet:$tilesVersion")
    {
    exclude group: 'org.slf4j', module: 'slf4j-jdk14'
    exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
    }
    compile("org.apache.tiles:tiles-jsp:$tilesVersion")
    {
    exclude group: 'org.slf4j', module: 'slf4j-jdk14'
    exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
    }
    compile("org.apache.tiles:tiles-api:$tilesVersion")
    {
    exclude group: 'org.slf4j', module: 'slf4j-jdk14'
    exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
    }
     compile("org.apache.tiles:tiles-core:$tilesVersion")
    {
    exclude group: 'org.slf4j', module: 'slf4j-jdk14'
    exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
    }        

    compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.9'    

    compile "jstl:jstl:1.2"

    providedCompile 'javax.servlet:javax.servlet-api:3.0.1'

    
    providedCompile 'javax:javaee-api:6.0'
    
}


Using the above build.gradle and after "Refresh Dependencies" you will see the following Gradle Dependencies in your project. Gradle downloads and caches jars in USER_HOME/.gradle folder.  



To build a Gradle project Run As | Gradle Build ...



Choose from all tasks available and order their sequence.  Tasks "classes" and "war" are sufficient to build a war.



How to setup Eclipse Juno with Gradle and Spring Tool Suite (STS)


Download & install Java JDK 1.7 64bit
http://www.oracle.com/technetwork/java/javase/downloads/index.html

On Windows, setup your SYSTEM environment variables
CLASSPATH=.;C:\Program Files\Java\jdk1.7.0_05\bin
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_05
Path=C:\Program Files\Java\jdk1.7.0_05\bin;

Download Eclipse Juno
http://www.eclipse.org/downloads/

Unzip eclipse-jee-juno-SR1-win32-x86_64.zip

Run C:\\eclipse-jee-juno-SR1-win32-x86_64\eclipse\eclipse.exe

From the Eclipse menu select Eclipse Marketplace


Search for Gradle and click on Install


Search for STS and click on Install


Your Eclipse Juno is now setup to work with Gradle and STS