IDEA MAVEN專案多個module,使用tomcat熱部署
1、背景
專案使用maven多模組構建,線上使用tomcat釋出。一個模組為maven web專案,其餘均為非web。釋出時其餘幾個模組作為web模組的依賴引入,一起打包釋出。
2、問題
使用idea+tomcat進行開發,實現熱部署功能。tomcat釋出過程中可選擇war 和 war exploded兩種模式。開發環境使用war exploded實現熱部署。
問題1:研發環境只有web模組一個war exploded包可選,配置好tomcat啟動後檢視釋出包裡面內容依賴的幾個子模組的class及配置檔案均未打包進去進行釋出。
問題2:線上環境使用war打包,在idea命令列視窗構建完成後,生成的war包將子模組以jar包的形式放到lib中進行釋出。會造成子模組中的bean無法初始化,且不好進行增量抽取上線。
3、解決辦法
針對問題1:
在idea的tomcat配置釋出選項頁中選擇對應的war exploded包後,選擇狀態下點選編輯按鈕。如下圖紅色按鈕所示,在編輯視窗中選擇圖2中1處的classes,會顯示當前web模組(3標識,其餘為新增後的內容)的編譯輸出,全部模組在右側區域顯示。點選新增(2標識),彈出的圖3所示內容選擇Module Output,選中其他需要加入的module即可。
圖1
圖2
圖3
針對問題2:
使用maven-dependency-plugin外掛,對依賴子module在構建時進行unpack將jar包內容輸出到指定目錄即可。實現方式為在web module的pom.xml中新增如下配置。其中groupId,artifactId,version按照依賴的子module實際名稱配置即可。outputDirectory指定解包的目錄;includes指定需要輸出到outputDirectory中的檔案型別。其他配置及使用可自行查閱官方教程--》
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>X.Y</groupId>
<artifactId>aaa-bbb</artifactId>
<version>${project.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>.\target\classes</outputDirectory>
<includes>**/*.class,**/*.xml,**/*.properties,**/*.txt</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>