追記)
Nexus 3を使って、Docker Registryも含めたエントリを書きました。
Sonatype Nexus 3を使用して、PrivateなMavenリポジトリ、npm Registry、Docker Registryを構築する - CLOVER
今まで、自分のローカルMavenリポジトリはApacheを使って単にWebアクセスができる程度にしていたのですが、ちゃんとしたMavenリポジトリのツールを導入したいと思いまして。
また、npmで使うリポジトリも入れられるといいなぁとも思いまして。
こう考えた時に、候補に挙がるのは以下の2つです。
なんとなく、最近の情報を見ているとArtifactoryがいいのかな?と思っていたのですが、Artifactoryは対応リポジトリは多いのですけれど、OSS版だとnpmが使えない(Dockerも…)ので、今回はNexusにすることにしました。
ドキュメントはこちらです。
https://books.sonatype.com/nexus-book/index.html
インストール
それでは、Nexusをインストールします。
基本的には、以下の手順やドキュメントを参照しつつ進めていきます。
前提としては、JDK 7 or 8が入っていればいいみたいです。
Nexusをダウンロードして展開します。
$ wget http://www.sonatype.org/downloads/nexus-latest-bundle.tar.gz
$ tar -zxvf nexus-latest-bundle.tar.gz
今回のバージョンは、「2.11.4-01」でした。
実行は、以下のコマンドで行います。
$ nexus-2.11.4-01/bin/nexus start Starting Nexus OSS... Started Nexus OSS.
ログは、以下のディレクトリにあったりします。
sonatype-work/nexus/logs
サービスとして動作させる場合は、以下を参照すればよさそうですね。
アクセスしてみる
Nexusにアクセスするには、Nexusを起動した状態で以下のURLにアクセスします。
右の方にログインへのリンクがあるので、とりあえずログインするには
admin / admin123
とユーザー名とパスワードをそれぞれ入力します。
現在のリポジトリ一覧は、左の「Views/Repositories」から「Repositories」を選択すれば見ることができます。
とりあえず、初期導入的にはここまで?
ローカルMavenリポジトリを作るにあたって、プロキシリポジトリを使う
で、自分はネットワーク構成の都合から、外部アクセスができない場所にMavenリポジトリを配置せざるをえないパターンが多いのですが、Mavenの場合は各種ゴールとローカルリポジトリの指定でJARなどを集め、それをApacheとかにポンっと置くような構成を取っていました。
これ、プロキシリポジトリを作って、それ経由で集めると少し楽だったり…(Mavenの場合はしないかもしれない…)。
というわけで、「Repositories」から「Add」を選び、「Proxy Repository」を選択してCentralに対するプロキシリポジトリを作成します。
CentralへのURLは、
https://repo1.maven.org/maven2/
ですね。
設定したら、Saveで作成。今回は、idを「proxied-maven-repo」としたので、アクセスするためのURLは
http://localhost:8081/nexus/content/repositories/proxied-maven-repo/
となります。
あとは、pom.xmlのリポジトリやプラグインリポジトリの設定に書いておけば、プロキシリポジトリ経由でアーティファクトをダウンロードしてきます。
<repositories> <repository> <id>my-repo</id> <url>http://localhost:8081/nexus/content/repositories/proxied-maven-repo/</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>my-repo</id> <url>http://localhost:8081/nexus/content/repositories/proxied-maven-repo/</url> </pluginRepository> </pluginRepositories>
この状態で、「mvn package」などすると、作成したリポジトリをプロキシしてアーティファクトをダウンロードしてきます。
この時、プロキシリポジトリ経由でアクセスされたアーティファクトがプロキシリポジトリにキャッシュされます。
$ ll sonatype-work/nexus/storage/proxied-maven-repo total 52 drwxr-xr-x 12 nexus nexus 4096 Dec 20 13:29 ./ drwxr-xr-x 11 nexus nexus 4096 Dec 20 13:27 ../ drwxr-xr-x 2 nexus nexus 4096 Dec 20 13:27 .meta/ drwxr-xr-x 4 nexus nexus 4096 Dec 20 13:27 .nexus/ -rw-r--r-- 1 nexus nexus 25 Dec 20 13:27 archetype-catalog.xml drwxr-xr-x 8 nexus nexus 4096 Dec 20 13:29 com/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:29 commons-cli/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:29 commons-codec/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:29 commons-collections/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:29 commons-configuration/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:29 commons-fileupload/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:29 commons-io/ drwxr-xr-x 4 nexus nexus 4096 Dec 20 13:29 org/
これでもいいんじゃ?
npmのプロキシリポジトリを作る
今度は、npmのプロキシリポジトリを作成します。
npmリポジトリへのアクセスURLはこちらなので、
参考)
registry | npm Documentation
こちらを設定したnpmのプロキシリポジトリを作成。Providerは、「npm」を選びます。
idは「proxied-npm-repo」としたので、アクセスするリポジトリのURLは以下となります。
http://localhost:8081/nexus/content/repositories/proxied-npm-repo/
これを「$HOME/.npmrc」というファイルの中に(なければ作成します)、以下のように記述します。
registry = http://localhost:8081/nexus/content/repositories/proxied-npm-repo/
この状態で、試しに
$ npm install -g gulp browserify
とかすると、Nexus経由でダウンロードが行われ、リポジトリ側にも以下のようにファイルが作成されます。
$ ll sonatype-work/nexus/storage/proxied-npm-repo total 928 drwxr-xr-x 230 nexus nexus 12288 Dec 20 13:55 ./ drwxr-xr-x 12 nexus nexus 4096 Dec 20 13:53 ../ drwxr-xr-x 4 nexus nexus 4096 Dec 20 13:55 .nexus/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 JSONStream/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 acorn/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 amdefine/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 ansi-regex/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 ansi-styles/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 archy/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 array-differ/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 array-filter/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 array-map/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 array-reduce/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 array-uniq/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 asn1.js/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 assert/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 astw/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 balanced-match/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 base64-js/ drwxr-xr-x 3 nexus nexus 4096 Dec 20 13:55 beeper/ 〜省略〜
とりあえず、使えそうですね。
実際にNexus経由でダウンロードされているかを確認するには、以下のファイルを見るとよいと思います。
sonatype-work/nexus/logs/request.log