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.



1 comment:

  1. Great tutorial!
    If maven was installed before, you may want to do an extra step before the step "Convert the project to a Gradle project":
    right click project -> maven -> Disable Maven Nature.
    Otherwise maven will also keep part of the dependencies, and these two may have dependency conflict.

    ReplyDelete