Contact

Technology

Dec 01, 2014

Converting a Spring Boot Project from Maven to Gradle in STS

Credera Team
Maria Knabe
Bodie Leonard

Credera Team, Maria Knabe, and Bodie Leonard

Default image background

I was recently asked to convert a Spring Boot project from Maven to Gradle in a Spring Tool Suite (STS) project. After several hours of Google searches and fighting with STS I still was unable to get my project running with Gradle. After getting help from some other employees I was eventually able to get my projects building and running with Gradle. This blog post will try to make this process straightforward for anyone else fighting with this challenge.

Note that this guide is not a catch all, and does not guarantee that the project will run without any issues. However, this method was tested with a number of the Spring Boot starter projects and successfully converted them from Maven to Gradle within STS.

GETTING STARTED

You will need:

If you just want to test this method first you can create a Maven project and add the following:

A basic controller:

package com.credera.blogExample;   import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;   @Controller @EnableAutoConfiguration public class ExampleController {   @RequestMapping(“/”) @ResponseBody String home() { return “Hello World!”; }   public static void main(String[] args) throws Exception { SpringApplication.run(ExampleController.class, args); }}

The pom.xml:

4.0.0   com.credera blogExample 0.0.1-SNAPSHOT jar   blogExample http://maven.apache.org   UTF-8   org.springframework.boot spring-boot-starter-parent 1.1.7.RELEASE   org.springframework.boot spring-boot-starter-web

These example files are from http://projects.spring.io/spring-boot/

DOING THE CONVERSION

Once you have a project ready to convert, follow these steps to convert the project from Maven to Gradle:

1. Fire up a terminal and cd into the project directory. From there, call “gradle init” which should take the pom.xml file and try to convert those dependencies into a build.gradle file.

xmaven11.png.pagespeed.ic.7CHxmLsIZC
xmaven11.png.pagespeed.ic.7CHxmLsIZC

2. Refresh your project in STS (select in package explorer and press f5). It should now have the following files: build.gradle, gradlew, gradlew.bat, pom.xml, and settings.gradle.

xmaven21.png.pagespeed.ic.UbD-7ENCo3
xmaven21.png.pagespeed.ic.UbD-7ENCo3

3. Open the build.gradle file and add the following lines, making sure that the buildscript comes before the “apply plugin.”

buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.7.RELEASE") }}   apply plugin: 'java' apply plugin: 'maven' apply plugin: 'spring-boot'   group = 'org.springframework' version = '0.1.0'   description = """"""   sourceCompatibility = 1.5 targetCompatibility = 1.5       repositories {   maven { url "http://repo.spring.io/libs-release" } maven { url "http://repo.maven.apache.org/maven2" }} dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.1.7.RELEASE'}

4. Right click on your project in the explorer and select “Configure” —> “Convert to Gradle Project”

5. Right click on the project in the package explorer and select “Maven” —> “Disable Maven Nature”

6. Select “Run As” —> “Spring Boot App” and the project should run with Gradle.

Note: If this option to run as a Spring Boot App is not available, most likely Gradle has not been enabled for dependency management. To enable Gradle to be used for dependency management, right click on the project in the project manager and choose “Gradle” —> “Enable Dependency Management”. The option to run as a Spring Boot App should now be available.

MOVING FORWARD

­­­This guide should help you convert simple Spring Boot applications from Maven to Gradle. Although this guide is specific to STS remember that you do not have to run the application from inside STS. The application can be built and run from a terminal in your project directory by using “gradle build” and “gradle run” respectively. If you still have issues after following this guide I recommend checking out some of the guides that are on the Spring website. All of their guides have code that provides both the gradle.build and the pom.xml files for you to easily compare. Mvn repository is another great resource. It has a huge library of dependencies and shows how to add them to Maven or Gradle as well as some other dependency management systems.

OTHER RESOURCES:

http://spring.io/guides

This section of the Spring website offers many tutorials to guide you through basics in Spring and Spring Boot. Even better, these can be imported directly into STS by selecting “File” —> “New”  —>  “Import Spring Getting Started Content”

http://mvnrepository.com

This website offers an easy-to-search library of the plugins you need to add to Maven or Gradle so that your dependencies are satisfied. I’ve found many of my problems were caused by not having the most current dependency. Often these can be solved by searching through mvnrepository for the most current snippet.

Conversation Icon

Contact Us

Ready to achieve your vision? We're here to help.

We'd love to start a conversation. Fill out the form and we'll connect you with the right person.

Searching for a new career?

View job openings