0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ant と Maven ① Apache Ivy の導入

Posted at

 こちらは「Ant発展 ②JUnit連携」の続きです。

 目次記事はこちらです。

Maven リポジトリとは!!!

 「MavenとGradleを理解する」が目的のこの記事も、ようやく Maven という単語が現れるところまで来ました(長すぎですね:disappointed_relieved:

 まずは Maven の理解の入り口である Maven リポジトリについて理解します。

「依存」という考え

 Ant のプロジェクトのサンプルとして FallModelProject なるプロジェクトを作成してきました。このプロジェクトは lib フォルダに外部ライブラリ(jar ファイル)を保存して、そのライブラリを参照する形で動作しています。
 このようにそれ単体では実行できないプログラムを、他のライブラリに依存しているって言い方をします。

 現状は5つのライブラリに依存したプロジェクトになっていますね。

image.png

 外部ライブラリを利用するために、今まではライブラリを公開しているサイトまで出向いて、jarファイルをダウンロードしてコピーしてきました。めんどくさかったですよね。

 そこで頭のいい人たちは、「ライブラリがたくさん集まった保管場所をインターネット上に用意して、依存関係の解決はそのプロジェクトが持つビルドファイルが担えばいいんじゃないか?」と考えました。

 ビルドファイルとは、Makefile や build.xml といったファイルたちです。

 この「インターネット上に用意された、ライブラリがたくさん集まった保管場所」が Mavenリポジトリ です。
 (正確にはライブラリとプラグインってやつが集まった場所なのですが、それはのちほど)

 Maven リポジトリを利用すると、いちいち jar ファイルをダウンロードする手間が省けます。

Apache Ivy

 便利な Maven リポジトリですが、Ant でこれを利用する標準のタグはありません。
 Ant から Maven リポジトリを利用するには Apache Ivy( Ivy はアイヴィーと読む。以下 Ivy) が非常に便利です。
 Ivy はそれ単体でも有用なのですが、今回は「 Ant と連携させて Maven リポジトリを利用する」という一点に絞って扱います。

Ivy の導入

 まずは下記のサイトから Ivy をダウンロードします。

image.png

 ダウンロードした zip ファイルを展開し、展開後に現れたフォルダを開くと ivy-2.5.3.jar というライブラリが見つかります。ivy-2.5.3.jar を apache-ant-1.10.15 フォルダの下の lib フォルダにコピーします。
(Cドライブ直下にtoolsフォルダを作って、その中に apache-ant-1.10.15 がある場合は C:\tools\apache-ant-1.10.15\lib というパスでアクセスできます)

image.png

 イメージとしては、「Ant というソフトウェアに Ivy というプラグイン(拡張機能)を追加した」という感じですね。

プロキシ環境下で使用する場合

 プロキシサーバを経由する場合、環境変数の設定が必要になります。
 
 変数名:ANT_OPTS
 値:-Dhttp.proxyHost=サーバのIP -Dhttp.proxyPort=ポート番号 -Dhttps.proxyHost=サーバのIP -Dhttps.proxyPort=ポート番号

 Windows であれば、環境変数の設定に飛んで下記画像のように入力します。

image.png

 以上で Ivy を使う準備ができました。

Ivy のサンプルプロジェクトを動かしてみる

 Ivy の動作確認もかねて、サンプルプロジェクトを動かしてみます。先ほど解凍した Ivy のフォルダを見ると src というフォルダがあります。それをダブルクリックすると example というフォルダがあり、その中にサンプルプロジェクトが入っています。

image.png

 動作確認のため、「hello-ivy」というプロジェクトを実行します。この記事ではわかりやすくするために hello-ivy プロジェクトをデスクトップに移動しておきます。

 hello-ivy プロジェクトの中身を確認します。ファイルエクスプローラで開くと次のようになっていることがわかります。

image.png

ツリー構造
hello-ivy
├── src
│   └── example
│       └── HelloConsole.java
├── build.xml
└── ivy.xml

 めっちゃシンプルな Ant プロジェクトですね。次に HelloConsole.java の中身を見てみます。

HelloConsole.java
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package example;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.lang.WordUtils;

/**
 * Simple example to show how easy it is to retrieve transitive libs with ivy !!!
 */
public final class HelloConsole {
    public static void main(String[] args) throws Exception {
        Option msg = Option.builder("m")
            .longOpt("message")
            .hasArg()
            .desc("the message to capitalize")
            .build();
        Options options = new Options();
        options.addOption(msg);

        CommandLineParser parser = new DefaultParser();
        CommandLine line = parser.parse(options, args);

        String  message = line.getOptionValue("m", "Hello Ivy!");
        System.out.println("standard message : " + message);
        System.out.println("capitalized by " + WordUtils.class.getName()
            + " : " + WordUtils.capitalizeFully(message));
    }

    private HelloConsole() {
    }
}

 初めにあるコメントは Apache のライセンスのお話でプログラムには関係ありません。注目すべきはたくさん書いてある import 文です。
 ライブラリないけどこれ実行できるんか!?:rage:って感じですよね。フォルダツリーを見て明らかに、jar ファイルなんてかけらも存在していません。

 コマンドプロンプトを起動して hello-ivy フォルダに移動します。もしデスクトップに hello-ivy があるなら次のコマンドを入力します。

cd C:\Users\[ユーザ名]\Desktop\hello-ivy

 ここで ant コマンドを実行してみましょう。すると少し長い時間がかかり、次のような表示が得られたと思います。

C:\Users\[ユーザ名]\Desktop\hello-ivy>ant
Buildfile: C:\Users\[ユーザ名]\Desktop\hello-ivy\build.xml

resolve:
[ivy:retrieve] :: Apache Ivy 2.5.3 - 20241223125031 :: https://ant.apache.org/ivy/ ::
[ivy:retrieve] :: loading settings :: url = jar:file:/C:/tools/apache-ant-1.10.15/lib/ivy-2.5.3.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: org.apache#hello-ivy;working@DESKTOP-OJ0QPQB
[ivy:retrieve]  confs: [default]
[ivy:retrieve]  found commons-lang#commons-lang;2.6 in public
[ivy:retrieve]  found junit#junit;3.8.1 in public
[ivy:retrieve]  found commons-cli#commons-cli;1.4 in public
[ivy:retrieve]  found junit#junit;4.12 in public
[ivy:retrieve]  found org.hamcrest#hamcrest-core;1.3 in public
[ivy:retrieve] downloading https://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6-javadoc.jar ...
[ivy:retrieve] ..................................................................................................... (1585kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve]  [SUCCESSFUL ] commons-lang#commons-lang;2.6!commons-lang.jar(javadoc) (1228ms)
[ivy:retrieve] downloading https://repo1.maven.org/maven2/commons-lang/commons-lang/2.6/commons-lang-2.6-sources.jar ...
[ivy:retrieve] ........................ (364kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve]  [SUCCESSFUL ] commons-lang#commons-lang;2.6!commons-lang.jar(source) 
~~~~中略~~~~
[ivy:retrieve] downloading https://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar ...
[ivy:retrieve] .... (43kB)
[ivy:retrieve] .. (0kB)
[ivy:retrieve]  [SUCCESSFUL ] org.hamcrest#hamcrest-core;1.3!hamcrest-core.jar (582ms)
[ivy:retrieve] :: resolution report :: resolve 16107ms :: artifacts dl 6090ms
[ivy:retrieve]  :: evicted modules:
[ivy:retrieve]  junit#junit;3.8.1 by [junit#junit;4.12] in [default]
        ---------------------------------------------------------------------
        |                  |            modules            ||   artifacts   |
        |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
        ---------------------------------------------------------------------
        |      default     |   5   |   5   |   5   |   1   ||   8   |   8   |
        ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: org.apache#hello-ivy
[ivy:retrieve]  confs: [default]
[ivy:retrieve]  0 artifacts copied, 8 already retrieved (0kB/15ms)

run:
WARNING: A terminally deprecated method in java.lang.System has been called
WARNING: System::setSecurityManager has been called by org.apache.tools.ant.types.Permissions (file:/C:/tools/apache-ant-1.10.15/lib/ant.jar)
WARNING: Please consider reporting this to the maintainers of org.apache.tools.ant.types.Permissions
WARNING: System::setSecurityManager will be removed in a future release
     [java] standard message : hello ivy !
     [java] capitalized by org.apache.commons.lang.WordUtils : Hello Ivy !

BUILD SUCCESSFUL
Total time: 23 seconds

 Ant の結果は BUILD SUCCESSFUL でした! jar ファイルないのになぜ!

 ant コマンド実行後、改めてフォルダのツリーを確認します。

hello-ivy
├── build
│   └── example
│       └── HelloConsole.class
├── lib
│   ├── commons-cli-1.4-javadoc.jar
│   ├── commons-cli-1.4-sources.jar
│   ├── commons-cli-1.4.jar
│   ├── commons-lang-2.6-javadoc.jar
│   ├── commons-lang-2.6-sources.jar
│   ├── commons-lang-2.6.jar
│   ├── hamcrest-core-1.3.jar
│   └── junit-4.12.jar
├── src
│   └── example
│       └── HelloConsole.java
├── build.xml
└── ivy.xml

 さっきまでありもしなかったライブラリたちが、勝手に新規作成された lib フォルダの中にいる!

 このように Ivy では足りないライブラリを Maven リポジトリからダウンロードしてくれます。

Ivy のキャッシュ

 早く build.xml や謎の Ivy.xml を見たいところですがそれは次の記事で行います。ここでもう少し Ivy の動きを確認しておきます。

 再度 ant コマンドを実行してみます。今度は何かをダウンロードする様子もなくサクッと終わったかと思います。
 Ivy ではダウンロード済みのライブラリを再度ダウンロードすることはありません

 ここで大事なのが、ダウンロードしたライブラリの保管場所はプロジェクトの中じゃないということです。試しに hello-ivy プロジェクトの lib フォルダを削除します。

image.png

 この状態で ant コマンドを実行してみましょう。lib フォルダを削除下にもかかわらず、すぐに ant の実行が終わったと思います。一応確認すると、hello-ivy プロジェクトの中で lib フォルダが復活し、ライブラリが入っていることがわかります。

image.png

 これは Ivy が持つキャッシュCache)という機能によります。これは Maven でいうところのローカルリポジトリという概念とほぼほぼ同義で対応します。

 Ivy はプロジェクト内にないライブラリの依存関係を見つけると、まず自身のキャッシュを見に行きます。そこにライブラリが転がっていればコピーしてきます。ライブラリがキャッシュに見つからない場合、Maven リポジトリからライブラリをダウンロードしてきます。

 Ivy のキャッシュのパスはデフォルトで、C:\Users\[ユーザ名]\.ivy2\cache です。.ivy2 は隠しフォルダなので、隠しフォルダを表示する設定にしてから確認してください。
 cache フォルダの中身を確認すると次のようになっています。

image.png

 今後どのプロジェクトでも、キャッシュにあるライブラリを利用する場合は Ivy がここからコピーしてきます。このおかげで 2 回目以降の ant コマンドは常に高速に終了していたのですね。

 試しに cache フォルダの中身を削除してみます。

image.png

 この状態で、再度 hello-ivy プロジェクトで ant コマンドを実行すると、また長い時間をかけてダウンロードが開始します。

 次の記事で build.xml の中身とか見ていきます。

次の記事

作成中

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?