[Java] Create Java Spring Boot project with IntelliJ

1 minute read

Introduction

Organize the steps to create a Spring Boot project in IntelliJ IDEA. This time we will use Java and Gradle.

Procedure

Project creation

Start IntelliJ and click Create New Project.

![Screenshot 2020-06-19 0.06.56.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/81113/e9d922b9-262d-b86b-f40b-(cae75571f479.png)

Select Gradle, Java and click Next.

Screenshot 2020-06-19 0.13.14.png

Create a project as SprintBootTest.

![Screenshot 2020-06-19 1.49.36.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/81113/26c17fb7-bba0-9c1f-06fc-(78555557cf91.png)

edit build.gradle

Build.gradle created in the project

build.gradle


plugins {
    id'java'
}

group'com.ykdevs'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group:'junit', name:'junit', version: '4.12'
}

Edit as follows. The latest version was specified from SpringBoot. Since IntelliJ is used, idea is specified in plugins. Reference JUnit uses JUnit5.

build.gradle


buildscript {
    ext {
        springBootVersion = '2.3.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id'idea' // IntelliJ
    id'java'
}

group'com.ykdevs'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    // SpringBoot
    implementation "org.springframework.boot:spring-boot-starter-thymeleaf:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
    testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"

    // JUnit
    testImplementation'org.junit.jupiter:junit-jupiter:5.6.2'
}

Create application execution class

The following directories are created when the project is created.

src
├─ main
│ │
│ ├─ java
│ │
│ └─ resources
└─ test
 │ │
 ├─java
 │ │
 └─ resources

Create a package under main/java.

![Screenshot 2020-06-21 21.58.00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/81113/7ce48cf3-52d1-c5bd-fc0a-(d2b832266cde.png)

If you specify the package names separated by commas, the directories will be created automatically.

![Screenshot 2020-06-21 22.22.16.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/81113/055463c7-257b-ab12-9a93-(6d743476f42f.png)

Create an application class file in the package directory.

![Screenshot 2020-06-21 22.05.29.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/81113/9ef39d6d-09f6-eca4-6302-(be81011e524c.png)

Annotate @SpringBootApplication and create main function.

Application.java


package com.ykdevs.springboottest;

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

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Right click on the class name and select Generate to create Test.

![Screenshot 2020-06-21 22.11.30.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/81113/084fb7c0-4b00-0b8b-9d6e-(8f1096537e73.png)

![Screenshot 2020-06-21 22.11.40.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/81113/f458cee6-5f98-6605-b48b-(1ee1a3879244.png)

The test is as follows.

package com.ykdevs.springboottest;

import org.junit.jupiter.api.Test;

class ApplicationTest {

@Test
void main() {
Application.main(new String[0]);
}
}

Test run with # Run

![Screenshot 2020-06-21 22.29.59.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/81113/0a999640-984e-0af8-9570-(70c4d93a6e4c.png)

Screenshot 2020-06-21 22.30.26.png

Reference

Gradle User Guide Introduction to Spring Boot-Official document JUnit 5 User Guide