复制于http://www.cnblogs.com/luxh/p/3506750.html

做个记录

一般web项目会进行分模块开发。这里简单分为domain(领域层)、persist(持久层)、service(业务层)、web(交互控制层)。

  用Maven构建以上各层,结构如下:

  

1、创建simple-parent,用来给各个子模块继承。

  1)进入命令行,输入以下命令:

mvn archetype:generate -DgroupId=cn.luxh -DartifactId=simple-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  可以看到在当前目录生成了simple-parent目录,里面有一个src目录和一个pom.xml文件。

  将src文件夹删除。

  2)修改pom.xml文件

将<packaging>jar</packaging>修改为<packaging>pom</packaging>

  pom表示它是一个被继承的模块,修改后的内容如下:

<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-parent</name>
<url>http://maven.apache.org</url> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies> </project>

2、创建simple-domain模块

  1)在命令行进入创建好的simple-parent目录,然后进入下列命令:

mvn archetype:generate -DgroupId=cn.luxh -DartifactId=simple-domain -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  可以看到在simple-parent目录中生成了simple-domain,里面包含src目录和pom.xml文件。

  同时,在simple-parent目录中的pom文件自动添加了如下内容:

 <modules>
<module>simple-domain</module>
</modules>

  这时,simple-parent的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-parent</name>
<url>http://maven.apache.org</url> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies> <modules>
<module>simple-domain</module>
</modules>
</project>

  2)修改simple-domain目录中的pom.xml文件

把<groupId>cn.luxh</groupId>和<version>1.0-SNAPSHOT</version>去掉,加上<packaging>jar</packaging>

  因为groupId和version会继承simple-parent中的groupId和version,packaging设置打包方式为jar

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>simple-domain</artifactId>
<packaging>jar</packaging>
<name>simple-domain</name>
<url>http://maven.apache.org</url> </project>

3、创建simple-persist模块

  1)在命令行进入创建好的simple-parent目录,然后进入下列命令:

mvn archetype:generate -DgroupId=cn.luxh -DartifactId=simple-persist -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  可以看到在simple-parent目录中生成了simple-persist,里面包含src目录和pom.xml文件。

  同时,在simple-parent目录中的pom文件自动变成如下内容:

<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-parent</name>
<url>http://maven.apache.org</url> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies> <modules>
<module>simple-domain</module>
<module>simple-persist</module>
</modules>
</project>

  2)修改simple-persist目录中的pom.xml文件

  添加对simple-domain模块的依赖,修改后的内容如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>simple-persist</artifactId>
<packaging>jar</packaging>
<name>simple-persist</name>
<url>http://maven.apache.org</url> <dependencies>
<dependency>
<groupId>cn.luxh</groupId>
<artifactId>simple-domain</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

4、创建simple-service模块

  1)在命令行进入创建好的simple-parent目录,然后进入下列命令:

mvn archetype:generate -DgroupId=cn.luxh -DartifactId=simple-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  可以看到在simple-parent目录中生成了simple-service,里面包含src目录和pom.xml文件。

  同时,在simple-parent目录中的pom文件自动变成如下内容:

<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-parent</name>
<url>http://maven.apache.org</url> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies> <modules>
<module>simple-domain</module>
<module>simple-persist</module>
<module>simple-service</module>
</modules>
</project>

  2)修改simple-service目录中的pom.xml文件

  simple-service依赖simple-persist和simple-domain,但是我们只需添加simple-persist的依赖即可,引文simple-persist已经依赖了simple-domain。

  修改后的内容如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>simple-service</artifactId>
 <packaging>jar</packaging>
<name>simple-service</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>cn.luxh</groupId>
<artifactId>simple-persist</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

5、创建simple-web模块

  1)在命令行进入创建好的simple-parent目录,然后进入下列命令:

mvn archetype:generate -DgroupId=cn.luxh -DartifactId=simple-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

  可以看到在simple-parent目录中生成了simple-web,里面包含src目录和pom.xml文件,在\simple-web\src\main\webapp目录中还生成了一个简单的index.jsp,将里面的内容改为

  Hey,Maven!

  simple-web\src\main\webapp\WEB-INF目录中生成了web.xml

  同时,在simple-parent目录中的pom文件自动变成如下内容:

<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-parent</name>
<url>http://maven.apache.org</url> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies> <modules>
<module>simple-domain</module>
<module>simple-persist</module>
<module>simple-service</module>
<module>simple-web</module>
</modules>
</project>

  2)修改simple-web目录中的pom.xml文件

     注意,web项目的打包方式是war,添加对simple-service的依赖

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>simple-web</artifactId>
<packaging>war</packaging>
<name>simple-web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>cn.luxh</groupId>
<artifactId>simple-service</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>simple-web</finalName>
</build>
</project>

6、至此创建好了这几个模块,怎么运行起来呢。

  1)由于最终运行的是simple-web模块,所以我们对该模块添加jetty容易支持,方便测试运行。修改simple-web项目的pom.xml如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>simple-web</artifactId>
<packaging>war</packaging>
<name>simple-web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>cn.luxh</groupId>
<artifactId>simple-service</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>simple-web</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

  2)命令行进入simple-parent目录,执行如下命令

mvn clean install

   执行完后,在simple-web目录下多出了target目录,里面有了simple-web.war

  3)命令行进入simple-web目录,执行如下命令,启动jetty

mvn jetty:run

  启动jetty服务器后,访问http://localhost:8080/simple-web/

7、加入Servlet的依赖支持

  修改simple-web目录的pom.xml,内容如下:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.luxh</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>simple-web</artifactId>
<packaging>war</packaging>
<name>simple-web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>cn.luxh</groupId>
<artifactId>simple-service</artifactId>
<version>${project.version}</version>
</dependency> <dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_2.4_spec</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency> </dependencies>
<build>
<finalName>simple-web</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

  

  最后,从eclipse中导入存在的Maven项目,选中simple-parent导入,即可在eclipse中进行开发了。

Maven构建简单的多模块项目的更多相关文章

  1. Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创

    原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...

  2. Maven 搭建spring boot多模块项目

    Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...

  3. 三、使用Maven构建简单的java项目

    前边,我刚搭建了Maven环境,还有给大家推荐了学习资源,这个小节,我们来就来,,简单的玩玩maven. 1.所需工具: 1.Eclipse     2.apache-maven-3.3.9   3. ...

  4. 转: maven进阶:一个多模块项目

    一个多模块项目通过一个父POM 引用一个或多个子模块来定义.父项目,通过以下配置,将子项目关联. <packaging>pom</packaging> <modules& ...

  5. maven安装与创建多模块项目

    最新版已同步至 http://yywang.info/2014/05/31/maven-install-and-create-project/ maven是一个比较流行的项目管理工具,在最近参与的项目 ...

  6. 基于maven使用IDEA创建多模块项目

    原文地址:http://blog.csdn.net/williamhappy/article/details/54376855 鉴于最近学习一个分布式项目的开发,讲一下关于使用IntelliJ IDE ...

  7. maven进阶:一个多模块项目

    一个多模块项目通过一个父POM 引用一个或多个子模块来定义.父项目,通过以下配置,将子项目关联. <packaging>pom</packaging> <modules& ...

  8. 第三章 Maven构建 Java Spring Boot Web项目

    3.1   认识Srping Boot Spring Boot是一个框架,是一种全新的编程规范,它的产生简化了对框架的使用,简化了Spring众多的框架中大量的繁琐的配置文件,所以说Spring Bo ...

  9. IntelliJ IDEA maven 构建简单springmvc项目

    环境: apache-tomcat-8.5.15 jdk1.8.0_172 IDEA 建立一个maven-webapp项目:Create New Project 后点击next 然后next 可以选择 ...

随机推荐

  1. ZLL本地局域网通信过程

    Interface_srpcserver -----以灯的状态操作位例 网关与客户端通过Socket API通信,Socket API在socket_server.c中实现,socket_server ...

  2. Computer Graphics Research Software

    Computer Graphics Research Software Helping you avoid re-inventing the wheel since 2009! Last update ...

  3. BurpSuite拦截HTTPS请求

    1.设置好浏览器代理 2. 3.请求https站点(比如https://www.baidu.com),以火狐浏览器例子: 4. 这一步主要是为了显示[我已充分了解可能的风险],如果有,就不用做以上步骤 ...

  4. Linux IO模型和网络编程模型

    术语概念描述: IO有内存IO.网络IO和磁盘IO三种,通常我们说的IO指的是后两者. 阻塞和非阻塞,是函数/方法的实现方式,即在数据就绪之前是立刻返回还是等待. 以文件IO为例,一个IO读过程是文件 ...

  5. hdu1059 多重背包(转换为01背包二进制优化)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1059 之前写过一个多重背包二进制优化的博客,不懂请参考:http://www.cnblog ...

  6. HTML5优点

    1.标签的改变:<header>, <footer>, <dialog>, <aside>, <figure>, <section&g ...

  7. Python基础11- 函数之自定义函数

    自定义函数语法结构:def fun1([x],[y],....): 语句1 语句2 使用def语句来定义函数,在def后依次写出函数名.小括号.参数(可无).冒号,然后缩进写函数体 1.无参函数:de ...

  8. Windows下安装Docker

    放在三年前,你不认识Docker情有可原,但如果现在你还这么说,不好意思,只能说明你OUT了,行动起来吧骚年,很可能你们公司或者你即将要去的公司,或者你想去的公司很可能就会引入Docker,或者已经引 ...

  9. 用gulp替代fekit构建前端项目

    https://segmentfault.com/a/1190000003060016 离开qunar有一个多月了,在离开的时候就决定不再用fekit.做出这个决定并不是因为fekit不好,恰恰相反, ...

  10. Android AIDL 进行进程间通讯(IPC)

    编写AIDL文件时,需要注意: 1.接口名和aidl文件名相同. 2.接口和方法前不用加访问权限修饰符 (public.private.protected等,也不能用final.static). 3. ...