eclipseに対応したmavenマルチモジュール プロジェクトの作成

maven3でマルチモジュールプロジェクトを作成する場合、デフォルトのディレクトリ構成では、親プロジェクト ディレクトリの下に子プロジェクト ディレクトリを格納する形になる。
例: 親プロジェクトが guide-ide-eclipse、子プロジェクトがguide-ide-eclipse-site、guide-ide-eclipse-core、guide-ide-eclipse-module1

問題点

親プロジェクトと子プロジェクトには依存関係がある。そのため、親プロジェクトディレクトリからのディレクトリ構成が必要になる。
eclipse上のsubversionから親プロジェクトをチェックアウトした場合、子プロジェクトはjavaプロジェクトとして認識されない。
一旦、親プロジェクトをeclipseから削除(物理削除はしない)した後で、子プロジェクトを個別にインポートする必要がある。
これは、面倒ということで親プロジェクト ディレクトリと子プロジェクト ディレクトリを同じ階層に持つ構造に変更する。
例:

eclipseに対応したmavenマルチモジュール プロジェクトの作成

親プロジェクトの作成
$ mvn archetype:create -DgroupId=guide.ide.eclipse -DartifactId=guide-ide-eclipse
$ cd guide-ide-eclipse/
親プロジェクトの設定
  • 不要なsrcディレクトリを削除
  • project/packaging 要素を 「pom」に変更。
  • project/modules 要素を追加。
  • project/dependencies 要素を削除。
$ rm -rf src
$ vi pom.xml

guide-ide-eclipse/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>guide.ide.eclipse</groupId>
  <artifactId>guide-ide-eclipse</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>guide-ide-eclipse</name>
  <url>http://maven.apache.org</url>

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

  <modules>
  </modules>
</project>
子プロジェクトの作成
$ mvn archetype:create -DgroupId=guide.ide.eclipse -DartifactId=guide-ide-eclipse-site
$ mvn archetype:create -DgroupId=guide.ide.eclipse.core -DartifactId=guide-ide-eclipse-core
$ mvn archetype:create -DgroupId=guide.ide.eclipse.module1 -DartifactId=guide-ide-eclipse-module1

※子プロジェクトの作成に成功すると、親プロジェクトのpom.xmlのproject/modules 要素に子プロジェクト名
が追記される。

guide-ide-eclipse/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  ....

  <modules>
    <module>guide-ide-eclipse-site</module>
    <module>guide-ide-eclipse-core</module>
    <module>guide-ide-eclipse-module1</module>
  </modules>
</project>
子プロジェクト間の参照関係の設定

guide-ide-eclipse-module1にguide-ide-eclipse-coreの設定を追加する。

$ cd guide-ide-eclipse-module1
$ vi pom.xml

guide-ide-eclipse-module1/pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>guide-ide-eclipse</artifactId>
    <groupId>guide.ide.eclipse</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>guide.ide.eclipse.module1</groupId>
  <artifactId>guide-ide-eclipse-module1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>guide-ide-eclipse-module1</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>guide.ide.eclipse.core</groupId>
      <artifactId>guide-ide-eclipse-core</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
動作確認

ここまでが、通常のmavenマルチモジュール プロジェクトの作成手順になる。
正常に作成できたか確認するため、プロジェクトのインストールとeclipse関連のファイルの生成を実行する。

$ cd ..
$ mvn install
$ mvn eclipse:eclipse
親プロジェクト ディレクトリと子プロジェクト ディレクトリを同じ階層に持つディレクトリ構成に変更

mavenディレクトリ構成をeclipseで使いやすいディレクトリ構成に変更する。

変更前のディレクトリ構成

変更後のディレクトリ構成

前の動作確認で作成したインストールとeclipse関連のファイルは削除しておく(必要ないかも)。

$ mvn clean
$ mvn eclipse:clean

guide-ide-eclipseディレクトリを作成し、親プロジェクトのpom.xmlを移動する。

$ mkdir guide-ide-eclipse
$ mv pom.xml guide-ide-eclipse/

project/modules 要素の参照設定を変更する。

$ cd guide-ide-eclipse
$ vi pom.xml

guide-ide-eclipse/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  ....

  <modules>
    <module>../guide-ide-eclipse-site</module>
    <module>../guide-ide-eclipse-core</module>
    <module>../guide-ide-eclipse-module1</module>
  </modules>
</project>

以上で、設定手順は完了になる。

もし、relativePathが何とか、のエラーが出た場合、子プロジェクトのproject/relativePath要素に親プロジェクト ディレクトリの参照を追加する。
guide-ide-eclipse-module1/pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>guide-ide-eclipse</artifactId>
    <groupId>guide.ide.eclipse</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../guide-ide-eclipse</relativePath>
  </parent>
  
  ....
</project>

参考資料

下記のURLの内容を抜粋したので、原文読んだ方が分かりやすいかも。