かんがるーさんの日記

最近自分が興味をもったものを調べた時の手順等を書いています。今は Spring Boot をいじっています。

共有ライブラリを管理するために Sonatype の Nexus Repository Manager OSS を使用する ( その13 )( 利用するライブラリが依存しているライブラリの別バージョンを build.gradle で指定したらどうなるのか? )

概要

共有ライブラリを管理するために Sonatype の Nexus Repository Manager OSS を使用する ( その12 )( 他のライブラリに依存するライブラリを作成する ) の続きです。

  • 今回の手順で確認できるのは以下の内容です。
    • 利用しているライブラリが依存するライブラリの別のバージョンを、ライブラリを利用するプロジェクトの build.gradle で指定した場合どうなるのか確認します。
    • 利用しているライブラリが依存するライブラリのバージョンを特定できるのか? ( ライブラリを利用するプロジェクトで下位バージョンを指定できないようにできるのか? )、も確認します。

参照したサイト・書籍

  1. POM Reference - Dependency Version Requirement Specification
    https://maven.apache.org/pom.html#Dependency_Version_Requirement_Specification

  2. Gradle 徹底入門

    Gradle徹底入門 次世代ビルドツールによる自動化基盤の構築

    Gradle徹底入門 次世代ビルドツールによる自動化基盤の構築

    • 「Chapter 7 依存関係の管理」を読み直しました。
    • やっと最近になってここに書いてあることが理解できるようになってきたと思います。

目次

  1. ライブラリの依存関係を確認するには?
  2. ksbysample-webapp-demo プロジェクトで org.apache.commons:commons-lang3 の下位バージョンを指定してみる
  3. ksbysample-library-depend-nospring ライブラリを利用するプロジェクトで org.apache.commons:commons-lang3:3.4 以外のバージョンを指定した時にエラーになるようにできるのか?

手順

ライブラリの依存関係を確認するには?

プロジェクトで使用するライブラリの依存関係を調べるには、コマンドラインから gradlew dependencies コマンドを実行します。

ksbysample-webapp-demo プロジェクトで依存関係を出力してみます。以下のコマンドを実行します。

> cd /d C:\project-springboot\ksbysample-nexus-repomng\ksbysample-webapp-demo
> gradlew dependencies

以下の内容が出力されます。かなり長いので途中は省略しています。

:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

archives - Configuration for archive artifacts.
No dependencies

compile - Dependencies for source set 'main'.
+--- org.springframework.boot:spring-boot-starter-web: -> 1.3.6.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.3.6.RELEASE
|    |    +--- org.springframework.boot:spring-boot:1.3.6.RELEASE
|    |    |    +--- org.springframework:spring-core:4.2.7.RELEASE
|    |    |    \--- org.springframework:spring-context:4.2.7.RELEASE
|    |    |         +--- org.springframework:spring-aop:4.2.7.RELEASE
|    |    |         |    +--- aopalliance:aopalliance:1.0
|    |    |         |    +--- org.springframework:spring-beans:4.2.7.RELEASE
|    |    |         |    |    \--- org.springframework:spring-core:4.2.7.RELEASE
|    |    |         |    \--- org.springframework:spring-core:4.2.7.RELEASE
|    |    |         +--- org.springframework:spring-beans:4.2.7.RELEASE (*)
|    |    |         +--- org.springframework:spring-core:4.2.7.RELEASE
|    |    |         \--- org.springframework:spring-expression:4.2.7.RELEASE
|    |    |              \--- org.springframework:spring-core:4.2.7.RELEASE
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:1.3.6.RELEASE
|    |    |    \--- org.springframework.boot:spring-boot:1.3.6.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:1.3.6.RELEASE
|    |    |    +--- ch.qos.logback:logback-classic:1.1.7
|    |    |    |    +--- ch.qos.logback:logback-core:1.1.7
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.20 -> 1.7.21
|    |    |    +--- org.slf4j:jcl-over-slf4j:1.7.21
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.21
|    |    |    +--- org.slf4j:jul-to-slf4j:1.7.21
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.21
|    |    |    \--- org.slf4j:log4j-over-slf4j:1.7.21
|    |    |         \--- org.slf4j:slf4j-api:1.7.21
|    |    +--- org.springframework:spring-core:4.2.7.RELEASE
|    |    \--- org.yaml:snakeyaml:1.16
|    +--- org.springframework.boot:spring-boot-starter-tomcat:1.3.6.RELEASE
|    |    +--- org.apache.tomcat.embed:tomcat-embed-core:8.0.36
|    |    +--- org.apache.tomcat.embed:tomcat-embed-el:8.0.36
|    |    +--- org.apache.tomcat.embed:tomcat-embed-logging-juli:8.0.36
|    |    \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.0.36
|    |         \--- org.apache.tomcat.embed:tomcat-embed-core:8.0.36
|    +--- org.springframework.boot:spring-boot-starter-validation:1.3.6.RELEASE
|    |    +--- org.springframework.boot:spring-boot-starter:1.3.6.RELEASE (*)
|    |    +--- org.apache.tomcat.embed:tomcat-embed-el:8.0.36
|    |    \--- org.hibernate:hibernate-validator:5.2.4.Final
|    |         +--- javax.validation:validation-api:1.1.0.Final
|    |         +--- org.jboss.logging:jboss-logging:3.2.1.Final -> 3.3.0.Final
|    |         \--- com.fasterxml:classmate:1.1.0
|    +--- com.fasterxml.jackson.core:jackson-databind:2.6.7
|    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.6.0 -> 2.6.7
|    |    \--- com.fasterxml.jackson.core:jackson-core:2.6.7
|    +--- org.springframework:spring-web:4.2.7.RELEASE
|    |    +--- org.springframework:spring-aop:4.2.7.RELEASE (*)
|    |    +--- org.springframework:spring-beans:4.2.7.RELEASE (*)
|    |    +--- org.springframework:spring-context:4.2.7.RELEASE (*)
|    |    \--- org.springframework:spring-core:4.2.7.RELEASE
|    \--- org.springframework:spring-webmvc:4.2.7.RELEASE
|         +--- org.springframework:spring-beans:4.2.7.RELEASE (*)
|         +--- org.springframework:spring-context:4.2.7.RELEASE (*)
|         +--- org.springframework:spring-core:4.2.7.RELEASE
|         +--- org.springframework:spring-expression:4.2.7.RELEASE (*)
|         \--- org.springframework:spring-web:4.2.7.RELEASE (*)
+--- ksbysample.library:ksbysample-library-simpleutils:1.0-RELEASE
\--- ksbysample.library:ksbysample-library-depend-nospring:1.0-RELEASE
     \--- org.apache.commons:commons-lang3:3.4

compileClasspath - Compile classpath for source set 'main'.
.....(省略).....

compileOnly - Compile dependencies for source set 'main'.
.....(省略).....

default - Configuration for default artifacts.
.....(省略).....

runtime - Runtime dependencies for source set 'main'.
.....(省略).....

testCompile - Dependencies for source set 'test'.
.....(省略).....

testCompileClasspath - Compile classpath for source set 'test'.
.....(省略).....

testCompileOnly - Compile dependencies for source set 'test'.
.....(省略).....

testRuntime - Runtime dependencies for source set 'test'.
.....(省略).....

(*) - dependencies omitted (listed previously)

BUILD SUCCESSFUL

Total time: 10.541 secs

ポイントとしては、

  • build.gradle でバージョンを指定していないものは org.springframework.boot:spring-boot-starter-web: -> 1.3.6.RELEASE のように使用されたバージョンが出力されます。
  • ライブラリの pom に記載されたバージョンとは別のバージョンが使用される場合には org.slf4j:slf4j-api:1.7.20 -> 1.7.21 のように使用されるバージョンが出力されます。

この出力結果を見ることで、ksbysample.library:ksbysample-library-depend-nospring:1.0-RELEASE では org.apache.commons:commons-lang3:3.4 が使用されていることが分かります。

ksbysample-webapp-demo プロジェクトで org.apache.commons:commons-lang3 の下位バージョンを指定してみる

build.gradle の dependencies を以下のように変更した後、Gradle projects View の左上にある「Refresh all Gradle projects」ボタンをクリックして build.gradle を反映します。

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    compile('org.apache.commons:commons-lang3:3.3')
    compile('ksbysample.library:ksbysample-library-simpleutils:1.0-RELEASE')
    compile('ksbysample.library:ksbysample-library-depend-nospring:1.0-RELEASE')
}
  • compile('ksbysample.library:ksbysample-library-simpleutils:1.0-RELEASE') の上に compile('org.apache.commons:commons-lang3:3.3') を追加します。

コマンドラインから gradlew dependencies コマンドを実行して依存関係を確認します。

\--- ksbysample.library:ksbysample-library-depend-nospring:1.0-RELEASE
     \--- org.apache.commons:commons-lang3:3.4 -> 3.3

org.apache.commons:commons-lang3ksbysample-library-depend-nospring の pom に記述された 3.4 ではなく build.gradle に記述した 3.3 が使用されました。

今度は build.gradle の dependencies で compile('ksbysample.library:ksbysample-library-simpleutils:1.0-RELEASE') の下に compile('org.apache.commons:commons-lang3:3.3') を記述してみます。

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    compile('ksbysample.library:ksbysample-library-simpleutils:1.0-RELEASE')
    compile('ksbysample.library:ksbysample-library-depend-nospring:1.0-RELEASE')
    compile('org.apache.commons:commons-lang3:3.3')
}

「Refresh all Gradle projects」ボタンをクリックして build.gradle を反映した後、gradlew dependencies コマンドを実行して依存関係を確認します。

+--- ksbysample.library:ksbysample-library-depend-nospring:1.0-RELEASE
|    \--- org.apache.commons:commons-lang3:3.4 -> 3.3

先程と同様に org.apache.commons:commons-lang3 は 3.4 ではなく 3.3 が使用されました。記述の位置には関係なく build.gradle に書かれたバージョンの方が優先されるようです。

ksbysample-library-depend-nospring ライブラリを利用するプロジェクトで org.apache.commons:commons-lang3:3.4 以外のバージョンを指定した時にエラーになるようにできるのか?

POM Reference の Dependency Version Requirement Specification を見ると依存するライブラリのバージョンを定義できるようです。試してみます。

ksbysample-webapp-demo プロジェクトを閉じて ksbysample-library-depend-nospring プロジェクトを開いた後、build.gradle を以下の内容に変更します。

group 'ksbysample.library'
version '1.1-RELEASE'

..........

dependencies {
    compile("org.apache.commons:commons-lang3:[3.4]")
    testCompile("org.spockframework:spock-core:1.0-groovy-2.4")
}
  • version の文字列を 1.0-RELEASE1.1-RELEASE に変更します。
  • org.apache.commons:commons-lang3 のバージョン番号の指定方法を 3.4[3.4] に変更します。

Gradle projects View の左上にある「Refresh all Gradle projects」ボタンをクリックして build.gradle を反映した後、uploadArchives タスクを実行して Nexus に登録します。

f:id:ksby:20160820170710p:plain f:id:ksby:20160820170812p:plain

Nexus に登録された 1.1-RELEASE の pom ファイルを見てみると、<version>[3.4]</version> で登録されていることが確認できます。

f:id:ksby:20160820171201p:plain

ksbysample-library-depend-nospring プロジェクトを閉じて ksbysample-webapp-demo プロジェクトを開いた後、build.gradle を以下の内容に変更します。

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')

    compile('ksbysample.library:ksbysample-library-simpleutils:1.0-RELEASE')
    compile('ksbysample.library:ksbysample-library-depend-nospring:1.1-RELEASE')
    compile('org.apache.commons:commons-lang3:3.3')
}
  • ksbysample.library:ksbysample-library-depend-nospring のバージョンを 1.0-RELEASE1.1-RELEASE に変更します。

Gradle projects View の左上にある「Refresh all Gradle projects」ボタンをクリックして build.gradle を反映しますが、特にエラーは出ませんでした。。。?

gradlew dependencies コマンドを実行してみると org.apache.commons:commons-lang3 のバージョンは 3.3 になっていました。。。

f:id:ksby:20160820172455p:plain

ライブラリが想定していないバージョンを使用しないよう制限をかけることは出来ないようです。Dependency Version Requirement Specification は単に書いておけるだけということなのでしょうか?

Nexus に登録した ksbysample-library-depend-nospring の 1.1-RELEASE は削除し、ここまで変更した内容も全て破棄して元に戻します。今回は何も commit しません。

ソースコード

履歴

2016/08/20
初版発行。