[ Mavenページへ戻る ]

Maven 2.0のインストール

Maven 2.0とは

Maven 1.0からかなり大々的に変更があります。

インストール

ダウンロード

まずはMavenを入手します。MavenのホームページからDownloadingのリンクをたどってmaven 2.0のアーカイブファイルをダウンロードします。2007年2月18日現在、以下のファイルが最新版です。

maven-2.0.5-bin.tar.bz2
maven-2.0.5-bin.tar.gz
maven-2.0.5-bin.tar.zip

インストール

どこか適当な場所にアーカイブファイルを展開します。

~$ cd /java
java$ tar xjf ~/maven-2.0.5-bin.tar.bz2
java$ cd maven-2.0.5
maven-2.0.5$ ls
LICENSE.txt  NOTICE.txt  README.txt  bin   conf  core  lib
maven-2.0.5$

設定

環境変数

次の環境変数を設定します。例は、C:\javaの下にmavenアーカイブファイルを展開した場合です。

JAVA_HOMEには使用するJDKのインストールディレクトリを指定しておきます。

環境変数名 設定例(OS:Windows)
PATH
%PATH%;%MAVEN_HOME%\bin
JAVA_HOME
C:\java\jdk1.6.0

インストール確認

maven2のbinディレクトリにあるmvnコマンド(スクリプト)を実行します。

$ mvn --version
Maven version: 2.0.5
$

betaまでのmaven 2.0ではm2コマンドでしたが、正式版ではmvnコマンドに改名され、m2は"DEPRECATED"になっています。

プロジェクトディレクトリの新規作成

初回は、ローカルリポジトリに必要なパッケージをダウンロードするので処理が長くなります。2回目からはダウンロードは行われないので簡単に済みます。

work$ mvn archetype:create -DgroupId=helloagain.kanday -DartifactId=kanday-app
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] org.codehaus.mojo: checking for updates from central
[INFO] -------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:create] (aggregator-style)
[INFO] -------------------------------------------------------------------------
Downloading: http://repo1.maven.org/maven2/plexus/plexus-utils/1.0.3/plexus-util
s-1.0.3.pom
970b downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-container-default/1.0-a
lpha-6/plexus-container-default-1.0-alpha-6.pom
916b downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-containers/1.0.2/plexus
-containers-1.0.2.pom
488b downloaded
Downloading: http://repo1.maven.org/maven2/classworlds/classworlds/1.1-alpha-2/c
lassworlds-1.1-alpha-2.pom
3K downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-utils/1.0.2/plexus-util
s-1.0.2.pom
740b downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-model/2.0-beta
-1/maven-model-2.0-beta-1.pom
1K downloaded
Downloading: http://repo1.maven.org/maven2/plexus/plexus-container-default/1.0-a
lpha-2/plexus-container-default-1.0-alpha-2.pom
170b downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-artifact/2.0-b
eta-1/maven-artifact-2.0-beta-1.pom
711b downloaded
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-plugin-api/2.0
-beta-1/maven-plugin-api-2.0-beta-1.pom
615b downloaded
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus
.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] **************************************************************
[INFO] Starting Jakarta Velocity v1.4
[INFO] RuntimeInstance initializing.
[INFO] Default Properties File: org\apache\velocity\runtime\defaults\velocity.pr
operties
[INFO] Default ResourceManager initializing. (class org.apache.velocity.runtime.
resource.ResourceManagerImpl)
[INFO] Resource Loader Instantiated: org.codehaus.plexus.velocity.ContextClassLo
aderResourceLoader
[INFO] ClasspathResourceLoader : initialization starting.
[INFO] ClasspathResourceLoader : initialization complete.
[INFO] ResourceCache : initialized. (class org.apache.velocity.runtime.resource.
ResourceCacheImpl)
[INFO] Default ResourceManager initialization complete.
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Literal
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Macro
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Parse
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Include
[INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
[INFO] Created: 20 parsers.
[INFO] Velocimacro : initialization starting.
[INFO] Velocimacro : adding VMs from VM library template : VM_global_library.vm
[ERROR] ResourceManager : unable to find resource 'VM_global_library.vm' in any
resource loader.
[INFO] Velocimacro : error using  VM library template VM_global_library.vm : org
.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'V
M_global_library.vm'
[INFO] Velocimacro :  VM library template macro registration complete.
[INFO] Velocimacro : allowInline = true : VMs can be defined inline in templates

[INFO] Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT
replace previous VM definitions
[INFO] Velocimacro : allowInlineLocal = false : VMs defined inline will be  glob
al in scope if allowed.
[INFO] Velocimacro : messages on  : VM system will output logging messages
[INFO] Velocimacro : autoload off  : VM system will not automatically reload glo
bal library macros
[INFO] Velocimacro : initialization complete.
[INFO] Velocity successfully started.
[INFO] [archetype:create]
[INFO] Defaulting package to group ID: helloagain.kanday
[INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking
 for updates from central
[INFO] -------------------------------------------------------------------------
[INFO] Using following parameters for creating Archetype: maven-archetype-quicks
tart:RELEASE
[INFO] -------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: helloagain.kanday
[INFO] Parameter: outputDirectory, Value: E:\home\toru\study\javaw\maven\hello_m
aven_2.0\kanday-app
[INFO] Parameter: packageName, Value: helloagain.kanday
[INFO] Parameter: package, Value: helloagain.kanday
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: artifactId, Value: kanday-app
[INFO] ResourceManager : found archetype-resources/pom.xml with loader org.codeh
aus.plexus.velocity.ContextClassLoaderResourceLoader
[INFO] ********************* End of debug info from resources from generated POM
 ***********************
[INFO] ResourceManager : found archetype-resources/src/main/java/App.java with l
oader org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader
[INFO] ResourceManager : found archetype-resources/src/test/java/AppTest.java wi
th loader org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader
[INFO] Archetype created in dir: D:\home\torutk\work\kanday-app
[INFO] -------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] -------------------------------------------------------------------------
[INFO] Total time: 26 seconds
[INFO] Finished at: Sun Oct 23 00:10:48 JST 2005
[INFO] Final Memory: 4M/7M
[INFO] -------------------------------------------------------------------------
work$

mavenは開発作業を行う際、標準ディレクトリ構成を持っています。これを変更することも可能ですが、労力が多いが益は少ないので、デフォルトの構成を使用します。デフォルト構成は、m2コマンドに"archetype:create"を指定すれば作成してくれます。

archetype:create生成したプロジェクトディレクトリ
work
  +--- kanday-app
        +--- pom.xml
        +--- src
              +--- main
              |     +--- java
              |           +--- helloagain
              |                 +--- kanday
              |                       +--- App.java
              +--- test
                    +--- java
                          +--- helloagain
                                +--- kanday
                                      +--- AppTest.java

ここで生成されたApp.javaは、"Hello World!"をプリントするクラスです。

注記1
-DgroupId=helloagain.kandayで指定したパッケージが生成されています。
注記2
mavenや依存関係で指定したライブラリファイルをダウンロードして保持するローカルリポジトリの場所は、デフォルトでは${user.home}/.m2ディレクトリの中です。Windows 2000での典型的な場所は、C:\Documents and Setting\ユーザ名\.m2 になります。コマンドライン環境では、環境変数USERPROFILEを使って%USERPROFILE%\.m2などでアクセスできます。

プロジェクトのビルド

compile

mavenにcompileを与えます。targetというディレクトリが生成され、その中にコンパイルしたクラスファイルが生成されています。

compileで生成したプロジェクトディレクトリ
work
  +--- kanday-app
        +--- pom.xml
        +--- src
        |     +--- main
        |     |     +--- java
        |     |           +--- helloagain
        |     |                 +--- kanday
        |     |                       +--- App.java
        |     +--- test
        |           +--- java
        |                 +--- helloagain
        |                       +--- kanday
        |                             +--- AppTest.java
        +--- target
              +--- classes
                    +--- helloagain
                          +--- kanday
                                +--- App.class

mavenコマンドでcompileを実行した例です。

kanday-app$ mvn compile
[INFO] ----------------------------------------------------------------------------
[INFO] Building Maven Quick Start Archetype
[INFO]    task-segment: [compile]
[INFO] ----------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] [compiler:compile]
Compiling 1 source file to D:\home\torutk\work\kanday-app\target\classes
[INFO] ----------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Mon Sep 19 20:54:28 JST 2005
[INFO] Final Memory: 3M/6M
[INFO] ----------------------------------------------------------------------------
kanday-app$ 

install

mavenにinstallを与えます。targetというディレクトリが生成され、その中にコンパイルしたクラスファイル、JARファイル、テストクラス、テスト結果が生成されています。また、ローカルリポジトリに生成したJARファイルがコピーされます。

installで生成したプロジェクトディレクトリ
work
  +--- kanday-app
        +--- pom.xml
        +--- src
        |     +--- main
        |     |     +--- java
        |     |           +--- helloagain
        |     |                 +--- kanday
        |     |                       +--- App.java
        |     +--- test
        |           +--- java
        |                 +--- helloagain
        |                       +--- kanday
        |                             +--- AppTest.java
        +--- target
              +--- classes
              |     +--- helloagain
              |           +--- kanday
              |                 +--- App.class
              +--- exported-pom.xml
              +--- kanday-app-1.0-SNAPSHOT.jar
              +--- surefire-reports
              |     +--- helloagain.kanday.AppTest.txt
              +--- test-classes
                    +--- helloagain
                          +--- kanday
                                +--- AppTest.class

%USERPROFILE%
  +--- .m2
        +--- plugin-registry.xml
        +--- repository
        |     +--- helloagain
        |           +--- kanday
        |                 +--- kanday-app
        |                       +--- 1.0-SNAPSHOT
        |                             +--- kanday-app-1.0-SNAPSHOT.jar
        |                             +--- kanday-app-1.0-SNAPSHOT.pom
        |                             +--- maven-metadata-local.xml
        +--- classworlds
        +--- commons-collections
        +--- commons-io 
        +--- commons-logging
        +--- junit
        +--- log4j
        +--- logkit
        +--- org
        +--- plexus
        +--- surefire
        +--- velocity

mavenコマンドでinstallを実行した例です。

kanday-app$ mvn install
[INFO] ----------------------------------------------------------------------------
[INFO] Building Maven Quick Start Archetype
[INFO]    task-segment: [install]
[INFO] ----------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] [compiler:testCompile]
Compiling 1 source file to D:\home\torutk\work\kanday-app\target\test-classes
[INFO] [surefire:test]
[INFO] Setting reports dir: D:\home\torutk\work\kanday-app\target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
[surefire] Running helloagain.kanday.AppTest
[surefire] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0 sec

Results :
[surefire] Tests run: 1, Failures: 0, Errors: 0

[INFO] [jar:jar]
[INFO] Building jar: D:\home\torutk\work\kanday-app\target\kanday-app-1.0-SNAPSHOT.jar
[INFO] [install:install]
[INFO] Installing D:\home\torutk\work\kanday-app\target\kanday-app-1.0-SNAPSHOT.jar to 
C:\Documents and Settings\torutk\.m2\repository\helloagain\kanday\kanday-app\1.0-SNAPSHOT\
kanday-app-1.0-SNAPSHOT.jar
[INFO] ----------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ----------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Mon Sep 19 20:57:45 JST 2005
[INFO] Final Memory: 3M/6M
[INFO] ----------------------------------------------------------------------------
kanday-app$ 
クリーンインストール

mavenに clean:clean install を与えます。targetディレクトリ以下を削除してからinstallを実行します。

m2 clean:clean だけ実行してもtargetディレクトリ以下が削除できます。

Mavenのディレクトリ・ポリシー

ディレクトリ構造は人それぞれでしょうが、mavenを使用する場合はmavenのポリシーを知っておく必要があります。どうしても気に入らないようなら自分でプロジェクト設定をがりがりと記述すればよいと思いますが・・・。

通常のアプリケーション

ソースファイル等

上述でmavenにディレクトリを生成させると分かりますが、ソースファイル等はsrcの下に収められます。ソースファイル等は、製品ソースファイル、テスト用ソースファイルの2つに分類されます。

src main java
resources
test java
resources

コンパイルした結果

ソースファイルをビルドした結果はtargetの下に格納されます。ビルド結果は、製品クラスファイル、テスト用クラスファイル、テスト結果、製品JARファイルに分類されます。

target torutk-app-1.0-SNAPSHOT.jar
classes
surefire-reports
test-classes

Mavenのゴール

mavenは、いくつかの定義済みのゴールを持っており、m2コマンドでゴールを指定することによってゴールを達成するために必要な作業を開始します。

定義されているゴールを一覧する

以下に主要なゴールとその内容を一覧表にして示します。

mavenゴール 省略形 内容
assemply:assembly
clean:clean clean
compiler:compile compile
compiler:testCompile
deploy:deploy 配布用のバイナリパッケージ、ソースパッケージを作成する
ejb:ejb
idea:idea
install:install
jar:jar
plugin:descriptor
resources:resources
resources:testResources
surefire:test
war:war

Mavenあれこれ

コンパイルに関して

ソースオプション、ターゲットオプションの指定

javacコマンドの-sourceオプション、-targetオプションを指定する方法は次のとおりです。

pom.xml
<project ...>
  :
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  :
</project>

JARファイル生成に関して

MANIFEST.MFの内容

デフォルトの設定でJARファイルを生成すると、META-INF/MANIFEST.MFは次の内容となっています。

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: toru
Build-Jdk: 1.6.0-beta-torutk
Extension-Name: kanday-app
Implementation-Title: kanday-app
Implementation-Version: 1.0-SNAPSHOT

実行可能JARファイルを生成する

MANIFEST.MFに、Main-Class: XXX を追加します。pom.xmlに以下の記述を追加します。

pom.xml
<project ...>
  :
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>helloagain.kanday.KandayMain</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
  :
</project>

MANIFESTファイルを指定

Mavenが生成するMANIFEST.MFにあらかじめ作成しておいたMANIFESTファイルの内容を使用することができます。標準以外のマニフェスト属性を使用する場合や自動生成する場合に使うと便利です。

pom.xml
<project ...>
  :
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifestFile>src/main/scripts/myapp.mani</manifestFile>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
  :
</project>

参考

日本語

英語

謝辞


This page is written by Toru TAKAHASHI.(torutk@02.246.ne.jp)