May 12, 2007
[Maven] Maven2 で POM の inheritance & aggregation を整理してみる(1)
POM の inheritance & aggregation
普段何気なく使っている POM の inheritance や aggregation だけれど、 考えてみると極端に複雑なプロジェクトを構築したことが無いので常に両方を同時に使用することが多かった。 そこで、一度 inheritance と aggregation の違いとそれぞれの意味を整理してみた。
inheritance の実験
下記の様な構成のプロジェクトをでっち上げてみた。 \inheritance\pom.xml は packageing=pom で親 POM になれるようにしてある(下記参照)。 \inheritance\child\pom.xml は \inheritance\pom.xml を継承するように設定してある。
\inheritance ├pom.xml └child ├pom.xml └src └java └com └example └HelloWorld.java
\inheritance\pom.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project>
<modelVersion>4.0.0</modelVersion>
<name>pom inheritance sample</name>
<groupId>com.example</groupId>
<artifactId>pom-inheritance-sample</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>jp.in-vitro.largo</groupId>
<artifactId>largo-support-pom</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>dumpPom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<charset>Shift_JIS</charset>
</configuration>
</plugin>
</plugins>
</reporting>
<organization>
<name>example.com</name>
<url>http://www.example.com/</url>
</organization>
<developers>
<developer>
<id>me</id>
<name>me</name>
</developer>
</developers>
<repositories>
<repository>
<id>repository.example.com</id>
<name>example.com repository</name>
<url>http://www.example.com/maven2</url>
</repository>
</repositories>
</project>
\inheritance\child\pom.xml
今回は inheritance のみ使用することが目的なので、aggregation を表す modules タグは使用していない。 ただ、parent タグで親の POM を指定しているため、記述量が圧倒的に減少していることがすぐに分かる。 ちなみに、POM の内容は簡単な自作の Maven Plugin で出力している。
<?xml version="1.0" encoding="UTF-8" ?>
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>pom-inheritance-sample</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>pom-inheritance-sample-child</artifactId>
<name>pom inheritance sample - child</name>
<packaging>jar</packaging>
</project>
\inheritance で Maven2 による compile を実行した結果
aggregation を使用していないので、\inheritance\pom.xml が表しているプロジェクトのみがコンパイルされている。 \inheritance\child\pom.xml は全く無視されていることに注目。
inheritance>mvn compile
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------------
---
[INFO] Building pom inheritance sample
[INFO] task-segment: [compile]
[INFO] -------------------------------------------------------------------------
---
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to D:\inheritance\target\classes
[INFO] [largo-support-pom:dumpPom {execution: default}]
***** POM INFORMATION *************************************
id : com.example:pom-inheritance-sample:pom:1.0.0
groupId : com.example
atrifactId : pom-inheritance-sample
version : 1.0.0
name : pom inheritance sample
***********************************************************
Parent : null
File : D:\inheritance\pom.xml
Basedir : D:\inheritance
Repositories : [org.apache.maven.model.Repository@33459432[name=example.com repo
sitory,url=http://www.example.com/maven2,id=repository.example.com,layout=defaul
t,releases=null,snapshots=null],org.apache.maven.model.Repository@13948523[name=
Maven Repository Switchboard,url=http://repo1.maven.org/maven2,id=central,layout
=default,releases=null,snapshots=org.apache.maven.model.RepositoryPolicy@149105b
],]
BuildPlugins : [Plugin [jp.in-vitro.largo:largo-support-pom],Plugin [org.apache.
maven.plugins:maven-resources-plugin],Plugin [org.apache.maven.plugins:maven-com
piler-plugin],]
ReportPlugins : [org.apache.maven.model.ReportPlugin@196f4b5,]
Developers : [org.apache.maven.model.Developer@17427094[name=me,email=null,id=me
,organization=null,organizationUrl=null,roles=[]],]
Organization : org.apache.maven.model.Organization@14069849[name=example.com,url
=http://www.example.com/]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Sun May 13 16:40:33 JST 2007
[INFO] Final Memory: 4M/9M
[INFO] ------------------------------------------------------------------------
inheritance>
\inheritance\child で Maven2 による compile を実行した結果
\inheritance\child\pom.xml を使用してコンパイルを実行してみる。 こちらもこちらで自分自身のプロジェクトのみを処理対象としている。 但し、POM の内容は \inheritance\pom.xml の内容を継承しているため、 \interitance\child\pom.xml では設定をしていない項目もきちんと設定が受け継がれていることが分かる。 例えば、Developer や Organization といった項目を見ると良く分かる。
inheritance>cd child
inheritance\child>mvn compile
[INFO] Scanning for projects...
[INFO] -------------------------------------------------------------------------
---
[INFO] Building pom inheritance sample - child
[INFO] task-segment: [compile]
[INFO] -------------------------------------------------------------------------
---
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to D:\inheritance\child\target\classes
[INFO] [largo-support-pom:dumpPom {execution: default}]
***** POM INFORMATION *************************************
id : com.example:pom-inheritance-sample-child:jar:1.0.0
groupId : com.example
atrifactId : pom-inheritance-sample-child
version : 1.0.0
name : pom inheritance sample - child
***********************************************************
Parent : org.apache.maven.project.MavenProject@a6be9458
File : D:\inheritance\child\pom.xml
Basedir : D:\inheritance\child
Repositories : [org.apache.maven.model.Repository@13948523[name=example.com repo
sitory,url=http://www.example.com/maven2,id=repository.example.com,layout=defaul
t,releases=null,snapshots=null],org.apache.maven.model.Repository@21565531[name=
Maven Repository Switchboard,url=http://repo1.maven.org/maven2,id=central,layout
=default,releases=null,snapshots=org.apache.maven.model.RepositoryPolicy@1d0d45b
],]
BuildPlugins : [Plugin [jp.in-vitro.largo:largo-support-pom],Plugin [org.apache.
ReportPlugins : [org.apache.maven.model.ReportPlugin@19ba640,]
Developers : [org.apache.maven.model.Developer@539419[name=me,email=null,id=me,o
rganization=null,organizationUrl=null,roles=[]],]
Organization : org.apache.maven.model.Organization@13878877[name=example.com,url
=http://www.example.com/]
***** POM INFORMATION *************************************
id : com.example:pom-inheritance-sample:pom:1.0.0
groupId : com.example
atrifactId : pom-inheritance-sample
version : 1.0.0
name : pom inheritance sample
***********************************************************
Parent : null
File : D:\inheritance\pom.xml
Basedir : D:\inheritance
Repositories : [org.apache.maven.model.Repository@26117441[name=example.com repo
sitory,url=http://www.example.com/maven2,id=repository.example.com,layout=defaul
t,releases=null,snapshots=null],org.apache.maven.model.Repository@30311876[name=
Maven Repository Switchboard,url=http://repo1.maven.org/maven2,id=central,layout
=default,releases=null,snapshots=org.apache.maven.model.RepositoryPolicy@1b03c1a
],]
BuildPlugins : [Plugin [jp.in-vitro.largo:largo-support-pom],]
ReportPlugins : [org.apache.maven.model.ReportPlugin@1578aab,]
Developers : [org.apache.maven.model.Developer@19658898[name=me,email=null,id=me
,organization=null,organizationUrl=null,roles=[]],]
Organization : org.apache.maven.model.Organization@13594894[name=example.com,url
=http://www.example.com/]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Sun May 13 17:28:32 JST 2007
[INFO] Final Memory: 4M/8M
[INFO] ------------------------------------------------------------------------
inheritance\child>



