共有ライブラリを管理するために Sonatype の Nexus Repository Manager OSS を使用する ( その22 )( Spring Framework に依存するライブラリを作成する5 )
概要
- 今回の手順で確認できるのは以下の内容です。
- 1.4 系のプロジェクトに 1.3.8 ベースの ksbysample-library-depend-spring ライブラリを導入すると Spring Boot はどのバージョンが使用されるのか確認します。
参照したサイト・書籍
目次
- 1.4 系のプロジェクト ksbysample-webapp-demo14 を作成する
- ksbysample-library-depend-spring ライブラリ導入時のログが出ることを確認する
- gradlew dependencies コマンドで依存関係を確認する
手順
1.4 系のプロジェクト ksbysample-webapp-demo14 を作成する
今回は IntelliJ IDEA から Spring Initializr を利用してプロジェクトを作成した後、build.gradle に Spring IO Platform の bom 等を追加します。
IntelliJ IDEA の「Welcome to IntelliJ IDEA」ダイアログから「Create New Project」メニューをクリックします。
「New Project」ダイアログが表示されます。画面左側の一覧から「Spring Initializr」を選択してから以下の画像の内容を入力後、「Next」ボタンをクリックします。
Group、Artifact 等を入力する画面が表示されます。以下の画像の内容を入力後、「Next」ボタンをクリックします。
Spring のプロダクトを選択する画面が表示されます。以下の画像のプロダクトをチェックした後、「Next」ボタンをクリックします。
Project name と Project location を入力する画面が表示されます。以下の画像の内容になっていることを確認した後、「Finish」ボタンをクリックします。
「Import Module from Gradle」ダイアログが表示されます。「Create directories for empty roots automatically」をチェックした後、「OK」ボタンをクリックします。
IntelliJ IDEA の 2016.3 ではプロジェクト作成直後に画面右側に「Gradle」アイコン&文字が表示されないので、一旦 IntelliJ IDEA を再起動します。
build.gradle を リンク先の内容 に変更します。変更後、Gradle projects View の左上にある「Refresh all Gradle projects」ボタンをクリックして反映します。
src/main/java/ksbysample/webapp/demo14 の下に TestController.java を作成し、リンク先の内容 を記述します。
ksbysample-library-depend-spring ライブラリ導入時のログが出ることを確認する
Gradle projects View から bootRun タスクを実行し、Tomcat を起動します。
ブラウザを起動し http://localhost:8080/test にアクセスすると、request, response のログが出力されることが確認できました。1.4 系のプロジェクトでも動作しますね。
gradlew dependencies コマンドで依存関係を確認する
コマンドプロンプトを起動しカレントディレクトリを c:\project-springboot\ksbysample-nexus-repomng\ksbysample-webapp-demo14 へ移動した後、gradlew dependencies コマンドを実行してみます。
今回は org.springframework.boot:spring-boot-starter-web:1.3.8.RELEASE -> 1.4.2.RELEASE (*)
と出力されており、1.4.2 が使用されていました。ライブラリ側のバージョンではなく、メインプロジェクト側のバージョンが使用されるようです。
compile - Dependencies for source set 'main'. +--- org.springframework.boot:spring-boot-starter-web: -> 1.4.2.RELEASE | +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE | | +--- org.springframework.boot:spring-boot:1.4.2.RELEASE | | | +--- org.springframework:spring-core:4.3.4.RELEASE | | | \--- org.springframework:spring-context:4.3.4.RELEASE | | | +--- org.springframework:spring-aop:4.3.4.RELEASE | | | | +--- org.springframework:spring-beans:4.3.4.RELEASE | | | | | \--- org.springframework:spring-core:4.3.4.RELEASE | | | | \--- org.springframework:spring-core:4.3.4.RELEASE | | | +--- org.springframework:spring-beans:4.3.4.RELEASE (*) | | | +--- org.springframework:spring-core:4.3.4.RELEASE | | | \--- org.springframework:spring-expression:4.3.4.RELEASE | | | \--- org.springframework:spring-core:4.3.4.RELEASE | | +--- org.springframework.boot:spring-boot-autoconfigure:1.4.2.RELEASE | | | \--- org.springframework.boot:spring-boot:1.4.2.RELEASE (*) | | +--- org.springframework.boot:spring-boot-starter-logging:1.4.2.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.3.4.RELEASE | | \--- org.yaml:snakeyaml:1.17 | +--- org.springframework.boot:spring-boot-starter-tomcat:1.4.2.RELEASE | | +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.6 | | +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.6 | | \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.6 | | \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.6 | +--- 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 -> 1.3.3 | +--- com.fasterxml.jackson.core:jackson-databind:2.8.4 | | +--- com.fasterxml.jackson.core:jackson-annotations:2.8.0 -> 2.8.4 | | \--- com.fasterxml.jackson.core:jackson-core:2.8.4 | +--- org.springframework:spring-web:4.3.4.RELEASE | | +--- org.springframework:spring-aop:4.3.4.RELEASE (*) | | +--- org.springframework:spring-beans:4.3.4.RELEASE (*) | | +--- org.springframework:spring-context:4.3.4.RELEASE (*) | | \--- org.springframework:spring-core:4.3.4.RELEASE | \--- org.springframework:spring-webmvc:4.3.4.RELEASE | +--- org.springframework:spring-aop:4.3.4.RELEASE (*) | +--- org.springframework:spring-beans:4.3.4.RELEASE (*) | +--- org.springframework:spring-context:4.3.4.RELEASE (*) | +--- org.springframework:spring-core:4.3.4.RELEASE | +--- org.springframework:spring-expression:4.3.4.RELEASE (*) | \--- org.springframework:spring-web:4.3.4.RELEASE (*) \--- ksbysample.library:ksbysample-library-depend-spring:1.0.0-RELEASE +--- org.springframework.boot:spring-boot-autoconfigure:1.3.8.RELEASE -> 1.4.2.RELEASE (*) +--- org.springframework.boot:spring-boot-starter-web:1.3.8.RELEASE -> 1.4.2.RELEASE (*) +--- org.aspectj:aspectjweaver:1.8.9 +--- org.apache.commons:commons-lang3:3.5 -> 3.4 \--- com.google.guava:guava:20.0 -> 17.0
ソースコード
ksbysample-webapp-demo14/build.gradle
buildscript { ext { springBootVersion = '1.4.2.RELEASE' } repositories { mavenCentral() maven { url "http://repo.spring.io/repo/" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath("io.spring.gradle:dependency-management-plugin:0.6.1.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' jar { baseName = 'ksbysample-webapp-demo14' version = '0.0.1-SNAPSHOT' } sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { maven { url "http://localhost:8081/repository/maven-public" } } dependencyManagement { imports { mavenBom 'io.spring.platform:platform-bom:Athens-SR1' } } dependencies { compile('org.springframework.boot:spring-boot-starter-web') runtime('org.springframework.boot:spring-boot-devtools') testCompile('org.springframework.boot:spring-boot-starter-test') compile('ksbysample.library:ksbysample-library-depend-spring:1.0.0-RELEASE') }
- buildscript の以下の点を変更します。
- Spring Boot は 1.4.3 がリリースされていますが Spring IO Platform の最新版で対応しているのは 1.4.2 なので、springBootVersion の値を
1.4.3.RELEASE
→1.4.2.RELEASE
に変更します。 - repositories に
maven { url "http://repo.spring.io/repo/" }
を追加します。 - dependencies に
classpath("io.spring.gradle:dependency-management-plugin:0.6.1.RELEASE")
を追加します。
- Spring Boot は 1.4.3 がリリースされていますが Spring IO Platform の最新版で対応しているのは 1.4.2 なので、springBootVersion の値を
apply plugin: 'io.spring.dependency-management'
を追加します。あと以前はapply plugin: 'spring-boot'
でしたがapply plugin: 'org.springframework.boot'
に変わっていますね。- repositories 内に書くレポジトリを
mavenCentral()
→maven { url "http://localhost:8081/repository/maven-public" }
へ変更します。 dependencyManagement { imports { mavenBom 'io.spring.platform:platform-bom:Athens-SR1' } }
を追加します。- dependencies に
compile('ksbysample.library:ksbysample-library-depend-spring:1.0.0-RELEASE')
を追加します。
TestController.java
package ksbysample.webapp.demo14; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/test") public class TestController { @RequestMapping @ResponseBody public String index() { return "test"; } }
履歴
2016/12/28
初版発行。