hatenob

プログラムって分からないことだらけ

一人Web開発~第7夜 Nexusの導入

アプリケーションを作る、前に、ライブラリ管理できる仕組みを整えておくことにします。
ライブラリ管理にはMavenを使用し、Mavenリポジトリを管理するために、Sonatype NexusのOSS版を使用します。
Nexusは、Mavenリポジトリのホストと、セントラルリポジトリ等へのプロキシが可能です。自分で作ったライブラリを

$ mvn deploy

することで、ライブラリとして登録でき、他のアプリケーションから使用することができるようになるわけです。
共用ライブラリなんかを作った場合には、そのjarファイルなんかをコピーして使いまわしたり、Subversionなんかのソースコードリポジトリに登録して個々が取得したりなんてことがよくあるわけですが、Mavenリポジトリに公開することで自作ライブラリもMavenの管理下におけるので、より簡単に共用ライブラリの配布や管理ができるようになります。

Nexusには、スタンドアロン版とwar版の2つが用意されています。スタンドアロン版はjettyが同梱されていてそれで動作します。war版は、TomcatとJettyで動作確認がされたwarファイルです。もちろん、自己責任において他のJavaEEコンテナにもデプロイできるのでしょうが、今回はスタンドアロン版を使用することにしました。

Nexusのインストール

もちろんNexusもChefを使ってインストールします。

group "nexus" do
  action :create
end

user "nexus" do
  supports :manage_home => true
  home "/home/nexus"
  gid "nexus"
  action :create
end

# Install Nexus

remote_file node[:nexus][:file] do
  source node[:nexus][:url]
  action :create_if_missing
end

directory "/opt/nexus" do
  action :create
  mode 00755
  owner "nexus"
  group "nexus"
end

bash "install_nexus" do
  cwd "/opt/nexus"
  user "nexus"
  group "nexus"
  code <<-EOH
    tar --no-same-owner -xzf #{node[:nexus][:file]}
  EOH
  not_if { File.exist?("/opt/nexus/nexus-#{node[:nexus][:version]}-01") }
end

link "/opt/nexus/available" do
  to "/opt/nexus/nexus-#{node[:nexus][:version]}-01"
  user "nexus"
  group "nexus"
end

# Setup Nexus

link "/etc/init.d/nexus" do
  to "/opt/nexus/available/bin/nexus"
end

directory "/var/run/nexus" do
  action :create
  mode 00755
  owner "nexus"
  group "nexus"
end

directory "/var/log/nexus" do
  action :create
  mode 00755
  owner "nexus"
  group "nexus"
end

# Setup conf

template "/opt/nexus/available/conf/nexus.properties" do
  source "nexus.properties.erb"
  mode 00644
  owner "nexus"
  group "nexus"
end

template "/opt/nexus/available/bin/nexus" do
  source "nexus.erb"
  mode 00755
  owner "nexus"
  group "nexus"
end

template "/opt/nexus/available/bin/jsw/conf/wrapper.conf" do
  source "wrapper.conf.erb"
  mode 00644
  owner "nexus"
  group "nexus"
end

今までのプロダクトのインストールとさほど変わらず、実行ユーザ作ったりディレクトリ作ったり、バイナリをダウンロードして展開したり、といった内容です。
今回は、インストールだけをChefで自動化し、実際のリポジトリ設定はNexusの管理画面を使ってGUIでやっております。そのあたりは下記のNexusのマニュアルを参照されたし。
Repository Management with Nexus - 3.6. Post-Install Checklist - Sonatype.com
公式のドキュメントで詳細に書かれているので、本格的な利用をする方は必読ですね。
ちなみに私はインストールのところ以外は読んでません。。

Mavenのsetting.xml設定

アプリケーションを開発するローカルのMavenでは、このNexusをMavenリポジトリとして見させるための設定が必要です。
$MVN_HOME/conf/settings.xmlを編集します。

(省略)
<servers>
  <server>
    <id>nexus</id>
    <username>deployment</username>
    <password>deployment123</password>
  </server>
</servers>
(省略)
<mirrors>
  <mirror>
    <id>nexus</id>
    <mirrorOf>*</mirrorOf>
   <url>http://nexushost:8081/nexus/content/groups/public/</url>
  </mirror>
</mirrors>
(省略)
<profile>
  <id>oneman-repository</id>
  <repositories>
    <repository>
      <id>oneman-repository</id>
      <name>oneman repository</name>
      <url>http://nexushost:8081/nexus/content/groups/public</url>
      <layout>default</layout>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
        <updatePolicy>never</updatePolicy>
      </snapshots>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>oneman-repository</id>
      <name>oneman repository</name>
      <url>http://nexushost:8081/nexus/content/groups/public</url>
      <layout>default</layout>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
        <updatePolicy>never</updatePolicy>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>
</profile>
(省略)
<activeProfiles>
 <activeProfile>oneman-repository</activeProfile>
</activeProfiles>

これで、ローカルでmvnコマンドをたたいた時に、Nexusからライブラリのダウンロードを行うことができます。
あと、最初にタグで書いているユーザとパスワードを使用して、リポジトリにデプロイ(ライブラリの公開)ができるようになります。 そのためには、作成アプリケーションのpom.xmlに下記を記載しておく必要があるようです。

<distributionManagement>
	<repository>
		<id>nexus</id>
		<name>Releases</name>
	<url>http://nexushost:8081/nexus/content/repositories/releases/</url>
	</repository>
	<snapshotRepository>
		<id>nexus</id>
		<name>Snapshots</name>
		<url>http://nexushost:8081/nexus/content/repositories/snapshots/</url>
	</snapshotRepository>
</distributionManagement>

で指定するのは、先のsettings.xmlに記載したのよです。 これで、アプリケーション側で

$ mvn deploy

をすると、NexusのMavenリポジトリに公開されます。
対象のアプリケーションのバージョンが0.0.1のように固定バージョンであればreleaseリポジトリにそのバージョンで登録されます。デフォルトだと上書きできない設定のようなので、Nexus側で適宜上書き可能に変更してやるのがいいかもしれません。
0.0.1-SNAPSHOTとなっていると、snapshotリポジトリに、0.0.1-(タイムスタンプ)の形で登録されます。

これで今度こそ、アプリケーションのプログラミング編に移ります。