项目结构
proj
|
|— proj-model
| |
| |— src
| |— pom (jar)
| |
|
|— proj-dao
| |
| |— src
| |— pom (jar)
| |
|
|— proj-service
| |
| |— src
| |— pom (jar)
| |
|
|— proj-web
| |
| |— src
| |— pom (war)
| |
|
|— pom.xml (pom)
|
创建模块项目
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>
<modules> <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>
<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 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>
子模块 proj-model/pom.xml 配置
作为子模块,需要在 pom.xml 中声明其父模块<parent>
<groupId>org.fanlychie</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<parent>
<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>
子模块 proj-dao/pom.xml 配置
为该模块声明 proj-model 的依赖,因为该模块需要使用 proj-model 模块中的数据模型<dependencies> <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>
子模块 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>
<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>
子模块 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>
<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>
导入项目到 eclipse
配置完成之后,接下来可以将项目导入到 eclipse,在 eclipse 中右键 --> Import --> Maven --> Existing Maven Projects,将 proj 项目所在的路径复制粘贴到 Root Directory 栏并敲回车,点击 Finish 按钮即可。


打包
选中父模块项目 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 包。