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 可以选择 ...
随机推荐
- 对于一个负数mod正数
鸟神说.. a/b靠零取整 然后呢..a%b定义成a-(a/b)*b c语言就是这么算的... 那么python2.6是怎么算的呢 如果最后你取模想得到一个正数.. 那么在上述取模定义不变的情况下 p ...
- 【spring bean】 spring中bean之间的引用以及内部bean
在spring中会有如下的几种情况: 1.在当前容器中,(即在spring.xml这一个配置文件中),一个bean引用了另一个bean. 使用 1> <ref bean="另 ...
- C# 静态方法和非静态方法
转载:http://www.cnblogs.com/mikelij/archive/2010/08/13/1798578.html 本文将围绕c#静态方法和实例方法讨论一下.针对一些观点,如:&quo ...
- 关于最近Google无法正常访问的变通之法(已经被墙)
对于习惯使用谷歌搜素的骚年最近可能比较蛋疼,没这方面需求的就另当别论.可能是政府限制加强的原因,有时访问谷歌在香港服务器也不大好使,不过最近发现,不通过谷歌域名而直接使用谷歌的ip来访问谷歌响应速度可 ...
- Python小例子(求和)
简单的数字的求和: a = input('请输入第一个数:') b = input('请输入第二个数:') sum = float(a) + float(b) print('数字{0}和数字{1}相加 ...
- Android App 性能优化实践
本文记录了Android App优化需要用到的工具和以及在实践中的Tips.也算对我这半年来部分工作的总结. 工具 Hierarchy Viewer 是 Android SDK 自带的 Layout ...
- HDU4807 Lunch Time(费用流变种)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4807 Description The campus of Nanjing Universit ...
- Apache ActiveMQの版本更迭和Apache ActiveMQの故障转移
本文描述apache activemq 版本更迭的原因以及Apache ActiveMQのThe Failover Transport new features in 5.2.0 1.对信息的传输/ ...
- ural 1143. Electric Path
1143. Electric Path Time limit: 1.0 secondMemory limit: 64 MB Background At the team competition of ...
- An Unfair Game-[ACdream1035]
Problem Description There are n people and n target, everyone should get one target, no two people g ...