Gradle Installation, Gradle Wrapper and Gradle Springboot Web Application

Romil Jain
4 min readNov 16, 2017

--

Gradle Setup and Build

Gradle Installation

On Mac

  • Download the latest Gradle from the website: https://gradle.org/
  • Choose the binary-only version, If in doubt
  • Unzip the distribution zip file in the directory of your choice
$ unzip gradle-4.3.1-bin.zip -d {directory}
  • Configure the system environment
$ export PATH=$PATH:/{directory}/gradle-4.3.1/bin
  • If installed in home directory then use below
$ export PATH=$PATH:$HOME/gradle-4.3.1/bin
  • Verify the installation
$ gradle -v

Gradle Springboot Web Application — Eclipse Mac

To create a Gradle project in eclipse first we need to install Gradle plugin

Start Eclipse and create a workspace.

GoTo Help → Eclipse Market Place → Search for Buildship Gradle Integration → click on Install

After installation, It will ask to restart Eclipse. After restarting you can check the plugin. It will display as below

Buildship Gradle Integration 2.0

Now GoTo Eclipse (at the top Menu)→ click Preferences → select Gradle → In Gradle Distribution check Specific Gradle Version (as shown below) → click Apply

Eclipse Gradle Plugin

Now in the same window select Java → Build Path → Classpath Variable → New → In Name Enter GRADLE_USER_HOME and Path Enter ~/.gradlefolder in your home directory (e.g. /Users/<user_name>/.gradle/)

Classpath Variable GRADLE_USER_HOME

Now Let’s create a new Project and select Gradle Project from the wizard. Enter Project Name and finish. This will create the gradle project with the required folder structure and files as shown below

Gradle Project Structre — Eclipse

Now lets create a spring-boot application and update build.gradle file as below to support spring-boot

build.gradle

Create a Class and add @SpringBootApplication, @RestController, @ComponentScan, @Configuration, @EnableAutoConfiguration as shown below

Service.java

Now open terminal, goto Project directory and run gradle build. It will execute all the required tasks and once successful you can see ‘BUILD SUCCESSFUL’ as below

$ gradle buildBUILD SUCCESSFUL in 7s6 actionable tasks: 6 executed

In the project directory, you can see build folder is created. Check build/libs folder and you will see gradle-springboot-1.0.jar is created.

To check the content of JAR, type in libs folder

$ jar tvf gradle-springboot-1.0.jar

Now Go to Eclipse → right click on the Project and Select Gradle → Refresh Gradle Project. Project will compile and all the compilation error will be resolved.

Now run the project from terminal either by running ‘gradle bootRun’ or ‘java -jar build/libs/gradle-springboot-1.0.jar’

$ gradle bootRunOR$ java -jar build/libs/gradle-springboot-1.0.jar

Type http://localhost:8080/ in browser and you will see message as ‘Greetings from Gradle Spring Boot Application!’. Otherwise use curl command.

$ curl http://localhost:8080/
Greetings from Gradle Spring Boot Application!
$ curl http://localhost:8080/hello?name=Romil
Hello Romil

To create a WAR, you need to add below in the build.gradle. It will create a WAR file and can be deployed on external Tomcat Server also.

$ gradle buildBUILD SUCCESSFUL in 2s6 actionable tasks: 3 executed, 3 up-to-date

Check build/libs and you will see gradle-springboot-1.0.war is created .

Now run the project from terminal by running ‘gradle bootRun’ and use curl command to test it. You will get the same o/p

$ curl http://localhost:8080/
Greetings from Gradle Spring Boot Application!
$ curl http://localhost:8080/hello?name=Romil
Hello Romil

Using Gradle Wrapper

Most tools require installation on your computer before you can use them. If the installation is easy, you may think that’s fine. But it can be an unnecessary burden on the users of the build. Equally importantly, will the user install the right version of the tool for the build? What if they’re building an old version of the software?

The Gradle Wrapper solves both these problems and is the preferred way of starting a Gradle build.

If Gradle wrapper (henceforth Wrapper) is not created while creating a Gradle project here is a way to create one. (Using a version helps in creating a wrapper to be used with that gradle version)

$ gradle wrapper --gradle-version 4.3.1BUILD SUCCESSFUL in 0s1 actionable task: 1 up-to-date

This will create a script file gradlew and gradlew.bat in the project. Also there is a folder created gradle/wrapper having gradle-wrapper.jar and gradle-wrapper.properties

The Wrapper is something you can check into version control. By distributing the Wrapper with your project, anyone can work with it without needing to install Gradle beforehand. Even better, users of the build are guaranteed to use the version of Gradle that the build was designed to work with. Of course, this is also great for continuous integration servers as it requires no configuration on the server.

To build and run the project using gradlew (Gradle Wrapper/Wrapper) from the project directory.

$ ./gradlew buildBUILD SUCCESSFUL in 2s6 actionable tasks: 3 executed, 3 up-to-date$ ./gradlew bootRun

Use curl command to test it. You will get the same o/p

$ curl http://localhost:8080/
Greetings from Gradle Spring Boot Application!
$ curl http://localhost:8080/hello?name=Romil
Hello Romil

How to migrate a Maven Spring Boot Web application or Java Web Application to Gradle — Click Here

--

--

No responses yet