Gradle installation and Gradle Spring Boot Microservice on Linux Mint

Gradle Installation

Gradle is a free and open source automation toolset based on Apache Ant and Apache Maven and builds support for development lifecycle. It uses domain specific language rather than XML form for project configuration.

Official website of Gradle: https://gradle.org/

Note: Before the Gradle installation, make sure JDK is installed and JAVA_HOME is configured.

Its very simple 5 step process.

  • To install gradle first create a directory inside /opt/ by running the following command and then cd inside that directory.
$ sudo mkdir /opt/gradle
$ cd /opt/gradle/
  • We need binary. Latest version at the time of writing is 5.0. You should manually check which version is current at https://gradle.org/releases
    and download the package for gradle using the below command.
$ sudo wget https://services.gradle.org/distributions/gradle-5.0-bin.zip
  • Extract the downloaded package into /opt/gradle
$ sudo unzip -d /opt/gradle gradle-5.0-bin.zip
  • Configure path environment variable inside /.bashrc , just add these lines at the end of file.
#GRADLE HOME directory setup
export GRADLE_HOME=/opt/gradle/gradle-5.0
export PATH="$PATH:$GRADLE_HOME/bin"
  • Verify the installation
$ gradle -v

Gradle Spring Boot Microservice using Spring Tool Suite on Linux Mint

I am assuming that as you are trying to build microservice or web service or web application, you have basic understanding of what microservice is. If not, then you can refer to some of these reference sites: microservice-understanding or wiki

Now first get STS (Spring Tool Suite) from https://spring.io/tools3/sts/all . In our case its Linux 64-bit. Unzip tar.gz and launch STS by going to location “/sts-bundle/sts-<version>.RELEASE” you will see STS executable.

Note: You can use any other IDE like Eclipse or IntelliJ based on your comfort.

Next step is to go to https://start.spring.io/ and use this spring initializer tool generate project.

unzip the project and import it in STS.

Go to github location(https://github.com/shivrajjadhav733/springboot-gradle-microservice) and pull code.

Please see project structure

Lets dive into SpringbootGradleMicroserviceApplication.java

If you see the file I have added @RestController annotation. What it does is it creates REST web service out of this class.

But that’s not the only thing sufficient here. We need to map incoming request to one method which will give response. How do we do that? Just add a method with @RequestMapping annotation.

@SpringBootApplication
@RestController
public class SpringbootGradleMicroserviceApplication {
@RequestMapping(“/”)
 public String home() {
 return “Hello World from Gradle”;
 }
 
 public static void main(String[] args) {
 SpringApplication.run(SpringbootGradleMicroserviceApplication.class, args);
 }
}

I encourage to not use port 8080 as it may be occupied by some other service(assuming you are a developer)

With that, application.properties has entry for setting server port.

server.port=8082

Gradle Build from command line/terminal

Now go to Project directory and right click to open terminal. Make sure you are in project directory and run command “gradle build”. It will execute all the required tasks and once successful you can see ‘BUILD SUCCESSFUL’ message.

$ gradle build
BUILD SUCCESSFUL in 4s
5 actionable tasks: 3 executed, 2 up-to-date
Note: When I ran “gradle build” first time, I got an error as an execution failed for task ‘:compileJava’. could not find tools.jar
To fix that I added gradle.properties file in Project directory and set JDK home path in that file as below:
org.gradle.java.home = <JDK home path>
Make sure this is not bin path.

Once build is successful we can run application using below command

$ gradle bootRun

This will start the application. Go to browser and type http://localhost:8082/ and verify if you can connect to microservice/web service.

You will see “Hello World from Gradle” displayed on screen.

You can do gradle build from STS also. For that fist go to WIndows- SHow View — Other- Add Gradle Tasks. Once that is done, you should see Gradle Tasks view. You will see your project there. If you collapse it, you will see something like below.

Go to “build” and collapse it to see build option there. Click on that and it will do build (its same like what you did above by running gradle build command)

Once its done, then collapse “application” to see bootRun option. Click on that and it starts application. You can go to any browser and type http://localhost:8082/ and verify if you can connect to web service.

You will see “Hello World from Gradle” displayed on screen.

You can also use curl command.

$ curl http://localhost:8082/
Hello World from Gradle