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:

 

運行成功,進入相應的頁面則成功

來源: http://www.cnblogs.com/leechenxiang/p/5339597.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章