Maven分模塊以及打war包


我們如何進行模塊化開發呢?

我們使用上面的例子進行演示,先進行合理的優化,我們希望dao和service作為通用的底層工具來使用,把它們合並成一個核心模塊(core),build成core.jar,簡單的Maven模塊化項目結構如下:

---------- mall //頂級項目 |------ pom.xml //packaging = pom |------ mall-util //通用工具 | |--- pom.xml //packaging = jar |------ mall-core //核心模塊 | |--- pom.xml //packaging = jar |------ mall-web-api //接口模塊 | |--- pom.xml //packaging = war |------ mall-web-admin//管理后台 | |--- pom.xml //packaging = war |------ mall-web-shop//商城前台 | |--- pom.xml //packaging = war

這些模塊中api、admin、shop均是可以單獨部署的web應用,相互之間沒有依賴關系,但都依賴於core模塊,而core模塊依賴於util模塊。接下來我們按照上述確定的結構來搭建項目結構。

使用IDEA來創建Maven多模塊項目

一、創建一個普通Maven項目

  1. New Project

image

  1. 填寫基本信息,這里使用ipr作為項目描述文件

image

  1. 普通Maven項目不需要使用Maven模板搭建

image

二、給Maven項目添加模塊

  1. New Module

image

  1. 填寫基本信息,jar項目同樣不需要使用Maven模板搭建

image

  1. 這個時候就可以看到,我們所添加的module已經被引入到parent的pom文件里了
<groupId>com.mall</groupId> <artifactId>mall</artifactId> <packaging>pom</packaging> //打包方式為pom <version>1.0-SNAPSHOT</version> <modules> <module>mall-util</module> </modules>
  1. 變更util模塊的構建方式為jar
<parent> <artifactId>mall</artifactId> <groupId>com.mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <packaging>jar</packaging> //打包方式為jar <artifactId>mall-util</artifactId>

三、給Maven項目添加web模塊

  1. 創建一個module,並選中“Create from archetype”選項,同時maven模板選擇webapp

image

  1. 接下來耐心的等待maven幫你創建好module,模塊信息已經被添加
<modules>
    <module>mall-util</module> <module>mall-web-admin</module> </modules>

目錄結構如下:

image

pom:

<parent> <artifactId>mall</artifactId> <groupId>com.mall</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>mall-web-admin</artifactId> <packaging>war</packaging> <name>mall-web-admin</name> <url>https://github.com/beiyoufx</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>mall-web-admin</finalName> </build>

四、添加模塊間的依賴關系

  1. 增加core與util的依賴

image

  1. 增加admin與core的依賴關系

image

admin與core、util的依賴鏈

image

多模塊項目的構建與發布

打包

image

所有在root項目中進行的構建都會傳遞到模塊中,例如root中的package會打包整個項目,當文件有變動時會進行重新聚合,其他命令同理。模塊中的package只會打包當前模塊。

使用source:jar命令會將源碼打包。

clean install 打出war包和源碼。

 

使用命令打包方法:

在Debug Configurations 參數 Goals 寫命令參數  DEBUG運行~

 

 

打包注意要點:eclipse工具

但是maven插件需要使用jdk,因此需要在eclipse修改Installed JRES
位置在-->【Window】-->【Prefrences】-->【Java】-->【Installed JREs】
詳見下圖。

 

 

 打完包后直接將war包扔到webapps里就可以了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM