Maven入門(二) 多模塊項目

在平時的Javaweb項目開發中爲了便於後期的維護,咱們通常會進行分層開發,最多見的就是分爲domain(域模型層)、dao(數據庫訪問層)、service(業務邏輯層)、web(表現層),這樣分層以後,各個層之間的職責會比較明確,後期維護起來也相對比較容易,今天咱們就是使用Maven來構建以上的各個層。html

  項目結構以下:web

  system-parent
        |----pom.xml
        |----system-domain
                |----pom.xml
        |----system-dao
                |----pom.xml
        |----system-service
                |----pom.xml
        |----system-web
                |----pom.xml數據庫

1、建立system-parent項目

  建立system-parent,用來給各個子模塊繼承。apache

  進入命令行,輸入如下命令:服務器

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  以下圖所示:app

  

  命令執行完成以後能夠看到在當前目錄(C:\Documents and Settings\Administrator)生成了system-parent目錄,裏面有一個src目錄和一個pom.xml文件,以下圖所示:dom

  

  將src文件夾刪除,而後修改pom.xml文件,將<packaging>jar</packaging>修改成<packaging>pom</packaging>,pom表示它是一個被繼承的模塊,修改後的內容以下:webapp

複製代碼

1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   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>
 4 
 5   <groupId>me.gacl</groupId>
 6   <artifactId>system-parent</artifactId>
 7   <version>1.0-SNAPSHOT</version>
 8   <packaging>pom</packaging>
 9 
10   <name>system-parent</name>
11   <url>http://maven.apache.org</url>
12 
13   <properties>
14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15   </properties>
16 
17   <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 </project>

複製代碼

2、建立sytem-domain模塊

  在命令行進入建立好的system-parent目錄,而後執行下列命令:jsp

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-domain -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  以下圖所示:maven

  

  命令執行完成以後能夠看到在system-parent目錄中生成了system-domain,裏面包含src目錄和pom.xml文件。以下圖所示:

  

  

  同時,在system-parent目錄中的pom.xml文件自動添加了以下內容:

<modules>
    <module>system-domain</module>
</modules>

  這時,system-parent的pom.xml文件以下:

複製代碼

1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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/xsd/maven-4.0.0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4 
 5   <groupId>me.gacl</groupId>
 6   <artifactId>system-parent</artifactId>
 7   <version>1.0-SNAPSHOT</version>
 8   <packaging>pom</packaging>
 9 
10   <name>system-parent</name>
11   <url>http://maven.apache.org</url>
12 
13   <properties>
14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15   </properties>
16 
17   <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   <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</packaging>,由於groupId和version會繼承system-parent中的groupId和version,packaging設置打包方式爲jar

  修改事後的pom.xml文件以下:

複製代碼

1 <?xml version="1.0"?>
 2 <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"
 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   </parent>
10   
11   <artifactId>system-domain</artifactId>
12   <packaging>jar</packaging>
13   
14   <name>system-domain</name>
15   <url>http://maven.apache.org</url>
16 </project>

複製代碼

3、建立sytem-dao模塊

  在命令行進入建立好的system-parent目錄,而後執行下列命令:

mvn archetype:create -DgroupId=me.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 <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/xsd/maven-4.0.0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4 
 5   <groupId>me.gacl</groupId>
 6   <artifactId>system-parent</artifactId>
 7   <version>1.0-SNAPSHOT</version>
 8   <packaging>pom</packaging>
 9 
10   <name>system-parent</name>
11   <url>http://maven.apache.org</url>
12 
13   <properties>
14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15   </properties>
16 
17   <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   <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</packaging>,由於groupId和version會繼承system-parent中的groupId和version,packaging設置打包方式爲jar,同時添加對system-domain模塊的依賴,修改後的內容以下:

複製代碼

1 <?xml version="1.0"?>
 2 <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"
 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   </parent>
10 
11   <artifactId>system-dao</artifactId>
12   <packaging>jar</packaging>
13 
14   <name>system-dao</name>
15   <url>http://maven.apache.org</url>
16   <properties>
17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
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>
26   </dependencies>
27 </project>

複製代碼

4、建立system-service模塊

  在命令行進入建立好的system-parent目錄,而後執行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

  以下圖所示:

  

  命令執行完成以後能夠看到在system-parent目錄中生成了system-service,裏面包含src目錄和pom.xml文件。以下圖所示:

  

  同時,在system-parent目錄中的pom.xml文件自動變成以下內容:

複製代碼

1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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/xsd/maven-4.0.0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4 
 5   <groupId>me.gacl</groupId>
 6   <artifactId>system-parent</artifactId>
 7   <version>1.0-SNAPSHOT</version>
 8   <packaging>pom</packaging>
 9 
10   <name>system-parent</name>
11   <url>http://maven.apache.org</url>
12 
13   <properties>
14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15   </properties>
16 
17   <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   <modules>
26     <module>system-domain</module>
27     <module>system-dao</module>
28     <module>system-service</module>
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,同時添加對system-dao模塊的依賴,system-service依賴system-dao和system-domain,可是咱們只需添加system-dao的依賴便可,由於system-dao已經依賴了system-domain。修改後的內容以下:

複製代碼

1 <?xml version="1.0"?>
 2 <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"
 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   </parent>
10 
11   <artifactId>system-service</artifactId>
12   <packaging>jar</packaging>
13   
14   <name>system-service</name>
15   <url>http://maven.apache.org</url>
16   <properties>
17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18   </properties>
19   <dependencies>
20     <!--
21     system-service依賴system-dao和system-domain,
22     可是咱們只需添加system-dao的依賴便可,由於system-dao已經依賴了system-domain
23     -->
24      <dependency>
25       <groupId>me.gacl</groupId>
26       <artifactId>system-dao</artifactId>
27       <version>${project.version}</version>
28     </dependency>
29   </dependencies>
30 </project>

複製代碼

5、建立system-web模塊

  在命令行進入建立好的system-parent目錄,而後執行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

  以下圖所示:

  

  命令執行完成以後能夠看到在system-parent目錄中生成了system-web,裏面包含src目錄和pom.xml文件。以下圖所示:

  

  在\system-web\src\main\webapp目錄中還生成了一個簡單的index.jsp,以下圖所示:

  

  裏面的內容爲

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

  system-web\src\main\webapp\WEB-INF目錄中生成了web.xml

  

  同時,在system-parent目錄中的pom.xml文件自動變成以下內容:

複製代碼

1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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/xsd/maven-4.0.0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4 
 5   <groupId>me.gacl</groupId>
 6   <artifactId>system-parent</artifactId>
 7   <version>1.0-SNAPSHOT</version>
 8   <packaging>pom</packaging>
 9 
10   <name>system-parent</name>
11   <url>http://maven.apache.org</url>
12 
13   <properties>
14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15   </properties>
16 
17   <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   <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>

複製代碼

  修改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"?>
 2 <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"
 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   </parent>
10 
11   <artifactId>system-web</artifactId>
12   <packaging>war</packaging>
13   
14   <name>system-web Maven Webapp</name>
15   <url>http://maven.apache.org</url>
16   <dependencies>
17     <!--
18     system-web依賴system-service
19     -->
20      <dependency>
21       <groupId>me.gacl</groupId>
22       <artifactId>system-service</artifactId>
23       <version>${project.version}</version>
24     </dependency>
25   </dependencies>
26   <build>
27     <finalName>system-web</finalName>
28   </build>
29 </project>

複製代碼

   注意,web項目的打包方式是war

6、編譯運行項目

  通過上面的五個步驟,相關的模塊所有建立完成,怎麼運行起來呢。因爲最終運行的是system-web模塊,因此咱們對該模塊添加jetty支持,方便測試運行。修改system-web項目的pom.xml以下:

複製代碼

1 <?xml version="1.0"?>
 2 <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"
 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   </parent>
10 
11   <artifactId>system-web</artifactId>
12   <packaging>war</packaging>
13   
14   <name>system-web Maven Webapp</name>
15   <url>http://maven.apache.org</url>
16   <dependencies>
17     <!--
18     system-web依賴system-service
19     -->
20      <dependency>
21       <groupId>me.gacl</groupId>
22       <artifactId>system-service</artifactId>
23       <version>${project.version}</version>
24     </dependency>
25   </dependencies>
26   <build>
27     <finalName>system-web</finalName>
28     <plugins>
29         <!--配置Jetty插件-->
30         <plugin>
31             <groupId>org.mortbay.jetty</groupId>
32             <artifactId>maven-jetty-plugin</artifactId>
33         </plugin>
34     </plugins>
35   </build>
36 </project>

複製代碼

  在命令行進入system-parent目錄,而後執行下列命令:

mvn clean install

  以下圖所示:

  

  

  命令執行完後,在system-web目錄下多出了target目錄,裏面有了system-web.war,以下圖所示:

  

  命令行進入sytem-web目錄,執行以下命令,啓動jetty

mvn jetty:run

  以下圖所示:

  

  

  啓動jetty服務器後,訪問http://localhost:8080/system-web/ 運行結果以下圖所示:

  

7、導入Eclipse中進行開發

  操做步驟以下所示:

相關文章
相關標籤/搜索