コンテンツにスキップ

プラグイン、依存ライブラリのバージョン定義一元化⚓︎

アプリケーションが使用する各種プラグインおよびライブラリのバージョンは、サブプロジェクト間のバージョン齟齬などを防ぐために dependencies.gradle で一元管理します。

ルートプロジェクトの設定⚓︎

ルートプロジェクト直下に dependencies.gradle ファイルを追加してください。その後以下のように、利用するプラグインとライブラリのバージョン、ライブラリ定義文字列を変数として定義します。

{ルートプロジェクト}/dependencies.gradle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
ext {
  // プラグインのバージョン
  springBootVersion = 'x.x.x'
  springDependencyManagementVersion = 'x.x.x'
  springdocOpenapiGradlePluginVersion = 'x.x.x'

  // 依存ライブラリのバージョン
  springdocOpenapiVersion = 'x.x.x'
  h2Version = 'x.x.x'

  // ライブラリ定義文字列
  supportDependencies = [
    spring_boot_starter : 'org.springframework.boot:spring-boot-starter',
    spring_boot_starter_web : "org.springframework.boot:spring-boot-starter-web",
    spring_boot_starter_actuator : "org.springframework.boot:spring-boot-starter-actuator",
    spring_boot_starter_test : 'org.springframework.boot:spring-boot-starter-test',
    springdoc_openapi_starter_webmvc_ui : "org.springdoc:springdoc-openapi-starter-webmvc-ui:$springdocOpenapiVersion",
    h2database : "com.h2database:h2:$h2Version",
  ]
}

dependencies.gradle に記載する情報の範囲について

dependencies.gradle には依存ライブラリのバージョン部分のみを定義してもかまいません。 しかし、ライブラリ定義文字列全体を変数として定義することで、 GitHub が提供する依存関係監視ツール Dependabot による通知が受けられます。

次に、上記ファイルをルートプロジェクトの build.gradle 内の buildscript ブロックで読み込みます。これにより、各サブプロジェクトからそれぞれの変数を参照できるようになります。 以下に示す buildscript ブロックを、ルートプロジェクトの build.gradle の先頭に追加してください。

{ルートプロジェクト}/build.gradle
1
2
3
buildscript {
  apply from: 'dependencies.gradle'
}
ここまでの手順を実行した際の {ルートプロジェクト}/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
buildscript {
  apply from: 'dependencies.gradle'
}

plugins {
  id 'com.github.spotbugs' version 'x.x.x' apply false
}

subprojects {

  apply plugin: 'java'
  apply plugin: 'jacoco'
  apply plugin: 'checkstyle'
  apply plugin: 'com.github.spotbugs'

  dependencies {
    // Lombok の設定
    annotationProcessor 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'
    testCompileOnly 'org.projectlombok:lombok'
  }

  test {
    // UTテスト時はtestプロファイルを利用
    jvmArgs=['-Dspring.profiles.active=test']
    useJUnitPlatform()
  }

  checkstyle {
    toolVersion = 'x.x.x'
  }

  spotbugs {
    toolVersion = 'x.x.x'
    excludeFilter.set(rootProject.file('フィルタファイルのパス'))
    ignoreFailures = true
  }

  jacocoTestReport {
    reports {
      html.required = true
    }
    afterEvaluate {
      classDirectories.setFrom(classDirectories.files.collect {
        fileTree(dir: it, excludes: ['**/xxx/*', '**/yyy.class'])
      })
    }
  }
}

サブプロジェクトの設定⚓︎

各サブプロジェクトでは、下記のように 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
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_web
  implementation supportDependencies.h2database
  implementation supportDependencies.springdoc_openapi_starter_webmvc_ui
  implementation supportDependencies.spring_boot_starter_actuator
  implementation supportDependencies.spring_boot_starter_log4j2
  testImplementation supportDependencies.spring_boot_starter_test
  implementation project(':application-core')
  implementation project(':infrastructure')
  implementation project(':system-common')
}
ここまでの手順を実行した際の 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
52
53
54
55
56
57
58
59
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'

java {
  toolchain {
    languageVersion = JavaLanguageVersion.of(x)
  }
}

repositories {
  mavenCentral()
}

dependencies {
  implementation supportDependencies.spring_boot_starter_web
  implementation supportDependencies.h2database
  implementation supportDependencies.springdoc_openapi_starter_webmvc_ui
  implementation supportDependencies.spring_boot_starter_actuator
  implementation supportDependencies.spring_boot_starter_log4j2
  testImplementation supportDependencies.spring_boot_starter_test
  implementation project(':application-core')
  implementation project(':infrastructure')
  implementation project(':system-common')
  // その他、プロジェクトに必要な依存ライブラリは任意で追加してください。
}

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")

tasks.named('test') {
  useJUnitPlatform()
}

バージョン定義一元化を実行した後に、適切にビルドが実行できるかを確認します。 ターミナルを用いてルートプロジェクト直下で以下を実行してください。

バックエンドアプリケーションのビルド
1
./gradlew build