Pages

21 April, 2013

Creating a Spring MDP Project Using Gradle

Summary


This post describes how to set up a new Java project using Gradle for use with Spring's Message Driven POJO functionality.

Tools Used


Create the Workspace and Source Folders


The first thing you should do is create your project structure. I'm following conventions here and suggest creating the following folder structure somewhere on your file system:

/Workspace <-- This is your Eclipse workspace
/Workspace/messaging <-- This is your project folder
/Workspace/messaging/src
/Workspace/messaging/src/main
/Workspace/messaging/src/main/java <-- Contains Java code
/Workspace/messaging/src/main/resources <-- Contains resources needed by the application (properties, xml, etc...)
/Workspace/messaging/src/main/webapp <-- Root folder for webapps. We are strictly worried about messaging so there will be no web content.
/Workspace/messaging/src/main/webapp/WEB-INF <-- Location of application context (web.xml)
/Workspace/messaging/src/main/webapp/WEB-INF/lib <-- Libraries that your application depends on and will be packaged with your web application.
/Workspace/messaging/src/test
/Workspace/messaging/src/test/java <-- Java test code (JUnit)
/Workspace/messaging/src/test/resources <-- Resources needed for testing.

Writing the Gradle Build Script


Now that the directory structure for the project is there, we need to start on the Gradle build script so Gradle can configure the project for us. Below is a simple gradle.build file for our messaging project. This file should be placed directly in the 'Workspace' directory (at the same level as the messaging folder).

//Define versions inside the 'ext' closure. This is an easy way to keep track
//of and manage the versions of your third-party dependencies.
ext{
 springVersion = '3.2.2.RELEASE'
 junitVersion = '4.+'
}

/*
* The subprojects closure can be used to define common configuration elements
* for all subprojects. Here we are applying the java and eclipse plugins as well
* as specifying that maven is our main repository. We also require that JUnit
* be added as a dependency so that we can test our code.
*/
subprojects{
 apply plugin: 'java'
 apply plugin: 'eclipse'
 
 repositories {
     mavenCentral()
 }

 dependencies {
  testCompile group: 'junit', name: 'junit', version: junitVersion
 }
}

/*
* Our first (and only a.t.m.) sub project. We are calling it messaging and
* applying a few extra plugins to it. The war plugin allows us to package the
* application as a web application archive (.war) file. The jetty plugin allows
* us to launch the messaging application in a Jetty v6 container that is included
* with the Gradle installation. The eclipse-wtp plugin allows us to configure
* project facets and components that we want applied to the project.
*/
project('messaging'){

 apply plugin: 'war'
 apply plugin: 'jetty'
 apply plugin: 'eclipse-wtp'
 
 //The eclipse closure lets us configure the eclipse project that Gradle
 //will configure for us.
 eclipse{
  //Target java version 1.7.
  jdt{
   sourceCompatibility=1.7
   targetCompatibility=1.7
  }
  //Add spring project nature. Only really useful if you are using
  //SpringSourceTool suite for development.
  project {
   natures 'org.springframework.ide.eclipse.core.springnature'
   buildCommand 'org.springframework.ide.eclipse.core.springbuilder'
  }
  //Add our Java, Dynamic Web Module, and Javascript facets to the project.
  wtp{ 
   facet {
    facet name: 'java', version: '1.7'
    facet name: 'jst.web', version: '3.0'
    facet name: 'wst.jsdt.web', version: '1.0'
   }
  }
 }
 
 //The messaging project will also have a dependency on spring-web and spring-jms.
 dependencies {
  compile group: 'org.springframework', name: 'spring-web', version: springVersion
  compile group: 'org.springframework', name: 'spring-jms', version: springVersion
 }
}

Since we are using a multi-project approach, we also need to include a settings.gradle file so Gradle recognizes our sub-project. This settings.gradle file should also be placed directly in the 'Workspace' directory:

include 'messaging'

Running Gradle


The next step in the setup of our messaging project is to run a Gradle task to setup our project so that Eclipse can read it. This is pretty simple, just open a terminal/command prompt and navigate to where you set up your workspace. Once there execute gradle :messaging:eclipse. This tells Gradle to execute the eclipse task (provided by the eclipse plugin) on the messaging project. As the task executes, you should see your dependencies being downloaded as well.
frank.r.greguska@MW713FDVLITTXR /cygdrive/c/Users/frank.r.greguska/Documents/Blog-Workspace
$ gradle :messaging:eclipse
:messaging:eclipseClasspath
Download http://repo1.maven.org/maven2/org/springframework/spring-web/3.2.2.RELEASE/spring-web-3.2.2.RELEASE.pom
Download http://repo1.maven.org/maven2/org/springframework/spring-web/3.2.2.RELEASE/spring-web-3.2.2.RELEASE-sources.jar
Download http://repo1.maven.org/maven2/org/springframework/spring-web/3.2.2.RELEASE/spring-web-3.2.2.RELEASE.jar
:messaging:eclipseJdt
:messaging:eclipseProject
:messaging:eclipseWtpComponent
:messaging:eclipseWtpFacet
:messaging:eclipseWtp
:messaging:eclipse

BUILD SUCCESSFUL

Total time: 7.28 secs

That's all there is to it! The next step is to open up Eclipse and create the project.

Creating the Eclipse Project


Launch Eclipse and point it to the location of your new Workspace on your filesystem. Once it starts up, select File -> New -> Java Project. When the New Project dialog box appears, enter messaging as the name of the project. Eclipse will let you know that it will configure the new project based on existing settings (since we already have created the 'messaging' folder). Click on finish and your new messaging project should be ready to go! There is no further configuration needed for the project in Eclipse because it was all done in Gradle.

Conclusion


This post walked through how to create a new project using Gradle and Eclipse. Future posts will go into detail about configuring the project for a Spring Message Driven POJO, how to get the project to run in a local Jetty container, looking up queues from JNDI, and more!