Mavenでプロジェクトを作成する際に,複数のサブプロジェクトを包含する親プロジェクトを作成したい場合のチュートリアルです.
目次
プロジェクト構成
以下のような構成のプロジェクトを作成します.あくまで概念を理解するためのものなので,アプリケーションの実装は行いません.
mvn-parent-app
├── mvn-sub-app
└── mvn-sub2-app
親プロジェクトのフォルダを作成する
mkdir mvn-parent-app
% cd mvn-parent-app
sub project1を作成する
% mvn archetype:generate
archetype: maven-archetype-quickstart
version: 最新
groupId: org.example
artifactId: mvn-sub-app
version: 1.0-SNAPSHOT
package: org.example.sub
sub project2を作成する
% mvn archetype:generate
mvn archetype:generate
archetype: maven-archetype-quickstart
version: 最新
groupId: org.example
artifactId: mvn-sub2-app
version: 1.0-SNAPSHOT
package: org.example.sub2
フォルダ構成を確認する
% tree
.
├── mvn-sub-app
│ ├── pom.xml
│ └── src
│ ├── main
│ │ └── java
│ │ └── org
│ │ └── example
│ │ └── sub
│ │ └── App.java
│ └── test
│ └── java
│ └── org
│ └── example
│ └── sub
│ └── AppTest.java
├── mvn-sub2-app
│ ├── pom.xml
│ └── src
│ ├── main
│ │ └── java
│ │ └── org
│ │ └── example
│ │ └── sub2
│ │ └── App.java
│ └── test
│ └── java
│ └── org
│ └── example
│ └── sub2
│ └── AppTest.java
└── pom.xml
paernt用pom.xmlを作成する
packagingにpomと書く.これは親プロジェクトはjarを持たず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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mvn-parent-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>mvn-parent-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<modules>
<module>mvn-sub-app</module>
<module>mvn-sub2-app</module>
</modules>
</project>
sub用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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mvn-sub-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>mvn-sub-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<parent>
<groupId>org.example</groupId>
<artifactId>mvn-parent-app</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>
sub2用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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mvn-sub2-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>mvn-sub2-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<parent>
<groupId>org.example</groupId>
<artifactId>mvn-parent-app</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>
mvn:packageを実行する
parent直下で以下を実行.サブプロジェクトにはdependencyでjunitは書かれていないが,sub, sub2ともjunitが実行されている.サブプロジェクトのpom.xmlは親のpom.xmlに書かれている内容を継承する.
% mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] mvn-parent-app [pom]
[INFO] mvn-sub-app [jar]
[INFO] mvn-sub2-app [jar]
[INFO]
[INFO] ---------------------< org.example:mvn-parent-app >---------------------
[INFO] Building mvn-parent-app 1.0-SNAPSHOT [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] ----------------------< org.example:mvn-sub-app >-----------------------
[INFO] Building mvn-sub-app 1.0-SNAPSHOT [2/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvn-sub-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub-app/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvn-sub-app ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mvn-sub-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub-app/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mvn-sub-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub-app/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mvn-sub-app ---
[INFO] Surefire report directory: /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub-app/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.example.sub.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.013 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mvn-sub-app ---
[INFO] Building jar: /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub-app/target/mvn-sub-app-1.0-SNAPSHOT.jar
[INFO]
[INFO] ----------------------< org.example:mvn-sub2-app >----------------------
[INFO] Building mvn-sub2-app 1.0-SNAPSHOT [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvn-sub2-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub2-app/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvn-sub2-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub2-app/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mvn-sub2-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub2-app/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mvn-sub2-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub2-app/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mvn-sub2-app ---
[INFO] Surefire report directory: /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub2-app/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.example.sub2.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.013 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mvn-sub2-app ---
[INFO] Building jar: /Users/[user]/Development/maven-practice/mvn-parent-app/mvn-sub2-app/target/mvn-sub2-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for mvn-parent-app 1.0-SNAPSHOT:
[INFO]
[INFO] mvn-parent-app ..................................... SUCCESS [ 0.001 s]
[INFO] mvn-sub-app ........................................ SUCCESS [ 0.752 s]
[INFO] mvn-sub2-app ....................................... SUCCESS [ 0.126 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.924 s
[INFO] Finished at: 2022-12-30T23:00:47+09:00
[INFO] ------------------------------------------------------------------------