共有ライブラリを管理するために Sonatype の Nexus Repository Manager OSS を使用する ( その18 )( Spring Framework に依存するライブラリを作成する )
概要
- 今回の手順で確認できるのは以下の内容です。
- Spring Framework に依存するライブラリを作成してみます。
- 作成するライブラリでは Spring Boot の auto-configuration の仕組みを利用してみます。
- 約4回で書く予定です。
- 今回は ksbysample-library-depend-spring プロジェクトを作成するところまで。
- 2回目は ksbysample-webapp-lending から RequestAndResponseLogger.java を持ってきて auto-configuration 関連の実装まで。
- 3回目はテストの実装、及び Nexus への登録まで。
- 4回目は ksbysample-webapp-demo プロジェクトに ksbysample-library-depend-spring ライブラリを導入して動作確認します。
参照したサイト・書籍
目次
手順
ライブラリの仕様を決める
今回は以下の仕様のライブラリを作成します。
- Gradle プロジェクトとして作成します。
- GroupId は
ksbysample.library
、ArtifactId はksbysample-library-depend-spring
にします。 - ksbysample-webapp-lending で作成した RequestAndResponseLoggerクラス を外部ライブラリにします。ksbysample-library-depend-spring ライブラリを build.gradle の dependencies に記述するだけで、@RequestMapping で定義された URL にアクセスされた時に request, response のデータがログに出力されるようにします。
feature/3-issue ブランチを作成する
- GitHub に Issue 3 を作成した後、feature/3-issue ブランチを作成します。
ksbysample-library-depend-spring プロジェクトを作成する
IntelliJ IDEA の「Welcome to IntelliJ IDEA」ダイアログから「Create New Project」メニューをクリックします。
「New Project」ダイアログが表示されます。画面左側の一覧から「Gradle」を選択してから以下の画像の内容を入力後、「Next」ボタンをクリックします。
GroupId、ArtifactId を入力する画面が表示されます。以下の画像の内容を入力後、「Next」ボタンをクリックします。
以下の画像の画面が表示されます。「Create directories for empty content roots automatically」をチェックした後、「Next」ボタンをクリックします。
Project name と Project location を入力する画面が表示されます。Project location を C:\project-springboot\ksbysample-nexus-repomng\ksbysample-library-depend-spring に変更した後、「Finish」ボタンをクリックします。
build.gradle を リンク先の内容 に変更します。変更後、Gradle projects View の左上にある「Refresh all Gradle projects」ボタンをクリックして反映します。
以下のディレクトリに .gitkeep ファイルを作成します。
commit します。
ソースコード
ksbysample-library-depend-spring/build.gradle
group 'ksbysample.library' version '1.0.0-RELEASE' buildscript { ext { springBootVersion = '1.3.8.RELEASE' } repositories { jcenter() 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: 'idea' apply plugin: 'spring-boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'groovy' apply plugin: 'maven' sourceCompatibility = 1.8 targetCompatibility = 1.8 compileJava.options.compilerArgs = ['-Xlint:all'] compileTestGroovy.options.compilerArgs = ['-Xlint:all'] compileTestJava.options.compilerArgs = ['-Xlint:all'] repositories { jcenter() } dependencyManagement { imports { mavenBom 'io.spring.platform:platform-bom:2.0.8.RELEASE' } } dependencies { // dependency-management-plugin によりバージョン番号が自動で設定されるもの // Appendix A. Dependency versions ( http://docs.spring.io/platform/docs/current/reference/htmlsingle/#appendix-dependency-versions ) 参照 compile("org.springframework.boot:spring-boot-autoconfigure") // dependency-management-plugin によりバージョン番号が自動で設定されないもの、あるいは最新バージョンを指定したいもの } javadoc { options.charSet = 'UTF-8' options.encoding = 'UTF-8' } task sourcesJar(type: Jar, dependsOn: classes) { classifier = 'sources' from sourceSets.main.allSource } task javadocJar(type: Jar, dependsOn: javadoc) { classifier = 'javadoc' from javadoc.destinationDir } artifacts { archives sourcesJar archives javadocJar } uploadArchives { def nexusUrl = "http://localhost:8081" def nexusUsername = "admin" def nexusPassword = "admin123" repositories { mavenDeployer { repository(url: "${nexusUrl}/repository/maven-releases") { authentication(userName: nexusUsername, password: nexusPassword) } snapshotRepository(url: "${nexusUrl}/repository/maven-snapshots") { authentication(userName: nexusUsername, password: nexusPassword) } pom.withXml { asNode().appendNode('name', 'Spring 依存のテストライブラリ ksbysample-library-depend-spring') asNode().appendNode('description', 'Spring に依存する共有ライブラリである') } } } }
- Spring Boot はまだ 1.4 系を触っていないので、1.3 系の最新バージョンである 1.3.8 を使用します。
- dependencies には現時点では
org.springframework.boot:spring-boot-autoconfigure
のみ定義します。 - uploadArchives タスク内に記述する nexus にアップロードするユーザは admin を使います。
履歴
2016/12/25
初版発行。