原文:http://www.blogjava.net/fancydeepin/archive/2015/06/27/maven-modules.html
项目结构
proj
|
|— proj-model
| |
| |— src
| |— pom (jar)
| |
|
|— proj-dao
| |
| |— src
| |— pom (jar)
| |
|
|— proj-service
| |
| |— src
| |— pom (jar)
| |
|
|— proj-web
| |
| |— src
| |— pom (war)
| |
|
|— pom.xml (pom)
|
这里的 proj 项目分了 4 个模块,它们分别是:proj-model(数据模型层)、proj-dao(数据库访问层)、proj-service(业务逻辑层)、
proj-web(页面表现层)。下面我们来依次的来创建这几个项目(确保你的 maven 环境已经配置好,以下是 window 环境的 cmd 终端操作) 。
创建模块项目
01.创建 proj 项目mvn archetype:create -DgroupId=org.fanlychie -DartifactId=proj
mvn archetype:create -DgroupId=org.fanlychie -DartifactId=proj-model
mvn archetype:create -DgroupId=org.fanlychie -DartifactId=proj-dao
mvn archetype:create -DgroupId=org.fanlychie -DartifactId=proj-service
mvn archetype:create -DgroupId=org.fanlychie -DartifactId=proj-web -DarchetypeArtifactId=maven-archetype-webapp
for /r /d %i in (proj-*) do move %i proj
rd /S /Q projsrc
mkdir projproj-websrcmainjava
mkdir projproj-websrc estjava
for /r /d %i in (projproj-*) do rd /S /Q %isrcmainjavaorg
for /r /d %i in (projproj-*) do rd /S /Q %isrc estjavaorg
父模块 proj/pom.xml 配置
作为父模块,packaging 类型必须是 pom<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
声明子模块 <artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>proj-model</module>
<module>proj-dao</module>
<module>proj-service</module>
<module>proj-web</module>
</modules>
在父模块中可以通过 dependencyManagement 对依赖的包进行统一的管理 <module>proj-model</module>
<module>proj-dao</module>
<module>proj-service</module>
<module>proj-web</module>
</modules>
<properties>
<spring.version>4.1.5.RELEASE</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-dao</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
配置完成之后,父模块以及父模块的子模块不会引入任何的构件依赖,这只是为了让子模块能够继承得到这些配置,这样做的好处是,在子模块中只<spring.version>4.1.5.RELEASE</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-dao</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
需要声明 groupId 和 artifactId 不需要声明 version 就能准确的获取得到依赖的构件。这样一来,不仅能够使子模块的依赖配置变得简单,
还能使项目范围内所有依赖的构件的版本能够得到统一,它们可以在父模块中统一的来管理,这样可以避免模块之间由于版本不一致而引发的异常。
你还可以在父模块中通过 pluginManagement 来对依赖的插件进行统一的管理
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.8.v20150217</version>
</plugin>
</plugins>
</pluginManagement>
</build>
proj/pom.xml 完整的配置:<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.8.v20150217</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<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.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>proj</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.1.5.RELEASE</spring.version>
</properties>
<modules>
<module>proj-model</module>
<module>proj-dao</module>
<module>proj-service</module>
<module>proj-web</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-dao</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.8.v20150217</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
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.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>proj</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.1.5.RELEASE</spring.version>
</properties>
<modules>
<module>proj-model</module>
<module>proj-dao</module>
<module>proj-service</module>
<module>proj-web</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-dao</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.8.v20150217</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
子模块 proj-model/pom.xml 配置
作为子模块,需要在 pom.xml 中声明其父模块<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
GAV(groupId、artifactId、version)参考父模块。<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
maven 默认认为父模块是在当前项目 pom.xml 所在的目录的上一级目录中,这里的 proj 项目结构设计符合这个规则。如果你的项目结构并不是
这样,你必须通过 <relativepath> 节点来指定你的父模块 pom.xml 所在的路径(默认是 <relativepath>../</relativepath>)。
<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
proj-model/pom.xml 完整配置: <groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<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>
<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>proj-model</artifactId>
<packaging>jar</packaging>
<name>proj-model project</name>
<url>http://maven.apache.org</url>
</project>
groupId 和 version 会从父模块中继承,在子模块中不需要再配置。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>
<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>proj-model</artifactId>
<packaging>jar</packaging>
<name>proj-model project</name>
<url>http://maven.apache.org</url>
</project>
子模块 proj-dao/pom.xml 配置
为该模块声明 proj-model 的依赖,因为该模块需要使用 proj-model 模块中的数据模型<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
</dependencies>
proj-dao/pom.xml 完整配置: <dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
</dependencies>
<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>
<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>proj-dao</artifactId>
<packaging>jar</packaging>
<name>proj-dao project</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
</dependencies>
</project>
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>
<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>proj-dao</artifactId>
<packaging>jar</packaging>
<name>proj-dao project</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
</dependencies>
</project>
子模块 proj-service/pom.xml 配置
为该模块声明 proj-model、proj-dao 模块的依赖,因为该模块需要使用 proj-model 中的数据模型和 proj-dao 中的数据库访问接口<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-dao</artifactId>
</dependency>
</dependencies>
proj-service/pom.xml 完整的配置: <dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-dao</artifactId>
</dependency>
</dependencies>
<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>
<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>proj-service</artifactId>
<packaging>jar</packaging>
<name>proj-service project</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-dao</artifactId>
</dependency>
</dependencies>
</project>
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>
<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>proj-service</artifactId>
<packaging>jar</packaging>
<name>proj-service project</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-dao</artifactId>
</dependency>
</dependencies>
</project>
子模块 proj-web/pom.xml 配置
为该模块声明 proj-model、proj-service 的依赖,因为 proj-web 需要使用 proj-model 中的数据模型和 proj-service 中的业务逻辑<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-service</artifactId>
</dependency>
</dependencies>
proj-web/pom.xml 完整的配置: <dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-service</artifactId>
</dependency>
</dependencies>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>proj-web</artifactId>
<packaging>war</packaging>
<name>proj-web project</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-service</artifactId>
</dependency>
</dependencies>
<build>
<finalName>proj-web</finalName>
</build>
</project>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>proj-web</artifactId>
<packaging>war</packaging>
<name>proj-web project</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-model</artifactId>
</dependency>
<dependency>
<groupId>org.fanlychie</groupId>
<artifactId>proj-service</artifactId>
</dependency>
</dependencies>
<build>
<finalName>proj-web</finalName>
</build>
</project>
导入项目到 eclipse
配置完成之后,接下来可以将项目导入到 eclipse,在 eclipse 中右键 --> Import --> Maven --> Existing Maven Projects,将 proj项目所在的路径复制粘贴到 Root Directory 栏并敲回车,点击 Finish 按钮即可。
导入后的项目结构
示例源码下载:proj.zip
示例使用 jetty 作为 web 服务器,选中 proj-web --> 右键 --> Run As --> Maven build... --> Goals 栏输入 jetty:run
记得把 Resolve Workspace artifacts 选项勾选上,否则启动时会报错。访问方式:http://localhost/info
打包
选中父模块项目 proj --> 右键 --> Run As --> Maven build... --> Goals 栏输入 clean package 并执行。[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] proj
[INFO] proj-model project
[INFO] proj-dao project
[INFO] proj-service project
[INFO] proj-web project
[INFO]
[INFO] ------------------------------------------------------------------------
. . . . . .
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] proj ............................................... SUCCESS [ 0.002 s]
[INFO] proj-model project ................................. SUCCESS [ 1.100 s]
[INFO] proj-dao project ................................... SUCCESS [ 0.102 s]
[INFO] proj-service project ............................... SUCCESS [ 0.069 s]
[INFO] proj-web project ................................... SUCCESS [ 0.552 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
可以看到,maven 在 build 的时候,会根据模块之间的依赖关系,整理出一个 build 的顺序,然后再按顺序来 build。
执行完 package 后,各个子模块的 target 目录下会输出相应的 JAR 或 WAR 包。
执行完 package 后,各个子模块的 target 目录下会输出相应的 JAR 或 WAR 包。