Java tips-Create a Spring Boot project in Gradle

Create a Spring Boot project in Gradle

Nowadays, you can create a Spring Boot project with just one click such as eclipse. But how do you create a Spring Boot project without the help of eclipse? I would like to write up to the point where RestController returns HelloWorld based on memorandum. (It's not that I don't use eclipse.)

environment

I have installed and running the following on my Mac.

table of contents

  1. Create Gradle project
  2. eclipse import
  3. Spring Boot dependency arrangement
  4. Java class creation
  5. Create Jar

this section

1. Create Gradle project

First, create an empty directory and create a Gradle project.

#Create an empty directory
mkdir GradleSpringBoot

#Gradle project
cd GradleSpringBoot
gradle init

#After typing the init command, the following options will appear, so select the one that applies.
#This time, we will select it to be a Java project.
Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
Enter selection (default: Java) [1..4] 3

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter #It's JUnit 5. If you don't have a reason, choose JUnit 5 as it's new.
Enter selection (default: JUnit 4) [1..4] 4

2. eclipse import

Rewrite build.gradle so that you can import your Gradle project into eclipse.

    id 'application'
//Let's add the following plugin around the 15th line.
    id 'eclipse'

After editing and saving, let's execute the following command.

#"Necessary to recognize as an eclipse project".project」「.Create a "setting".
gradle eclipse

At this point, you can import it into eclipse, so Let's import with import-> existing projects into workspace.

3. Spring Boot dependency arrangement

The dependency of SpringBoot is summarized by Gradle official site, so please refer to this for build. Edit .grale.

build.gradle


/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * User Manual available at https://docs.gradle.org/5.5.1/userguide/tutorial_java_projects.html
 */

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    // Apply the application plugin to add support for building a CLI application
    id 'application'
    id 'eclipse'
    id 'com.gradle.build-scan' version '2.3'
    id 'org.springframework.boot' version '2.1.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is used by the application.
    implementation 'com.google.guava:guava:27.1-jre'
    
    implementation 'org.springframework.boot:spring-boot-dependencies:2.1.6.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter-web:2.1.6.RELEASE'

	// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
	testImplementation 'org.springframework.boot:spring-boot-starter-test:2.1.6.RELEASE'

    // Use JUnit Jupiter API for testing.
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'

    // Use JUnit Jupiter Engine for testing.
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
    
    components {
        withModule('org.springframework:spring-beans') {
            allVariants {
                withDependencyConstraints {
                    // Need to patch constraints because snakeyaml is an optional dependency
                    it.findAll { it.name == 'snakeyaml' }.each { it.version { strictly '1.19' } }
                }
            }
        }
    }
}

bootJar {
    mainClassName = 'co.jp.study.App'
}

buildScan {

    // always accept the terms of service
    termsOfServiceUrl = 'https://gradle.com/terms-of-service'
    termsOfServiceAgree = 'yes'

    // always publish a build scan
    publishAlways()
}

test {
    // Use junit platform for unit tests
    useJUnitPlatform()
}

The version of each library is set to the latest one (as of July 26, 2019) while checking with maven.

4. Java class creation

We will create a POJO class to start SpringBoot and a RestController to return HelloWorld.

App.java


package co.jp.study;

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

/**
 *SpringBoot boot class
 */
@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

HelloController


package co.jp.study.controller;

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

/**
 *Sample RestController class
 */
@RestController("/")
public class HelloContoller {

	@RequestMapping
	public String hello() {
		return "Hello World!";
	}

}

5. Create Jar

Execute the following command on the console Create siteiki and Jar and start Spring Boot.


#Creating a jar
gradle bootJar
#Run
gradle bootRun

If you access the following address and output "Hello World", it is successful. http://localhost:8080/

Summary

Thank you for your hard work. Thank you for reading the long text. Let's send a comfortable Spring Boot development.

Recommended Posts

Java tips-Create a Spring Boot project in Gradle
Create Java Spring Boot project in IntelliJ
Run a Spring Boot project in VS Code
View the Gradle task in the Spring Boot project
How to create a Spring Boot project in IntelliJ
Build a Java project with Gradle
Implement Spring Boot application in Gradle
Launch (old) Spring Boot project in IntelliJ
Build Spring Boot + Docker image in Gradle
How to create a new Gradle + Java + Jar project in Intellij 2016.03
Create a website with Spring Boot + Gradle (jdk1.8.x)
Create a Spring Boot project in intellij and exit immediately after launching
Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA
Automatically deploy a web application developed in Java using Jenkins [Spring Boot application]
How to add a classpath in Spring Boot
Build Spring Boot project by environment with Gradle
Add a project in any folder with Gradle
[Spring Boot] How to create a project (for beginners)
Try gRPC in Spring Boot & Spring Cloud project (Mac OS)
Set context-param in Spring Boot
Spring + Gradle + Java Quick Start
[Java] Thymeleaf Basic (Spring Boot)
Find a subset in Java
CICS-Run Java application-(4) Spring Boot application
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Until you create a Spring Boot project in Intellij and push it to Github
Java development environment construction (Mac + Pleiades All in One Eclipse 4.7 + Spring Boot + Gradle (Buildship))
How to call and use API in Java (Spring Boot)
Introducing Spring Boot2, a Java framework for web development (for beginners)
[Java] Sample project for developing web applications with Spring Boot
Spring Boot Hello World in Eclipse
Spring Boot application development in Eclipse
Automatically specify version in gradle project
◆ Spring Boot + gradle environment construction memo
Do HelloWorld in Java / IntelliJ / Gradle
3 Implement a simple interpreter in Java
I created a PDF in Java.
Java Spring environment in vs Code
Write test code in Spring Boot
Spring Boot: Restful API sample project
Elastic Beanstalk (Java) + Spring Boot + https
Implement reCAPTCHA v3 in Java / Spring
Create a Java project using Eclipse
A simple sample callback in Java
Implement REST API in Spring Boot
What is @Autowired in Spring boot?
[Java] LINE integration with Spring Boot
A memo that touched Spring Boot
Get stuck in a Java primer
Thymeleaf usage notes in Spring Boot
Hello world in Java and Gradle
Spring Boot gradle build with Docker
RSocket is supported in Spring Boot 2.2, so give it a try
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
Get a proxy instance of the component itself in Spring Boot
[Java] Get data from DB using singleton service in Spring (Boot)
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Let's write a test code for login function in Spring Boot
[Gradle] Build a Java project with a configuration different from the convention