Skip to content

Instantly share code, notes, and snippets.

@onesword0618
Last active October 12, 2020 16:10
Show Gist options
  • Select an option

  • Save onesword0618/86f29dd60511dd6e2a4522b15ce4deda to your computer and use it in GitHub Desktop.

Select an option

Save onesword0618/86f29dd60511dd6e2a4522b15ce4deda to your computer and use it in GitHub Desktop.
build.gradleで、springを適用するための調査
/**
* build.gradle
* https://docs.gradle.org/6.6/userguide/userguide.html
* Author : onesword0618
*/
// Gradleの機能を拡張する。
// 追加することでプラグインのタスクを利用することが可能になる。
// コアプラグイン以外で利用できるものは、https://plugins.gradle.org/ を参照すること。
plugins {
// JVM Plugins
// Javaのコマンドを定義する。
// ビルド、Jarの作成、テスト実行が該当する。
// https://docs.gradle.org/6.6/userguide/java_plugin.html
// spring bootは専用のタスクで上書きや無効になることがある。
id 'java'
// アプリケーションの実行時のコマンドを定義する。
// アプリケーション実行時のクラスやディレクトリを指定する。
// https://docs.gradle.org/6.6/userguide/application_plugin.html
// spring bootは専用のタスクで上書きや無効になることがある。
id 'application'
// 静的なソースコード分析
// 定義したルールを元に違反しているかをレポーティングする。
// https://pmd.github.io/latest/
// https://docs.gradle.org/6.6/userguide/pmd_plugin.html
id 'pmd'
// XMLファイルに定義したルールに違反しているかをレポーティングする。
// https://checkstyle.org/index.html
// https://docs.gradle.org/6.6/userguide/checkstyle_plugin.html
id 'checkstyle'
// テストの網羅率をレポーティングする。
// https://www.eclemma.org/jacoco/
// https://docs.gradle.org/6.6/userguide/jacoco_plugin.html
id 'jacoco'
// Eclipseがプロジェクトとして認識するためのファイルを作成する。
// https://www.eclipse.org/
// https://docs.gradle.org/6.6/userguide/eclipse_plugin.html
id 'eclipse'
// IDEAがプロジェクトとして認識するためのファイルを作成する。
// https://www.jetbrains.com/idea/
// https://docs.gradle.org/6.6/userguide/idea_plugin.html
id 'idea'
// spring bootを利用する。
// https://plugins.gradle.org/plugin/org.springframework.boot
// https://docs.spring.io/spring-boot/docs/2.3.4.RELEASE/reference/html/
// id("org.springframework.boot") version "2.3.4.RELEASE" apply false
id 'org.springframework.boot' version '2.3.4.RELEASE'
// spring bootのアプリケーションの依存関係の管理をする。
// Gradle 6以降は、Gradleのnative bom サポートを利用することでバージョンのカスタマイズやビルドの高速化を行うことができる。
   // https://docs.spring.io/spring-boot/docs/2.3.4.RELEASE/gradle-plugin/reference/html/#managing-dependencies
// https://docs.spring.io/dependency-management-plugin/docs/current-SNAPSHOT/reference/html/
id 'io.spring.dependency-management'
// fat Jarを作成するのに利用する。
// https://imperceptiblethoughts.com/shadow/
id 'com.github.johnrengelman.shadow' version '6.1.0'
}
repositories {
// https://repo.maven.apache.org/maven2/
// Use mavenCentral for resolving dependencies.
// https://docs.spring.io/spring-boot/docs/2.3.4.RELEASE/reference/html/using-spring-boot.html#using-boot-build-systems
mavenCentral()
}
dependencies {
// Managing Dependencies with Gradle’s Bom Support
// implementation platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
// This dependency is used by the application.
implementation 'com.google.guava:guava:29.0-jre'
// Hibernate で Spring Data JPA を使用するためのスターター
implementation 'spring-boot-starter-data-jpa'
// JSONの読み書きをするためのスターター
implementation 'spring-boot-starter-json'
// 自動構成サポート、ロギング、YAMLで設定するスターター
implementation 'spring-boot-starter'
// Spring MVC を利用してRESTfulを含むWebアプリケーションを構築するスターター
implementation 'spring-boot-starter-web'
// Spring Framework のキャッシュサポートを使用するためのスターター
implementation 'spring-boot-starter-cache'
// LogBackを利用したロギングのスターター
implementation 'spring-boot-starter-logging'
// thymeleafを利用するためのスターター
implementation 'spring-boot-starter-thymeleaf'
// アプリケーションの監視と管理に役立つ本番対応機能を提供するスターター
implementation 'spring-boot-starter-actuator'
// Use JUnit Jupiter API for testing.
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
// Use JUnit Jupiter Engine for testing.
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
}
// ./gradlew bootRun
// https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html
// https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/JavaExec.html#setArgsString-java.lang.String-
application {
mainClassName = 'boilerplate.springboot.App'
manifest {
attributes 'Start-Class': 'com.example.ExampleApplication'
}
}
// bootJarのブロックでメインクラスを指定するパターン
// bootJar {
// mainClassName = 'com.example.ExampleApplication'
// }
// springBootのブロックでメインクラスを指定するパターン
// springBoot {
// mainClassName = 'com.example.ExampleApplication'
// }
test {
// Use junit platform for unit tests
useJUnitPlatform()
}
// 開発時のリソースのリロードができるようになる。
bootRun {
sourceResources sourceSets.main
}
// gradle bootBuildImage --builder=mine/java-cnb-builder --runImage=mine/java-cnb-run
// gradle bootBuildImage --imageName=example.com/library/my-app:v1
bootBuildImage {
builder = "mine/java-cnb-builder"
runImage = "mine/java-cnb-run"
environment = ["BP_JVM_VERSION" : "8.*"]
imageName = "example.com/library/${project.name}"
}
// PMDの設定
// https://docs.gradle.org/current/userguide/pmd_plugin.html#sec:pmd_configuration
// https://qiita.com/onesword/items/424536f099e9af914b8a
pmd {
consoleOutput = true
toolVersion = "6.23.0"
rulePriority = 5
ruleSets = ["category/java/errorprone.xml", "category/java/bestpractices.xml"]
}