Maven 多模块项目
来源:原创 发布时间:2015-04-19 归档:maven
环境 :
JDK 7
Maven 3
Tomcat 7
Eclipse Luna
项目结构示意
手工构建项目的目录结构如下, 可参考 archetype:create, 这里略。
kitty | |— pom.xml (pom) | |— kitty-parent | | |— pom.xml (pom) | |— kitty-app | | | |— src | |— pom (war) | |— kitty-core | | | |— src | |— pom (jar) |导入项目到 eclipse
Import --> Maven --> Existing Maven Projects
kitty pom
<project> <modelVersion>4.0.0</modelVersion> <groupId>org.lychie</groupId> <artifactId>kitty</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <name>kitty project</name> <url>http://maven.apache.org</url> <modules> <module>kitty-parent</module> </modules> </project>
这里配置的项不需要很多, 作为父模块, packaging 类型必须为 pom。另外配置了一个子模块 kitty-parent, 见下面的配置。
kitty-parent pom
<project> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.lychie</groupId> <artifactId>kitty</artifactId> <version>1.0.0</version> </parent> <artifactId>kitty-parent</artifactId> <packaging>pom</packaging> <name>kitty-parent project</name> <url>http://maven.apache.org</url> <modules> <module>kitty-app</module> <module>kitty-core</module> </modules> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </project>
通过 <parent> 声明其父模块, maven 默认会认为父模块是在当前项目 pom.xml 所在的目录的上一级目录中, kitty 的项目结构设计符合这个规则。如果你的项目结构并不是这样, 你必须通过 <relativePath> 来指定你的父模块 pom.xml 所在的路径, relativePath 的值默认是 ../。仔细观察发现, 这个 pom 并没有 groupId, 其实不是没有, 而是从父模块中继承了过来, 子模块会继承父模块 groupId、version、description、organization、url、developers、contributors、distributionManagement、properties、dependencies、dependencyManagement、build 等的配置。另外, 这里还配置了 2 个子模块 kitty-app、kitty-core, 见下面的配置。
kitty-core pom
<project> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.lychie</groupId> <artifactId>kitty-parent</artifactId> <version>1.0.0</version> </parent> <artifactId>kitty-core</artifactId> <packaging>jar</packaging> <name>kitty-core project</name> <url>http://maven.apache.org</url> </project>
这里的配置没有什么特别的。
kitty-app pom
<project> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.lychie</groupId> <artifactId>kitty-parent</artifactId> <version>1.0.0</version> </parent> <artifactId>kitty-app</artifactId> <packaging>war</packaging> <name>kitty-app project</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.lychie</groupId> <artifactId>kitty-core</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>kitty-app</finalName> </build> </project>
这里的第13至第17行引入了 kitty-core 项目的依赖, 除此之外, 没有什么特别的。
kitty-core
package org.lychie.service; public class SimpleService { public String getInfomation(String name) { return "Hello " + name; } }kitty-app
package org.lychie.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.lychie.service.SimpleService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SimpleServlet extends HttpServlet { private static final long serialVersionUID = 1L; private SimpleService service = new SimpleService(); @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String info = service.getInfomation("Kitty"); request.setAttribute("info", info); request.getRequestDispatcher("/home.jsp").forward(request, response); return ; } }
以上是一个简单的示例, kitty 的核心代码部分在 kitty-core 项目中编写, 由于 kitty-app 项目配置了 kitty-core 项目的依赖, 因此在 kitty-app 项目中可以直接使用 kitty-core 项目中编写好的业务核心代码。
打包项目
除了打包之外, 你还可以执行编译、测试等动作。选中 kitty 项目, 右键 --> Run As --> Maven Build..., clean package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] kitty project
[INFO] kitty-parent project
[INFO] kitty-core project
[INFO] kitty-app project
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building kitty project 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building kitty-parent project 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building kitty-core project 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.lychie.service.SimpleServiceTest
---> Hello Kitty
---> Hello World
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.1 sec
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building kitty-app project 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] kitty project ...................................... SUCCESS [ 0.506 s]
[INFO] kitty-parent project ............................... SUCCESS [ 0.004 s]
[INFO] kitty-core project ................................. SUCCESS [ 6.635 s]
[INFO] kitty-app project .................................. SUCCESS [ 0.985 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] kitty project
[INFO] kitty-parent project
[INFO] kitty-core project
[INFO] kitty-app project
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building kitty project 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building kitty-parent project 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building kitty-core project 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.lychie.service.SimpleServiceTest
---> Hello Kitty
---> Hello World
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.1 sec
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building kitty-app project 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] kitty project ...................................... SUCCESS [ 0.506 s]
[INFO] kitty-parent project ............................... SUCCESS [ 0.004 s]
[INFO] kitty-core project ................................. SUCCESS [ 6.635 s]
[INFO] kitty-app project .................................. SUCCESS [ 0.985 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
可以看到, maven 在 build kitty 的时候, 会根据模块之间的依赖关系, 整理出一个 build 的顺序, 然后再按顺序来 build。
示例代码下载