共有ライブラリを管理するために Sonatype の Nexus Repository Manager OSS を使用する ( その5 )( 簡単なライブラリを作成して Nexus に登録してみる )
概要
- 今回の手順で確認できるのは以下の内容です。
- 簡単なライブラリを作成して Nexus に登録します。
参照したサイト・書籍
目次
- GitHub に ksbysample-nexus-repomng レポジトリを作成する
- git clone して README.md を追加する
- ライブラリの仕様を決める
- feature/1-issue ブランチを作成する
- ksbysample-library-simpleutils プロジェクトを作成する
- StringListUtils クラスを作成する
- テストを作成して動作確認する
- Nexus に登録する
手順
GitHub に ksbysample-nexus-repomng レポジトリを作成する
今回作成するファイルを保存するためのレポジトリを GitHub 上に作成します。
GitHub の画面右上の「+」ボタンを押してドロップダウンメニューを表示した後、「New repository」をクリックします。
「Create a new repository」画面が表示されますので、以下の画像の内容を入力後、「Create repository」ボタンをクリックします。
ksbysample-nexus-repomng レポジトリが作成されました。
git clone して README.md を追加する
SourceTree から git clone して README.md を追加します。SourceTree は 1.7.0.32509 を利用しており、まだ 1.8 系にはバージョンアップしていません。
SourceTree の左上の「新規 / クローンを作成する」ボタンをクリックします。
「リポジトリをクローン/追加/作成」ダイアログが表示されます。以下の画像の内容を入力後、「クローン」ボタンをクリックします。
ksbysample-nexus-repomng レポジトリがクローンされて SourceTree 上に表示されます。
C:\project-springboot\ksbysample-nexus-repomng の下に リンク先の内容 の README.md を作成します。
commit、push します。
今回から Git Flow で進めるので master ブランチから develop ブランチを作成して push します。
ライブラリの仕様を決める
今回作成するライブラリの仕様を決めます。
- Gradle プロジェクトとして作成します。
- GroupId は
ksbysample.library
、ArtifactId はksbysample-library-simpleutils
にします。 - StringListUtils クラスを作成し、文字列のリストを受け取って各要素の文字列長をカンマ区切りで結合した文字列を返すメソッドを実装します。
- 依存するライブラリは JDK のみとします。
- テストは Spock で実装します。
feature/1-issue ブランチを作成する
- GitHub に Issue 1 を作成した後、feature/1-issue ブランチを作成します。
ksbysample-library-simpleutils プロジェクトを作成する
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-simpleutils に変更した後、「Finish」ボタンをクリックします。
C:\project-springboot\ksbysample-nexus-repomng の下に リンク先の内容 の .gitignore を作成します。
build.gradle を リンク先のその1の内容 に変更します。変更後、Gradle projects View の左上にある「Refresh all Gradle projects」ボタンをクリックして反映します。
commit します。
StringListUtils クラスを作成する
src/main/java の下に ksbysample.library.simpleutils パッケージを作成します。
src/main/java/ksbysample/library/simpleutils の下に StringListUtils.java を作成し、リンク先の内容 に変更します。
テストを作成して動作確認する
Spock でテストを作成して動作確認します。
StringListUtils クラスのソース上で Ctrl+Shift+T を押してコンテキストメニューを表示した後、「Create New Test...」を選択します。
「Create Test」ダイアログが表示されます。「Testing library」を「Spock」に変更し、画面下部の「Member」リスト内のチェックボックスをチェックした後、「OK」ボタンをクリックします。
「Choose Destination Directory」ダイアログが表示されます。groovy のディレクトリを選択して「OK」ボタンをクリックします。
src/test/groovy/ksbysample/library/simpleutils の下に StringListUtilsTest.groovy が作成されますので、リンク先の内容 に変更します。
テストを実行します。
def "ConvertStringList2LengthCsv(#stringList) --> #result"()
メソッド名の左側の矢印アイコンをクリックしてメニューを表示した後、「Run 'ConvertStringList...()'」メニューを選択します。テストが実行され、全て成功することが確認できました。
Nexus に登録する
build.gradle を修正する
build.gradle を リンク先のその2の内容 に変更します。変更後、Gradle projects View の左上にある「Refresh all Gradle projects」ボタンをクリックして反映します。
Gradle projects View を見ると uploadArchives タスクが追加されていることが確認できます。
Nexus に登録する
Gradle projects View の uploadArchives タスクをダブルクリックして実行します。Run View に実行結果が表示され、BUILD SUCCESSFUL が表示されました。
Nexus の管理画面から登録されていることを確認する
Nexus の管理画面を表示します。画面左側のツリーから「Browse」->「Components」を選択した後、画面右側の repository 一覧から「maven-snapshots」を選択します。
ksbysample-library-simpleutils が登録されていることが確認できます。Version は 1.0-SNAPSHOT ではなく 1.0-20160716.025552-1 という文字列に変更されるようです。
リスト上の ksbysample-library-simpleutils をクリックすると、詳細な情報や登録されているファイル一覧が表示されます。「Delete component」ボタンが表示されていますので、削除可能なユーザでログインすれば削除できるようです。
ソースコード
README.md
共有ライブラリを管理するために Sonatype の Nexus Repository Manager OSS を使用する
.gitignore
# built application files *.apk *.ap_ # files for the dex VM *.dex # Java class files *.class # generated files **/bin/ **/gen/ # Local configuration file (sdk path, etc) local.properties # Eclipse project files .classpath .project # Proguard folder generated by Eclipse **/proguard/ # Intellij project files *.iml *.ipr *.iws **/.idea/ **/out/ #Gradle .gradletasknamecache **/.gradle/ **/build/ **/bin/
build.gradle
■その1
group 'ksbysample.library' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'groovy' sourceCompatibility = 1.8 targetCompatibility = 1.8 compileJava.options.compilerArgs = ['-Xlint:all'] compileTestGroovy.options.compilerArgs = ['-Xlint:all'] compileTestJava.options.compilerArgs = ['-Xlint:all'] repositories { jcenter() } dependencies { testCompile("org.spockframework:spock-core:1.0-groovy-2.4") }
apply plugin: 'groovy'
を追加します。sourceCompatibility = 1.5
→sourceCompatibility = 1.8
へ変更します。targetCompatibility = 1.8
を追加します。- 以下の3行を追加します。
compileJava.options.compilerArgs = ['-Xlint:all']
compileTestGroovy.options.compilerArgs = ['-Xlint:all']
compileTestJava.options.compilerArgs = ['-Xlint:all']
mavenCentral()
→jcenter()
へ変更します。testCompile group: 'junit', name: 'junit', version: '4.11'
を削除し、testCompile("org.spockframework:spock-core:1.0-groovy-2.4")
を追加します。
■その2
group 'ksbysample.library' version '1.0-SNAPSHOT' apply plugin: 'java' 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() } dependencies { testCompile("org.spockframework:spock-core:1.0-groovy-2.4") } 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) } } } }
apply plugin: 'maven'
を追加します。- uploadArchives タスクを追加します。Nexus にアップロードする時のユーザは admin をします。
StringListUtils.java
package ksbysample.library.simpleutils; import java.util.List; import java.util.stream.Collectors; public class StringListUtils { static public String convertStringList2LengthCsv(List<String> stringList) { String result = null; if (stringList != null) { result = stringList.stream() .map(s -> s == null ? "0" : Integer.toString(s.length())) .collect(Collectors.joining(",")); } return result; } }
StringListUtilsTest.groovy
package ksbysample.library.simpleutils import spock.lang.Specification import spock.lang.Unroll class StringListUtilsTest extends Specification { @Unroll def "ConvertStringList2LengthCsv(#stringList) --> #result"() { expect: StringListUtils.convertStringList2LengthCsv(stringList) == result where: stringList || result null || null [null] || "0" ["a"] || "1" ["テスト"] || "3" ["a", "ab", "abc"] || "1,2,3" ["a", null, "abc"] || "1,0,3" } }
履歴
2016/07/16
初版発行。