使用Maven构建多模块项目.docx

上传人:罗晋 文档编号:12076135 上传时间:2021-12-01 格式:DOCX 页数:10 大小:22.87KB
返回 下载 相关 举报
使用Maven构建多模块项目.docx_第1页
第1页 / 共10页
使用Maven构建多模块项目.docx_第2页
第2页 / 共10页
使用Maven构建多模块项目.docx_第3页
第3页 / 共10页
使用Maven构建多模块项目.docx_第4页
第4页 / 共10页
使用Maven构建多模块项目.docx_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《使用Maven构建多模块项目.docx》由会员分享,可在线阅读,更多相关《使用Maven构建多模块项目.docx(10页珍藏版)》请在三一文库上搜索。

1、使用 Maven 构建多模块项目在平时的 Javaweb 项目开发中为了便于后期的维护, 我们一般会进行分层开发, 最常见的就是分为domain (域模型层)、dao (数据库访问层)、service (业务逻辑层)、web (表现 层) ,这样分层之后,各个层之间的职责会比较明确,后期维护起来也相对比较容易,今天我们就是使用 Maven 来构建以上的各个层。项目结构如下:system-parent|pom.xml|system-domain|pom.xml|system-dao|pom.xml|system-service|pom.xml|system-web|pom.xml一、创建 sys

2、tem-parent 项目创建 system-parent ,用来给各个子模块继承。进入命令行,输入以下命令:mvnarchetype:create-DgroupId=me.gacl-DartifactId=system-parent-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false命令执行完成之后可以看到在当前目录 (C:Documents and SettingsAdministrator)生成了 system-parent 目录,里面有一个src 目录和一个pom.xml 文件。将 src 文件

3、夹删除,然后修改pom.xml 文件,将 <packaging>jar</packaging> 修改为<packaging>pom</packaging> , pom 表示它是一个被继承的模块,修改后的内容如下:复制代码1<projectxmlns="http:/maven.apache.org/POM/ 4.0.0"xmlns:xsi="http:/www.w3.org/ 2001/XMLSchema-instance"2xsi:schemaLocation="http:/maven.ap

4、ache.org/POM/ 4.0.0http:/maven.apache.org/xsd/maven-4.0.0.xsd">3 <modelVersion>4.0.0</modelVersion>45 <groupId>me.gacl</groupId>6 <artifactId>system-parent</artifactId>7 <version>1.0-SNAPSHOT</version>8 <packaging>pom</packaging> 91

5、0 <name>system-parent</name>11 <url>http:/maven.apache.org</url>1213 <properties>14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>15 </properties>1617 <dependencies>18 <dependency>19<groupId>junit</groupId>20

6、 <artifactId>junit</artifactId>21<version>3.8.1</version>22<scope>test</scope>23 </dependency>24 </dependencies>25 </project>二、创建 sytem-domain 模块在命令行进入创建好的 system-parent 目录,然后执行下列命令:mvnarchetype:create-DgroupId=me.gacl-DartifactId=system-domain-D

7、archetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 命令执行完成之后可以看到在system-parent 目录中生成了 system-domain , 里面包含 src 目录和 pom.xml 文件。 同时,在 system-parent 目录中的 pom.xml 文件自动添加。<modules><module>system-domain</module></modules>这时, system-parent 的 pom.xml 文件。1 <?xml

8、version="1.0" encoding="UTF-8"?>2<projectxmlns="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">3 <mo

9、delVersion>4.0.0</modelVersion>45 <groupId>me.gacl</groupId>6 <artifactId>system-parent</artifactId>7 <version>1.0-SNAPSHOT</version>8 <packaging>pom</packaging>911 <url>http:/maven.apache.org</url>1213 <properties>14 <pro

10、ject.build.sourceEncoding>UTF-8</project.build.sourceEncoding>15 </properties>1617 <dependencies>18 <dependency>19<groupId>junit</groupId>20 <artifactId>junit</artifactId>21<version>3.8.1</version>22<scope>test</scope>23 <

11、/dependency>24 </dependencies>25 <modules>26 <module>system-domain</module>27 </modules>28 </project>修 改 system-domain 目 录 中 的 pom.xml 文 件 , 把 <groupId>me.gacl</groupId> 和 <version>1.0-SNAPSHOT</version"掉,力口上 <packaging>jar</p

12、ackaging> ,因为 groupId 和 version 会继承 system-parent 中的 groupId 和 version , packaging 设置打包方式为 jar 修改过 后的 pom.xml 文件1 <?xml version="1.0"?>2<projectxsi:schemaLocation="http:/maven.apache.org/POM/ 4.0.0http:/maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http:/maven.apa

13、che.org/POM/ 4.0.0"3 xmlns:xsi="http:/www.w3.org/ 2001/XMLSchema-instance">4 <modelVersion>4.0.0</modelVersion>5 <parent>6<groupId>me.gacl</groupId>7<artifactId>system-parent</artifactId>8<version>1.0-SNAPSHOT</version>9 </pa

14、rent>1011 <artifactId>system-domain</artifactId>12 <packaging>jar</packaging>1314 <name>system-domain</name>15 <url>http:/maven.apache.org</url>16 </project>三、创建 sytem-dao 模块在命令行进入创建好的 system-parent 目录,然后执行下列命令:mvnarchetype:create-DgroupId=me.

15、gacl-DartifactId=system-dao-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 命令执行完成之后可以看到在system-parent 目录中生成了 system-dao ,里面包含src 目录和 pom.xml 文件。 同时,在 system-parent 目录中的 pom.xml 文件。1 <?xml version="1.0" encoding="UTF-8"?>2<projectxmlns="http:

16、/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">3 <modelVersion>4.0.0</modelVersion>45 <groupId>me.gacl</groupId>6

17、<artifactId>system-parent</artifactId>7 <version>1.0-SNAPSHOT</version>8 <packaging>pom</packaging>910 <name>system-parent</name>11 <url>http:/maven.apache.org</url>1213 <properties>14 <project.build.sourceEncoding>UTF-8</pro

18、ject.build.sourceEncoding>15 </properties>1617 <dependencies>18 <dependency>19<groupId>junit</groupId>20 <artifactId>junit</artifactId>21<version>3.8.1</version>22<scope>test</scope>23 </dependency>24 </dependencies>25

19、<modules>26<module>system-domain</module>27<module>system-dao</module>28 </modules>29 </project>修 改 system-dao 目 录 中 的 pom.xml 文 件 , , 把 <groupId>me.gacl</groupId> 和 <version>1.0-SNAPSHOT</version"掉,力口上 <packaging>jar</pack

20、aging> ,因为 groupId 和 version 会继承 system-parent 中的 groupId 和 version , packaging 设置打包方式为 jar , 同时添 加对 system-domain 模块的依赖。1 <?xml version="1.0"?>xsi:schemaLocation="http:/maven.apache.org/POM/ 4.0.02<project http:/maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http:

21、/maven.apache.org/POM/ 4.0.0"3 xmlns:xsi="http:/www.w3.org/ 2001/XMLSchema-instance">4 <modelVersion>4.0.0</modelVersion>5 <parent>6<groupId>me.gacl</groupId>7<artifactId>system-parent</artifactId>8<version>1.0-SNAPSHOT</version>

22、;9 </parent>1011 <artifactId>system-dao</artifactId>12 <packaging>jar</packaging>1314 <name>system-dao</name>15 <url>http:/maven.apache.org</url>16 <properties>17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

23、;18 </properties>19 <dependencies>20<!-system-dao 需要使用到 system-domain 中的类,所以需要添加对 system-domain模块的依赖->21 <dependency>22<groupId>me.gacl</groupId>23<artifactId>system-domain</artifactId>24<version>$project.version</version>25 </dependency

24、>26 </dependencies>27 </project>四、创建 system-service 模块在命令行进入创建好的 system-parent 目录,然后执行下列命令:mvnarchetype:create-DgroupId=me.gacl-DartifactId=system-service-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false命令执行完成之后可以看到在 system-parent 目录中生成了 system-service , 里面包含 sr

25、c 目录和 pom.xml 文件。同时,在 system-parent 目录中的 pom.xml 文件。 。1 <?xml version="1.0" encoding="UTF-8"?>2<projectxmlns="http:/maven.apache.org/POM/ 4.0.0"xmlns:xsi="http:/www.w3.org/ 2001/XMLSchema-instance" xsi:schemaLocation="http:/maven.apache.org/POM/

26、4.0.0 http:/maven.apache.org/xsd/maven-4.0.0.xsd">3 <modelVersion>4.0.0</modelVersion> 45 <groupId>me.gacl</groupId>6 <artifactId>system-parent</artifactId>7 <version>1.0-SNAPSHOT</version>8 <packaging>pom</packaging> 910 <name&g

27、t;system-parent</name>11 <url>http:/maven.apache.org</url> 1213 <properties>14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>15 </properties> 1617 <dependencies>18 <dependency>19<groupId>junit</groupId>20 <artif

28、actId>junit</artifactId>21<version>3.8.1</version>22<scope>test</scope>23 </dependency>24 </dependencies>25 <modules>26<module>system-domain</module>27<module>system-dao</module>28<module>system-service</module>29

29、 </modules>30 </project>修 改 system-service 目 录 中 的 pom.xml 文 件 , , 把 <groupId>me.gacl</groupId> 和 <version>1.0-SNAPSHOT</version"掉,力口上 <packaging>jar</packaging> ,因为 groupId 和 version 会继承 system-parent 中的 groupId 和 version , packaging 设置打包方式为 jar , 同

30、时添 加对 system-dao 模块的依赖, system-service 依赖 system-dao 和 system-domain , 但是我们只 需添加 system-dao 的依赖即可,因为system-dao 已经依赖了 system-domain 。修改后的内容如下:1 <?xml version="1.0"?>2<projectxsi:schemaLocation="http:/maven.apache.org/POM/ 4.0.0http:/maven.apache.org/xsd/maven-4.0.0.xsd" x

31、mlns="http:/maven.apache.org/POM/ 4.0.0"3 xmlns:xsi=" 2001/XMLSchema-instance">4 <modelVersion>4.0.0</modelVersion>5 <parent>6<groupId>me.gacl</groupId>7<artifactId>system-parent</artifactId>8 <version>1.0-SNAPSHOT</version>

32、;9 </parent>1011 <artifactId>system-service</artifactId>12 <packaging>jar</packaging>1314 <name>system-service</name>15 <url>http:/maven.apache.org</url>16 <properties>17 <project.build.sourceEncoding>UTF-8</project.build.sourceEnc

33、oding>18 </properties>19 <dependencies>20<!-21system-service 依赖 system-dao 和 system-domain ,22 但 是 我 们 只 需 添 加 system-dao 的 依 赖 即 可 , 因 为 system-dao 已 经 依 赖 了 system-domain23->24 <dependency>25<groupId>me.gacl</groupId>26<artifactId>system-dao</artifac

34、tId>27<version>$project.version</version>28 </dependency>29 </dependencies>30 </project>五、创建 system-web 模块在命令行进入创建好的 system-parent 目录,然后执行下列命令:mvnarchetype:create-DgroupId=me.gacl-DartifactId=system-web-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=fa

35、lse命令执行完成之后可以看到在system-parent 目录中生成了 system-web ,里面包含src 目录和 pom.xml 文件。在 system-websrcmainwebapp 目录中还生成了一个简单的 index.jsp,<html><body><h2>Hello World!</h2></body></html>system-websrcmainwebappWEB-INF 目录中生成了 web.xmlsystem-parent 目录中的 pom.xml 文件自动变成1 <?xml version

36、="1.0" encoding="UTF-8"?>2<projectxmlns="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">3 <modelVer

37、sion>4.0.0</modelVersion>45 <groupId>me.gacl</groupId>6 <artifactId>system-parent</artifactId>7 <version>1.0-SNAPSHOT</version>8 <packaging>pom</packaging> 910 <name>system-parent</name>11 <url>http:/maven.apache.org</url&

38、gt;1213 <properties>14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>15 </properties>1617 <dependencies>18 <dependency>19<groupId>junit</groupId>20 <artifactId>junit</artifactId>21<version>3.8.1</version>22&l

39、t;scope>test</scope>23 </dependency>24 </dependencies>25 <modules>26<module>system-domain</module>27<module>system-dao</module>28<module>system-service</module>29<module>system-web</module>30 </modules>31 </project&g

40、t;修 改 system-web 目 录 中 的 pom.xml 文 件 , , 把 <groupId>me.gacl</groupId> 和 <version>1.0-SNAPSHOT</version法掉,因为 groupId 和 version 会继承 system-parent 中的 groupId 和 version ,同时添加对system-service 模块的依赖,修改后的内容如下:1 <?xml version="1.0"?>xsi:schemaLocation="http:/maven.ap

41、ache.org/POM/ 4.0.02<project http:/maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http:/maven.apache.org/POM/ 4.0.0"3 xmlns:xsi="http:/www.w3.org/ 2001/XMLSchema-instance">4 <modelVersion>4.0.0</modelVersion>5 <parent>6<groupId>me.gacl</groupId

42、>7<artifactId>system-parent</artifactId>8<version>1.0-SNAPSHOT</version>9 </parent>1011 <artifactId>system-web</artifactId>12 <packaging>war</packaging>1314 <name>system-web Maven Webapp</name>15 <url>http:/maven.apache.org&l

43、t;/url>16 <dependencies>17<!-18system-web依赖 system-service19->20 <dependency>21<groupId>me.gacl</groupId>22<artifactId>system-service</artifactId>23<version>$project.version</version>24 </dependency>25 </dependencies>26 <build&g

44、t;27 <finalName>system-web</finalName>28 </build>29 </project>复制代码注意, web 项目的打包方式是war 。六、编译运行项目经过上面的五个步骤, 相关的模块全部创建完成, 怎么运行起来呢。 由于最终运行的是system-web 模块,所以我们对该模块添加jetty 支持,方便测试运行。修改system-web 项目的 pom.xml 如下:复制代码1 <?xml version="1.0"?>2<projectxsi:schemaLocatio

45、n="http:/maven.apache.org/POM/ 4.0.0http:/maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http:/maven.apache.org/POM/ 4.0.0"3 xmlns:xsi="http:/www.w3.org/ 2001/XMLSchema-instance">4 <modelVersion>4.0.0</modelVersion>5 <parent><groupId>me.gacl<

46、/groupId><artifactId>system-parent</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>system-web</artifactId><packaging>war</packaging><name>system-web Maven Webapp</name><url>http:/maven.apache.org</url>&l

47、t;dependencies><!-system-web 依赖 system-service-><dependency><groupId>me.gacl</groupId><artifactId>system-service</artifactId><version>$project.version</version></dependency></dependencies><build><finalName>system-web</finalName><plugins><!-配置Jetty插件-><plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId></plugin></plugins></build>6789101112131415161718192021222324252627282930313233343536</project>

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1