LoginSignup
15
18

More than 5 years have passed since last update.

Spring Boot解説第5回(開発環境編:build.gradleについて)

Last updated at Posted at 2016-04-10

こんにちはこんばんは!
株式会社情創 技術開発局
暖かくなり食欲がわいてきて
焼肉が食べたくて仕方がない@YAKINIKUです!

今回はbuild.gradleについて解説します。

build.gradleとは

Groovyで記述されたGradleのビルドスクリプトファイルのことです。
gradleコマンドの実行時にここを参照してビルドを実行します。

build.gradleのファイルはこちらに生成されます。
build.gradle.png

build.gradle

実際のソースをご覧ください。
各項目の解説はコメントで記入しています。

build.gradle
// 外部ライブラリを使うときの依存関係を設定する箇所
buildscript {
    ext {
        springBootVersion = '1.3.1.RELEASE'
    }
    repositories {
        // インターネット上に存在するリポジトリーを取得する
        mavenCentral()
    }
    dependencies {  
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        // HOT deployの実現  
        classpath("org.springframework:springloaded:1.2.1.RELEASE")
    }
}

// プラグインの適用
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'war'

// warの名前とバージョン設定
war {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}

// エンコーディング
def defaultEncoding = 'UTF-8'
[compileJava, compileTestJava]*.options*.encoding = defaultEncoding

// ソースと実行ファイルのJava バージョン
String jdkVersion = 1.8
sourceCompatibility = jdkVersion
targetCompatibility = jdkVersion


repositories {
// インターネット上に存在するリポジトリーを取得する
    mavenCentral()
}

// 依存ライブラリー(使用するjarファイルのこと)はdependencies で指定する。
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
// warプラグインでは、providedCompileとprovidedRuntimeの2つの依存構成を追加します。
// この2つの構成は、warアーカイブに追加されない点以外は、それぞれcompile、runtimeと同じ機能です。
    providedCompile("org.projectlombok:lombok:1.16.6")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}
import org.gradle.plugins.ide.eclipse.model.SourceFolder
eclipse {
    // .project の設定
    project {
        // プロジェクト独自の特徴づけを行う eclipse上で右クリックした際に出てくるメニューの追加等を行える
        natures 'org.springframework.ide.eclipse.core.springnature'
        natures 'org.springsource.ide.eclipse.gradle.core.nature'
        natures 'org.eclipse.jdt.core.javanature'
        natures 'net.harawata.mybatipse.MyBatisNature'
        // ビルドコマンド
        buildCommand 'org.springframework.ide.eclipse.core.springbuilder'
        buildCommand 'org.eclipse.jdt.core.javabuilder'
    }

    // .classpath の設定
    classpath {
        file {
            // ソースパスの設定を削除
            beforeMerged { classpath ->
                classpath.entries.removeAll { it.kind == "src" }
            }
            // 出力パスを gradle のデフォルトパスに合わせる
            whenMerged { classpath ->
                classpath.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/main/java")      }*.output = "build/classes/main"
                classpath.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/main/resources") }*.output = "build/resources/main"
                classpath.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/test/java")      }*.output = "build/classes/test"
                classpath.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/test/resources") }*.output = "build/resources/test"
                classpath.entries.removeAll { it.kind == "output" }
            // デフォルトランチャーの削除
            containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
            // ランチャー指定 JREシステムライブラリーが変更される
            containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
            containers 'org.eclipse.buildship.core.gradleclasspathcontainer'
        }
    }
}

では、今回はここまでです。
またお会いしましょう!

15
18
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
15
18