プラグイン、依存ライブラリのバージョン定義一元化
アプリケーションが使用する各種プラグイン、ツールおよびライブラリのバージョンは、サブプロジェクト間のバージョン齟齬などを防ぐために dependencies.gradle で一元管理します。
dependencies.gradle は依存関係の指定を一元管理し、 gradle.lockfile は推移的依存関係を含む解決済みバージョンを記録します。 ロックの設定方法は 依存ライブラリのバージョン固定 を参照してください。
ルートプロジェクトの設定
ルートプロジェクト直下に dependencies.gradle ファイルを追加してください。その後以下のように、利用するプラグインとツール、ライブラリのバージョン、ライブラリ定義文字列を変数として定義します。
| {ルートプロジェクト}/dependencies.gradle |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | ext {
// プラグインのバージョン
springBootVersion = 'x.x.x'
springDependencyManagementVersion = 'x.x.x'
springdocOpenapiGradlePluginVersion = 'x.x.x'
spotbugsVersion = 'x.x.x'
// ツールのバージョン
checkstyleToolVersion = 'x.x.x'
spotbugsToolVersion = 'x.x.x'
jacocoToolVersion = 'x.x.x'
// 依存ライブラリのバージョン
springdocOpenapiVersion = 'x.x.x'
// ライブラリ定義文字列
supportDependencies = [
spring_boot_starter_webmvc : 'org.springframework.boot:spring-boot-starter-webmvc',
spring_boot_starter_actuator : 'org.springframework.boot:spring-boot-starter-actuator',
spring_boot_starter_log4j2 : 'org.springframework.boot:spring-boot-starter-log4j2',
spring_boot_starter_webmvc_test : 'org.springframework.boot:spring-boot-starter-webmvc-test',
springdoc_openapi_starter_webmvc_ui : "org.springdoc:springdoc-openapi-starter-webmvc-ui:$springdocOpenapiVersion",
h2database : "com.h2database:h2",
lombok : "org.projectlombok:lombok",
slf4j : "org.slf4j:slf4j-simple",
spotbugs_annotations : "com.github.spotbugs:spotbugs-annotations:$spotbugsToolVersion",
]
}
|
dependencies.gradle に記載する情報の範囲について
dependencies.gradle には依存ライブラリのバージョン部分のみを定義してもかまいません。 しかし、ライブラリ定義文字列全体を変数として定義することで、 GitHub が提供する依存関係監視ツール Dependabot による通知が受けられます。
次に、上記ファイルをルートプロジェクトの build.gradle 内の buildscript ブロックで読み込みます。これにより、各サブプロジェクトからそれぞれの変数を参照できるようになります。 以下に示す buildscript ブロックを、ルートプロジェクトの build.gradle の先頭に追加してください。
| {ルートプロジェクト}/build.gradle |
|---|
| buildscript {
apply from: 'dependencies.gradle'
}
|
プロジェクトの共通設定 で解説した SpotBugs プラグインのバージョンも、同様に変数を参照する形に修正します。 併せて、各サブプロジェクトで共通して利用するライブラリや Checkstyle・SpotBugs・JaCoCo の toolVersion も変数を参照する形に修正します。
| {ルートプロジェクト}/build.gradle |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | plugins {
id 'com.github.spotbugs' version "${spotbugsVersion}" apply false
}
subprojects {
annotationProcessor supportDependencies.lombok
testAnnotationProcessor supportDependencies.lombok
compileOnly supportDependencies.lombok
testCompileOnly supportDependencies.lombok
compileOnly supportDependencies.spotbugs_annotations
testCompileOnly supportDependencies.spotbugs_annotations
checkstyle {
toolVersion = "${checkstyleToolVersion}"
}
spotbugs {
toolVersion = "${spotbugsToolVersion}"
excludeFilter.set(rootProject.file('フィルタファイルのパス'))
ignoreFailures = true
}
jacoco {
toolVersion = "${jacocoToolVersion}"
}
}
|
ここまでの手順を実行した際の {ルートプロジェクト}/build.gradle の例
| {ルートプロジェクト}/build.gradle |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 | buildscript {
apply from: 'dependencies.gradle'
}
plugins {
id 'com.github.spotbugs' version "${spotbugsVersion}" apply false
}
dependencyLocking {
lockAllConfigurations()
lockMode = LockMode.STRICT
}
subprojects {
dependencyLocking {
lockAllConfigurations()
lockMode = LockMode.STRICT
}
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'checkstyle'
apply plugin: 'com.github.spotbugs'
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'
javadoc.options.encoding = 'UTF-8'
dependencies {
// Lombok の設定
annotationProcessor supportDependencies.lombok
testAnnotationProcessor supportDependencies.lombok
compileOnly supportDependencies.lombok
testCompileOnly supportDependencies.lombok
compileOnly supportDependencies.spotbugs_annotations
testCompileOnly supportDependencies.spotbugs_annotations
// SpotBugsの警告対策
// https://github.com/spotbugs/spotbugs-gradle-plugin/issues/136
spotbugsSlf4j supportDependencies.slf4j
}
test {
// UTテスト時はtestプロファイルを利用
jvmArgs=['-Dspring.profiles.active=test']
useJUnitPlatform()
finalizedBy jacocoTestReport
}
checkstyle {
toolVersion = "${checkstyleToolVersion}"
}
spotbugs {
toolVersion = "${spotbugsToolVersion}"
excludeFilter.set(rootProject.file('フィルタファイルのパス'))
ignoreFailures = true
}
spotbugsMain {
reports {
// XML 形式のレポートが不要な場合は以下を追加
xml.required = false
html {
required = true
outputLocation = layout.buildDirectory.file('reports/spotbugs/main.html')
}
}
}
jacoco {
toolVersion = "${jacocoToolVersion}"
}
jacocoTestReport {
reports {
html.required = true
}
afterEvaluate {
classDirectories.setFrom(classDirectories.files.collect {
fileTree(dir: it, excludes: ['**/xxx/*', '**/yyy.class'])
})
}
}
}
tasks.register('allDependencies') {
group = 'help'
description = 'ルートおよび全サブプロジェクトの依存関係を解決します。'
dependsOn tasks.named('dependencies')
dependsOn subprojects.collect { "${it.path}:dependencies" }
}
|
サブプロジェクトの設定
各サブプロジェクトでは、下記のように dependencies.gradle で定義された変数を読み取る形にプラグインや依存ライブラリの記載を修正します。 以下に、バージョン定義を dependencies.gradle に移管した web プロジェクトの build.gradle の一部を示します。
Groovy ファイルにおける一重引用符と二重引用符の使い分け
Groovy では文字列は一重引用符で囲み、変数を含む文字列は二重引用符で囲んで表現します。 変数を含む文字列を一重引用符で囲むとエラーが出るため、注意して使い分けてください。
| web/build.gradle |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | plugins {
id 'java'
id 'org.springframework.boot' version "${springBootVersion}"
id 'io.spring.dependency-management' version "${springDependencyManagementVersion}"
id 'org.springdoc.openapi-gradle-plugin' version "${springdocOpenapiGradlePluginVersion}"
}
dependencies {
implementation supportDependencies.spring_boot_starter_webmvc
implementation supportDependencies.spring_boot_starter_actuator
implementation supportDependencies.spring_boot_starter_log4j2
implementation supportDependencies.springdoc_openapi_starter_webmvc_ui
implementation supportDependencies.h2database
implementation project(':application-modules')
implementation project(':system-common')
testImplementation supportDependencies.spring_boot_starter_webmvc_test
}
|
ここまでの手順を実行した際の web/build.gradle の例
| web/build.gradle |
|---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 | plugins {
id 'java'
id 'org.springframework.boot' version "${springBootVersion}"
id 'io.spring.dependency-management' version "${springDependencyManagementVersion}"
id 'org.springdoc.openapi-gradle-plugin' version "${springdocOpenapiGradlePluginVersion}"
}
group = 'プロジェクトのグループ名'
version = 'x.x.x-SNAPSHOT'
description = 'プロジェクトの説明'
repositories {
mavenCentral()
}
dependencies {
implementation supportDependencies.spring_boot_starter_webmvc
implementation supportDependencies.spring_boot_starter_actuator
implementation supportDependencies.spring_boot_starter_log4j2
implementation supportDependencies.springdoc_openapi_starter_webmvc_ui
implementation supportDependencies.h2database
implementation project(':application-modules')
implementation project(':system-common')
testImplementation supportDependencies.spring_boot_starter_webmvc_test
// その他、プロジェクトに必要な依存ライブラリは任意で追加してください。
}
configurations {
all {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
}
// OpenAPI 仕様書出力の作業ディレクトリを指定する。
afterEvaluate {
tasks.named("forkedSpringBootRun") {
workingDir("$rootDir/api-docs")
}
}
// OpenAPI 仕様書の出力先を指定する。
openApi {
apiDocsUrl.set("http://localhost:8080/api-docs")
outputDir.set(file("$rootDir/api-docs"))
outputFileName.set("api-specification.json")
}
// ビルド時に OpenAPI 仕様書の出力を行うよう設定する。
build.dependsOn("generateOpenApiDocs")
|
バージョン定義一元化を実行した後に、適切にビルドが実行できるかを確認します。 ターミナルを用いてルートプロジェクト直下で以下を実行してください。