BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Spring Boot : Hello World with Gradle 2 - 2020

Duke 512




Bookmark and Share





bogotobogo.com site search:




Preparing for Spring Boot

By default, Spring Boot 1.3.2.RELEASE requires Java 7 and Spring Framework 4.1.5 or above. Spring Boot can be used with "classic" Java development tools or installed as a command line tool. Regardless, we will need Java SDK v1.6 or higher

$ java -version
java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.14.04.1)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

Maven install:

$ sudo apt-add-repository ppa:andrei-pozolotin/maven3
$ sudo apt-get update
$ sudo apt-get install maven3

$ maven -version
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T09:29:23-08:00)
Maven home: /home/k/java/apache-maven-3.2.5
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-40-generic", arch: "amd64", family: "unix"

Gradle install:

$ sudo add-apt-repository ppa:cwchien/gradle
$ sudo apt-get update
$ sudo apt-get install gradle

$ gradle -version

------------------------------------------------------------
Gradle 2.11
------------------------------------------------------------

Build time:   2016-02-08 07:59:16 UTC
Build number: none
Revision:     584db1c7c90bdd1de1d1c4c51271c665bfcba978

Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.7.0_79 (Oracle Corporation 24.79-b02)
OS:           Linux 3.13.0-40-generic amd64





Create project directory structure

We need to configure our project's directory structure. Spring Boot, whether we're using Gradle or Maven, both follow the classic convention for directory naming and source structure.

$ mkdir -p myproject-gradle/src/main/java/hello




build.gradle file

First we want to set up a basic build script.

Though we can use any build system we like when building apps with Spring, in this section, we'll use Gradle following Building an Application with Spring Boot.


build.gradle:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-spring-boot'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.7
targetCompatibility = 1.7

dependencies {
    // tag::jetty[]
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
    }
    compile("org.springframework.boot:spring-boot-starter-jetty")
    // end::jetty[]
    // tag::actuator[]
    compile("org.springframework.boot:spring-boot-starter-actuator")
    // end::actuator[]
    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.11'
}

The 'buildscript' closure sets up the build itself. This is where the plugins used by the build are declared, along with the repositories in which they can be found.

As we can see from the file, there are several plugin applications:

  1. The java plugin provides the basic tasks related to a simple java project - compiling and packaging, among others.
  2. The eclipse and idea plugins allow project files to be created for eclipse and Intellij, respectively.
  3. The spring-boot plugin contains tasks that build executable jars and execute them using embedded tomcat using tasks like bootRun, which we'll use often in these tutorials.

Next the repositories closure is used to declare where the project's dependencies will be found followed by dependencies closure that contains the jars required by the project.

The task wrapper - which is used to enforce the version of gradle being used on the project, as well as allow easy execution of the build by users who do not have gradle installed.





Application Configuration

In the our project root, let's create a file called Application.java.

src/main/java/hello/Application.java:

package hello;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }
}

Here is out new directory structure:

myproject-gradle/
|-- build.gradle
|-- src
    |-- main
        |-- java
            |-- hello
                |-- Application.java

The annotations are marker interfaces used to alert the framework that this is a Spring config file (@Configuration), that we'd like to scan for beans to load in the current and child packages (@ComponentScan) and that we'd like to use auto configuration settings (@EnableAutoConfiguration).

Auto configuration in Spring Boot takes the concept of convention over configuration to an almost Rails or Grails-like level. It provides basic configuration of an application - where to find properties files, how properties files are named when using Spring Profiles, configuration a DispatcherServlet (note the lack of web.xml), and much more.

It is worth it to look further into what auto configuration is providing to insure our application does not conflict with it, especially if we're planning on deploying to containers that also provide libraries on the class path by default.

The main method simply allows the application to be executed from the command line. This includes the startup of our embedded container (in our case, Tomcat).

It's worth mentioning here that the return type of SpringApplication.run is an ApplicationContext object which can be further manipulated.





Creating a Controller

We'll develop our app using a sample controller configuration as a RestController to verify our app is functioning correctly.

In our source root, let's create a file, src/main/java/hello/HelloController.java:

package hello;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Gradle : Hello Spring Boot!";
    }

}

Here is our project directory structure:

myproject-gradle
├-- build.gradle
└-- src
    └-- main
        └-- java
            ├-- HelloGradle.java
            └-- MyController.java

The RestController interface declares just that - a RESTful controller.

This controller will return strings instead of views by default. The RequestMapping annotation on the only method in this class should be familiar, too. This calls for the controller to respond to requests at "/" by executing the index() method.

In this case, we're responding with a simple String.





Run

At this point our application should work.

To execute the app, we may want to use gradle bootRun:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.2.RELEASE)
...

HelloSpringBootGradle.png




Run

Our directory structure:

$ tree myproject-gradle
myproject-gradle
├-- build
│   ├-- classes
│   │   └-- main
│   │       └-- hello
│   │           ├-- Application.class
│   │           └-- HelloController.class
│   ├-- dependency-cache
│   └-- tmp
│       └-- compileJava
│           └-- emptySourcePathRef
├-- build.gradle
└-- src
    └-- main
        └-- java
            └-- hello
                ├-- Application.java
                └-- HelloController.java





Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Spring Boot



Spring Boot : Hello world with Mavan 3

Spring Boot : Hello world with Gradle 2

Spring Boot (Gradle 2) : Hello world with Authentication

Spring Boot : Deploying War file to Tomcat 8's webapps

How to Setup Apache as Reverse Proxy for Tomcat Server using mod proxy

Maven : mvn command cheat sheet

Spring-Boot REST API with CORS App Maven war file deploy to Tomcat

Spring-Boot / Spring Security with AngularJS - Part I (Introduction)

Spring-Boot / Spring Security with AngularJS - Part II (Dynamic resource load from Angular)

Spring-Boot / Spring Security with AngularJS : Part III (Form-based Authentication)




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Java Tutorials



Java Tutorial Home

Basics - Compiling and Launching

Inner Classes

Constructor

Enums

Static & Finally

Default and Protected

Polymorphism

Exception Handling

Exception Handling II

String Class

Threads

Threads II - State Transition

Threads III - Synchronization

Object Class

File I/O

Serialization

ArrayList

Autoboxing

Java Graphics Interface I - Basics

Java Graphics Interface II - Labels, Text Fields, Layouts

Java Graphics Interface III - paintComponent

TCP Sockets Server/Client

Scala - Functional Java Programming

Apache CXF install

Tomcat 7 Ubuntu 14 Install on Amazon EC2 instance

What is Apache Maven?

Maven life cycle

Eclipse Maven 3 plugin on Ubuntu 14.04

Apache Maven 3 - Setting up and creating a project

Apache Maven 3 - Compile, build, and install a Maven project

Apache Maven 3 - Dependencies

Apache Maven 3 - Web Application

Apache Maven 3 - Plugins (compiler)

Apache Maven 3 - Plugins (Jetty)

Eclipse CDT / JNI (Java Native Interface) / MinGW



Spring Framework

Hello World App with Spring 4 & Maven 3 - Part I





JUnit & Maven Tutorial



JUnit 4 Introduction (Hello World)

JUnit 4 Test with Eclipse Luna (Hello World)

JUnit 4 Test with Maven (Hello World)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master