LoginSignup
11
9

More than 3 years have passed since last update.

Java tips - GradleでSpringBootプロジェクトを作成する

Posted at

GradleでSpringBootプロジェクトを作成する

昨今はeclipseなどワンクリックでSpringBootプロジェクトが作成できる時代です。
しかし、eclipseの力を借りずにSpringBootプロジェクトを作成するのはどうすればいいのかと
思い、備忘録ベースでRestControllerでHelloWorldを返すところまで書いて行きたいと思います。
(eclipseを使わない訳ではないです。)

環境

Macで以下のものを導入して実行しています。
- Gradle 5.5.1
- JDK openjdk64-11.0.4
- eclipse 201906

目次

  1. Gradleプロジェクト作成
  2. eclipseインポート
  3. SpringBootの依存関係整理
  4. Javaクラス作成
  5. Jar作成

本節

1. Gradleプロジェクト作成

まず、空のディレクトリを作成してGradleプロジェクトを作成していきます。

# 空のディレクトリ作成
mkdir GradleSpringBoot

# Gradleプロジェクト化
cd GradleSpringBoot
gradle init

# initコマンドを打ち込んだら、以下のような選択肢たちが出てくるので該当するのを選んでいきます。
# 今回はJavaのプロジェクトとなるように選択していきます。
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 #JUnit5のことです。理由がなければ、JUnit5を新しいので選びましょう。
Enter selection (default: JUnit 4) [1..4] 4

2. eclipseインポート

作成したGradleプロジェクトをeclipseにインポートできるようにbuild.gradleを書き換えます。

    id 'application'
// 15行目あたりに以下のプラグインを追加しましょう。
    id 'eclipse'

編集して保存したら、以下のコマンドを実行しましょう。

# eclipseプロジェクトと認識するために必要な「.project」「.setting」を作成します。
gradle eclipse

ここまできたら、eclipseにインポートできるので、
import->existing projects into workspaceでインポートしてみましょう。

3. SpringBootの依存関係整理

SpringBootの依存関係はGradle公式サイトがまとめていてくれているので、こちらを参考にbuild.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()
}

各ライブラリのversionはmavenで確認しながら最新のもの(2019/7/26時点)を設定しています。

4. Javaクラス作成

SpringBootを起動するためのPOJOクラスとHelloWorldを返すためのRestControllerを作成していきます。

App.java
package co.jp.study;

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

/**
 * SpringBoot起動クラス
 */
@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;

/**
 * サンプルRestControllerクラス
 */
@RestController("/")
public class HelloContoller {

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

}

5. Jar作成

コンソールで以下のコマンドを実行siteiki、Jarを作成してSpringBootを起動していきます。


# jarの作成
gradle bootJar
# 実行
gradle bootRun

以下のアドレスにアクセスして「HelloWorld」と出力されたら成功です。
http://localhost:8080/

まとめ

お疲れ様でした。長い文章を見ていただきありがとうございました。
快適なSpringBoot開発を送っていきましょう。

11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9