maven の使い方

Menu Menu

maven は、make や ant と同じ program の作り方を管理するプログラム。

maven は、決まった directory 構造を持っている。

    bin
    src
    target


pom.xml

pom.xml に作り方が書かれる。使用する jar などもここに記述することになる。


作り方

いきなり pom.xml を書いてもよいが、

Eclipse から、m2e (maven plugin) を使って作成するのが簡単。


m2e plug in の導入

Eclipse の Help -> Market place -> Find

で、maven を検索。

Maven Integration を Install

License を accept して、Next (結構、時間がかかります)


Maven Project の作り方

new から maven project を作るのは、割と面倒。そのかわり、まず、普通の Java Project を作成する。(この時に、main を作る option を入れると便利)

それから、Project を control click

Configure から、Convert to maven project を選択

これで、directory が設定されて、pom.xml ができます。

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

を手動で追加しないと Warning が出ます。


使い方

普通に Eclipse で build して問題ない。

command line からは、workspace で、

  % mvn

とすると、ERROR で phase の一覧が出る。この phase のどれかを実行する。


jar の作り方

  % mvn package

とすると、jar ファイルが作成される。

  % jar tvf target/*.jar

で中身を確認しよう。

pom.xml に

      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
         <configuration>
       <archive>
         <manifest>
           <mainClass>fileWrite.FileWrite</mainClass>
           <packageName>fileWrite.FileWrite</packageName>
         </manifest>
         <manifestEntries>
           <mode>development</mode>
           <url>${pom.url}</url>
         </manifestEntries>
       </archive>
         </configuration>
     </plugin>

を、plugins の中に追加する。
           <mainClass>fileWrite.FileWrite</mainClass>

を実行したい main を持つ class にする。

  % java -jar target/*.jar

で実行できるはず。


最後に

JUnit を追加した時には、maven を Eclipse から reconfigure すること

pom.xml を Mercurial に add するのを忘れないこと。


Shinji KONO / Tue Oct 22 16:45:01 2013