maven 多工程搭建演示
maven出现后,很多公司会用maven来构建项目,单仅仅只是单项目单工程的
并没有使用多工程来构建,这样在以后,项目越来越大,业务越来越多以后,项目会难以维护,越发庞大,维护成本提高,团队士气也会下降
等等情况,使用maven构建多工程就是如今的趋势
这边演示一个maven工程相互依赖的例子,高手轻拍,大家互相学习,有不足之处请指出
1.新建demo-parent项目,作为父工程
选择quickstart
修改pom中的jar改为pom,这样的意思是当前工程为父工程,可以再其下创建子工程
2.新建demo-project-web,作为web工程,这个工程是总的web工程,下面将会包含另外2个子web工程
选择webapp作为web工程
3.同理创建demo-project-customer-web作为子web工程(webapp),这个工程是一个独立web工程,他包含了service,dao
4.同理创建demo-project-gods-web作为子web工程(webapp),这个工程将有一个demo-project-gods-core作为后台(core就是包含了service,dao)
5.创建demo-project-gods-core作为子工程(quickstart),依赖demo-project-gods-web
6.创建demo-common,作为子工程(quickstart),作用是工具包,可被其他工程依赖引用
3-6步骤略,生成项目如下图:
父工程pom会成这样:
1 <modules> 2 <module>demo-project-web</module> 3 <module>demo-project-gods-web</module> 4 <module>demo-project-customer-web</module> 5 <module>demo-project-gods-core</module> 6 <module>demo-common</module> 7 </modules>
好,那么现在来做工程之间的依赖
首先,demo-project-web作为顶级web项目,所有的子web项目打包后都会整合在此(如果同名文件则被顶级覆盖,如果顶级没有同名文件,但是子web有同名文件,则按照依赖顺序被优先依赖的同名文件覆盖)
pom如下:
1 <?xml version="1.0"?> 2 <project 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 4 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <parent> 8 <groupId>com.demo</groupId> 9 <artifactId>demo-parent</artifactId> 10 <version>0.0.1-SNAPSHOT</version> 11 </parent> 12 13 <groupId>com.demo</groupId> 14 <artifactId>demo-project-web</artifactId> 15 <version>0.0.1-SNAPSHOT</version> 16 <packaging>war</packaging> 17 <name>demo-project-web Maven Webapp</name> 18 <url>http://maven.apache.org</url> 19 20 <dependencies> 21 22 <!-- 增加要合并的war包依赖 --> 23 <dependency> 24 <groupId>com.demo</groupId> 25 <artifactId>demo-project-gods-web</artifactId> 26 <version>0.0.1-SNAPSHOT</version> 27 <type>war</type> 28 </dependency> 29 <dependency> 30 <groupId>com.demo</groupId> 31 <artifactId>demo-project-customer-web</artifactId> 32 <version>0.0.1-SNAPSHOT</version> 33 <type>war</type> 34 </dependency> 35 36 <dependency> 37 <groupId>junit</groupId> 38 <artifactId>junit</artifactId> 39 <version>3.8.1</version> 40 <scope>test</scope> 41 </dependency> 42 43 </dependencies> 44 45 <build> 46 47 <finalName>demo-project-web</finalName> 48 49 <plugins> 50 51 <plugin> 52 <groupId>org.apache.maven.plugins</groupId> 53 <artifactId>maven-war-plugin</artifactId> 54 <version>2.6</version> 55 <configuration> 56 <!-- 把另外2个web子工程加入进来, 合并war --> 57 <overlays> 58 <overlay> 59 <groupId>com.demo</groupId> 60 <artifactId>demo-project-gods-web</artifactId> 61 </overlay> 62 <overlay> 63 <groupId>com.demo</groupId> 64 <artifactId>demo-project-customer-web</artifactId> 65 </overlay> 66 </overlays> 67 </configuration> 68 </plugin> 69 70 </plugins> 71 </build> 72 73 </project>
修改demo-project-gods-web的pom,添加自身依赖core作为service以及DAO
1 <?xml version="1.0"?> 2 <project 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 4 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>com.demo</groupId> 8 <artifactId>demo-parent</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 </parent> 11 <groupId>com.demo</groupId> 12 <artifactId>demo-project-gods-web</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <packaging>war</packaging> 15 <name>demo-project-gods-web Maven Webapp</name> 16 <url>http://maven.apache.org</url> 17 18 <dependencies> 19 20 <!-- 引入依赖, core中包含service以及DAO --> 21 <dependency> 22 <groupId>com.demo</groupId> 23 <artifactId>demo-project-gods-core</artifactId> 24 <version>0.0.1-SNAPSHOT</version> 25 </dependency> 26 27 <dependency> 28 <groupId>junit</groupId> 29 <artifactId>junit</artifactId> 30 <version>3.8.1</version> 31 <scope>test</scope> 32 </dependency> 33 34 </dependencies> 35 36 <build> 37 <finalName>demo-project-gods-web</finalName> 38 </build> 39 </project>
在customer中添加工具类依赖
1 <?xml version="1.0"?> 2 <project 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 4 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>com.demo</groupId> 8 <artifactId>demo-parent</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 </parent> 11 <groupId>com.demo</groupId> 12 <artifactId>demo-project-customer-web</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <packaging>war</packaging> 15 <name>demo-project-customer-web Maven Webapp</name> 16 <url>http://maven.apache.org</url> 17 <dependencies> 18 19 <!-- 添加common工具类依赖 --> 20 <dependency> 21 <groupId>com.demo</groupId> 22 <artifactId>demo-common</artifactId> 23 <version>0.0.1-SNAPSHOT</version> 24 </dependency> 25 26 <dependency> 27 <groupId>junit</groupId> 28 <artifactId>junit</artifactId> 29 <version>3.8.1</version> 30 <scope>test</scope> 31 </dependency> 32 33 </dependencies> 34 <build> 35 <finalName>demo-project-customer-web</finalName> 36 </build> 37 </project>
============进入代码阶段============
parent是父工程,不需要编写代码
demo-common:
1 package org.demo.common; 2 3 import java.util.Date; 4 5 public class DateUtil { 6 7 public static void showTime(String name) { 8 System.out.println("Run in demo-common - Hello ~ " + name + ", it is " + new Date().toString()); 9 } 10 11 }
demo-project-customer-web:
1 package com.demo.project.customer.web; 2 3 import org.demo.common.DateUtil; 4 5 public class AbcService { 6 7 public static void getDateService(String name) { 8 DateUtil.showTime(name); 9 System.out.println("Run in demo-project-customer-web..."); 10 } 11 12 }
customer.jsp
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <%@page import="com.demo.project.customer.web.AbcService"%> 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 <html xmlns="http://www.w3.org/1999/xhtml" 7 xmlns:f="http://java.sun.com/jsf/core" 8 xmlns:h="http://java.sun.com/jsf/html"> 9 <head> 10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 11 <title>Insert title here</title> 12 </head> 13 <body> 14 <% 15 System.out.print("hello demo-project-customer-web customer.jsp"); 16 AbcService.getDateService("abc"); 17 %> 18 19 </body> 20 </html>
demo-project-gods-core:
1 package org.demo.project.gods.core; 2 3 4 public class Gods { 5 6 public static void getDateService(String name) { 7 System.out.println("Run in demo-project-gods-core..."); 8 } 9 10 }
demo-project-gods-web
index.jsp
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <%@page import="org.demo.project.gods.core.Gods"%> 3 <%@ page language="java" contentType="text/html; charset=UTF-8" 4 pageEncoding="UTF-8"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 <html xmlns="http://www.w3.org/1999/xhtml" 7 xmlns:f="http://java.sun.com/jsf/core" 8 xmlns:h="http://java.sun.com/jsf/html"> 9 <head> 10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 11 <title>Insert title here</title> 12 </head> 13 <body> 14 15 <% 16 System.out.print("hello demo-project-gods-web gods.jsp"); 17 Gods.getDateService("nathan"); 18 19 %> 20 </body> 21 </html>
run - mvn install:
运行成功,进入相应的页面则成功