使用Maven構建多模塊項目

GuiHuey 8年前發布 | 10K 次閱讀 Maven 項目構建

來自: http://my.oschina.net/weiweiblog/blog/610602


在平時的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

一、創建system-parent項目

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

  進入命令行,輸入以下命令:

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

  如下圖所示:

  

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

  

  將src文件夾刪除,然后修改pom.xml文件,將<packaging>jar</packaging>修改為<packaging>pom</packaging>,pom表示它是一個被繼承的模塊,修改后的內容如下:

復制代碼
 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>
復制代碼

二、創建sytem-domain模塊

  在命令行進入創建好的system-parent目錄,然后執行下列命令:

mvn archetype:create -DgroupId=me.gacl -DartifactId=system-domain -DarchetypeArtifactId=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 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>
復制代碼

三、創建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>
復制代碼

四、創建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>
復制代碼

五、創建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

六、編譯運行項目

  經過上面的五個步驟,相關的模塊全部創建完成,怎么運行起來呢。由于最終運行的是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/ 運行結果如下圖所示:

  

七、導入Eclipse中進行開發

  操作步驟如下所示:

  

  

  

  

  

 本文由用戶 GuiHuey 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!