[ Mavenページへ戻る ]
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"を指定すれば作成してくれます。
work +--- kanday-app +--- pom.xml +--- src +--- main | +--- java | +--- helloagain | +--- kanday | +--- App.java +--- test +--- java +--- helloagain +--- kanday +--- AppTest.java |
ここで生成されたApp.javaは、"Hello World!"をプリントするクラスです。
mavenにcompileを与えます。targetというディレクトリが生成され、その中にコンパイルしたクラスファイルが生成されています。
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$
mavenにinstallを与えます。targetというディレクトリが生成され、その中にコンパイルしたクラスファイル、JARファイル、テストクラス、テスト結果が生成されています。また、ローカルリポジトリに生成したJARファイルがコピーされます。
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にディレクトリを生成させると分かりますが、ソースファイル等はsrcの下に収められます。ソースファイル等は、製品ソースファイル、テスト用ソースファイルの2つに分類されます。
src | main | java |
resources | ||
test | java | |
resources |
ソースファイルをビルドした結果はtargetの下に格納されます。ビルド結果は、製品クラスファイル、テスト用クラスファイル、テスト結果、製品JARファイルに分類されます。
target | torutk-app-1.0-SNAPSHOT.jar | |
classes | ||
surefire-reports | ||
test-classes |
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 |
javacコマンドの-sourceオプション、-targetオプションを指定する方法は次のとおりです。
<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ファイルを生成すると、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 |
MANIFEST.MFに、Main-Class: XXX を追加します。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> |
Mavenが生成するMANIFEST.MFにあらかじめ作成しておいたMANIFESTファイルの内容を使用することができます。標準以外のマニフェスト属性を使用する場合や自動生成する場合に使うと便利です。
<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> |