Thursday, 10 September 2015

A Sample Spring MVC Project via Ant Ivy - Maven - Gradle Builds

Objective

The objective of this post is to show you a sample Spring MVC Eclipse Project using three popular build tools Apache Ivy, Maven and Gradle.

Environment

  • Eclipse Java EE IDE
  • Postgres DB
  • Ant, Ivy, Maven and Gradle installed (Eclipse plugins)
  • Apache Tomcat 8 Server

Pre-requisite

  • Create a new database in postgres with the name 'fulltextsearch' (you may change this name but make sure you update the same in src/main/resources/application.properties file). Since we are using hibernate's ddl auto generation feature, no need to create table manually it will be created automatically when the application is deployed and run.
  • For Ant Ivy to work together you will have to copy the ivy.jar to Ant's lib folder. Also do not forget to update/refresh the Ant classpath in Eclipse to make sure ivy.jar is added:

Ant Ivy Project

  1. Copy the source files from following location to your local disk: https://github.com/mchopker/myprojects/tree/master/SpringMVCProjectWithAntIvy
  2. Import this as existing project into your Eclipse IDE.
  3. Run the Ant build.xml via Eclipse.
  4. The build will create a traget folder under which you will find 'springmvcproject.war' file created.
  5. Deploy it into your local Tomcat server (put under webapps folder and start the server).
  6. Open a browser and hit the URL: http://localhost:8080/springmvcproject/

Maven Project

  1. Copy the source files from following location to your local disk: https://github.com/mchopker/myprojects/tree/master/SpringMVCProjectWithMaven
  2. Import this as existing Maven project into your Eclipse IDE.
  3. Run the Maven build ( build.xml via Eclipse.
  4. The build will create a traget folder under which you will find 'springmvcproject.war' file created.
  5. Deploy it into your local Tomcat server (put under webapps folder and start the server).
  6. Open a browser and hit the URL: http://localhost:8080/springmvcproject/

Gradle Project

  1. Copy the source files from following location to your local disk: https://github.com/mchopker/myprojects/tree/master/SpringMVCProjectWithGradle
  2. Now open the folder via command line and type the command 'gradle eclipse', this will generate Ecipse .classpath and .project files after which you can import this as existing project into your Eclipse IDE. (Remeber you would need Gradle installed in your computer to run the gradle command).
  3. Run the Gradle build.gradle file via Eclipse (give 'war' as Gradle Task).
  4. The build will create a build folder under which you will find 'SpringMVCProjectWithGradle-1.0.war' file created.
  5. Deploy it into your local Tomcat server (put under webapps folder and start the server).
  6. Open a browser and hit the URL: http://localhost:8080/http://localhost:8080/SpringMVCProjectWithGradle-1.0/

Closing Notes

Now you must have noticed following points while working on above three versions of the sample project:
  1. Ant (build.xml) provides different targets to specify compile, clean, package tasks. Ivy (ivy.xml) is used for dependency management as we did specify 3rd party libraries like Spring, Hibernate under dependencies and the build took care of automatically downloading the respective jars.
  2. Maven (pom.xml) provides both building and dependency management capability. We used maven central repository to pull 3rd party jars during build and packaging.
  3. Gradle (build.gradle) also provides both building and dependency management capability. We have used maven repository for Gradle dependency management here.
  4. Now you must be able to notice the simplicity of Gradle's build over Ant-Ivy and Maven. For more detail please refer following post: http://technologyconversations.com/2014/06/18/build-tools/

Thanks!