ぺーぺーSEのブログ

備忘録・メモ用サイト。

Maven2、Maven3のPOMの書き方まとめ〜依存関係設定編〜

参考:http://maven.apache.org/pom.html

blog.pepese.com








■概要

<project ...>

  <dependencies>
    <dependency>
      <groupId>...</groupId>
      <artifactId>...</artifactId>
      <version>...</version>
      <type>...</type>
      <scope>...</scope>
      <optional>...</optional>
      <exclusions>
        <exclusion>
          <groupId>...</groupId>
          <artifactId>...</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <relativePath>...</relativePath>
  </parent>

  <dependencyManagement>
    <dependencies>...</dependencies>
  </dependencyManagement>

  <modules>
    <module>...</module>
  </modules>

  <properties>...</properties>

</project>

dependencies-dependency要素

  • groupId、artifactId、version要素
    • 参照したいライブラリのグループID、アーティファクトID、バージョンを指定する。
    • わからない場合はhttp://mvnrepository.com/で検索。
  • type
    • 依存ライブラリのパッケージングタイプを指定する。
    • デフォルトは「jar」。
    • 他に「ejb-client」や「test-jar」等がある。
  • scope
    • compile
      • デフォルトのスコープ。コンパイル、実行、テストのいずれでも必要になる依存アーティファクト。
    • provided
      • JDKまたは環境が実行時に提供してくれると考えてよい依存アーティファクト。
    • runtime
      • 実行時に必要な依存アーティファクトで、実行時にクラスパスで指定される。
    • test
      • テストをコンパイル、実行するために必要な依存アーティファクト。
      • warやearへビルドするプロジェクトで単体試験で使用するライブラリをlib配下に入れたくない場合に使用するとよい。
    • system
      • システムによって提供され常に参照可能だが、JARのパスを要素で指定しなければならない。
    • import
      • 子が親の要素経由でアーティファイクトをインポートする。
  • systemPath
    • scopeが「system」の場合のみに使用できる。
  • optional
    • このプロジェクトに依存しているプロジェクトでは、「optional=true」のdependencyを無視する。
  • exclusions
    • このライブラリが依存しているライブラリを除去したい場合に指定する。

設定例

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>2.5.6SEC01</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.apache.ibatis</groupId>
            <artifactId>ibatis-sqlmap</artifactId>
        </exclusion>
    </exclusions>
</dependency>

maven-dependency-pluginのゴールについて

  • 依存性を分析する(使用、非使用、宣言済み、未宣言)
    • $ mvn dependency:analyze
  • 重複する依存関係を判定する
    • $ mvn dependency:analyze-duplicate
  • すべての依存関係を解決する
    • $ mvn dependency:resolve
  • すべてのプラグインを解決する
    • $ mvn dependency:resolve-plugin
  • 依存関係ツリーを表示する

参考:http://maven.apache.org/plugins/maven-dependency-plugin/

parent要素

  • groupId、artifactId、version要素
    • 設定を継承したいPOM(親POM)のグループID、アーティファクトID、バージョンを指定する。
    • 親POMの「packaging」要素は「pom」となる。
  • relativePath
    • 親POMプロジェクトまでの相対パスを指定する。
    • (例)../my-parent

dependencyManagement

依存関係の定義のみ行う。
通常は親POMに本要素でを指定したライブラリを定義しておき、子POM(親POMの設定を継承するPOM)のdependencies要素にてのみ指定することにより、親POMを継承するプロジェクト横断でライブラリのバージョンや設定を設定することができる。

modules要素

  • module
    • 子POMのプロジェクト名を設定する。
    • 子POMにてparent要素を設定すればよいので通常はわざわざ設定する必要はない。
    • (例)my-project

properties

「key - value」形式でプロパティを定義することができる。
例えば、「123」と設定すると、POM内から「${hoge}」で参照することができ「123」が代入される。

Super POM

全てのPOMが暗黙的に継承しているPOM。
Maven 3.0.4の場合は下記がSuper POMとなる。

<project>
  <modelVersion>4.0.0</modelVersion>

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>