Maven构建简单的多模块项目
复制于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构建简单的多模块项目的更多相关文章
- Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创
原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...
- Maven 搭建spring boot多模块项目
Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...
- 三、使用Maven构建简单的java项目
前边,我刚搭建了Maven环境,还有给大家推荐了学习资源,这个小节,我们来就来,,简单的玩玩maven. 1.所需工具: 1.Eclipse 2.apache-maven-3.3.9 3. ...
- 转: maven进阶:一个多模块项目
一个多模块项目通过一个父POM 引用一个或多个子模块来定义.父项目,通过以下配置,将子项目关联. <packaging>pom</packaging> <modules& ...
- maven安装与创建多模块项目
最新版已同步至 http://yywang.info/2014/05/31/maven-install-and-create-project/ maven是一个比较流行的项目管理工具,在最近参与的项目 ...
- 基于maven使用IDEA创建多模块项目
原文地址:http://blog.csdn.net/williamhappy/article/details/54376855 鉴于最近学习一个分布式项目的开发,讲一下关于使用IntelliJ IDE ...
- maven进阶:一个多模块项目
一个多模块项目通过一个父POM 引用一个或多个子模块来定义.父项目,通过以下配置,将子项目关联. <packaging>pom</packaging> <modules& ...
- 第三章 Maven构建 Java Spring Boot Web项目
3.1 认识Srping Boot Spring Boot是一个框架,是一种全新的编程规范,它的产生简化了对框架的使用,简化了Spring众多的框架中大量的繁琐的配置文件,所以说Spring Bo ...
- IntelliJ IDEA maven 构建简单springmvc项目
环境: apache-tomcat-8.5.15 jdk1.8.0_172 IDEA 建立一个maven-webapp项目:Create New Project 后点击next 然后next 可以选择 ...
随机推荐
- 最实用的APP界面设计知识,有温度的APP设计(转)
在逛简书的时候,无意之间看到了这样的一篇非常有意思的app设计博文.顾25学堂的摘录了其中的一些关于移动端APP界面设计的精华.分享给25学堂的app设计师们. 当然,下面的这些app设计知识点是来自 ...
- Codeforces Testing Round #12 C. Subsequences 树状数组
C. Subsequences For the given sequence with n different elements find the number of increasing s ...
- 修改vs helpview手册路径
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Help\v2.1\Ca ...
- Hbase系统架构
HBase 系统架构 HBase是Apache Hadoop的数据库,能够对大型数据提供随机.实时的读写访问.HBase的目标是存储并处理大型的数据.HBase是一个开源的,分布式的,多版本的,面向列 ...
- JAVA Day2
标识符(类名:变量.属性.方法名: ) 组成:类名开头不能是数字,只能有字母数字_$组成. 命名规范: 类名每一个单词首字母大写(HelloWorld大驼峰法则) ...
- Linux定时,计划任务cron
假如你有一些任务要定期执行,比如清理磁盘.删除过期文件.发送邮件和提醒,这个时候可以用cron来实现. crond是后台进程,而crontab则是定制好的计划任务表. 启动与停止 查看状态/sbin/ ...
- SpringHttpInvoker解析3-客户端实现
主要的配置文件 <bean id="httpInvokerUserService" class="org.springframework.remoting.http ...
- Redis开启持久化
配置appendonly 打开redis.conf找到appendonly. 将 appendonly no 改为 appendonly yes 配置appendfsync appendfsync a ...
- javascript选取文档元素
用指定的id属性 用指定的name属性 用指定的标签名字 用指定的CSS类 匹配指定的CSS选择器 通过ID选取元素 var section1 = document.getElementById(&q ...
- HDU4888 Redraw Beautiful Drawings(最大流唯一性判定:残量网络删边判环)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4888 Description Alice and Bob are playing toget ...