項目描述: springboot+springcloud+zookeeper+eureka+maven;為多模塊多module的分布式架構(gòu);
項目目錄結(jié)構(gòu)如下
父工程為server工程,其中有多個子module工程:
1、獨立子工程:db、model、quartz、redis、util、basecontroller;
2、獨立功能模塊:dao、service、controller;
其中dao、service、controller分別依賴db、model、quartz、redis、util、baseController,具體依賴關(guān)系如下圖所示:
問題描述: 在使用maven打包時打包報錯;
報錯描述:
[WARNING] The requested profile "dev" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.13.RELEASE:repackage (default) on project axis-login-dao: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.13.RELEASE:repackage failed: Unable to find main class -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
解決方案:該報錯為找不到mainclass錯誤:有人說直接在maven插件中加上mainclass就好了,可是我打的是dao層的包,他就是一個jar,不是一個可執(zhí)行程序,所以問題出在哪呢?請接著往下看:下面會報出程序包不存在的問題;
[INFO] Compiling 5 source files to D:\-server\-dao\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/-server/-login-dao/src/main/java/com//system/dao/RoleDaoMapper.java:[3,33] 程序包com.ccx.axis.system.model不存在
[ERROR] /D:/-server/login-dao/src/main/java/com//system/dao/RoleDaoMapper.java:[4,33] 程序包com..system.model不存在
這個問題困擾了我兩天,本身不太了解maven,所以趁著這個機(jī)會多了解了一下;如果你也遇到了類似的問題,而且你也不懂maven構(gòu)建的生命周期,那么你要先去學(xué)習(xí)一下他,重要的事情說三遍:maven構(gòu)建的生命周期!maven構(gòu)建的生命周期!maven構(gòu)建的生命周期!
然后再過來看這個問題:我再父類的pom.xml 中添加了maven構(gòu)建的插件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< plugins > <!--創(chuàng)建項目時自帶的 --> < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > <!-- 自己添加的 --> < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < version >1.5.13.RELEASE</ version > < executions > < execution > < goals > < goal >repackage</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > |
根據(jù)網(wǎng)上的資料,找不到程序包的問題主要是在maven依賴相關(guān)的,我把所有的maven依賴都屢清楚了,可是還是構(gòu)建不成功,這個時候就需要一些騷操作了,比如在打包我的登錄模塊的時候,你不能一個個的從底層網(wǎng)上面打包,你需要一次性把包打好了:
首先看父類的pom.xml依賴關(guān)系:
* 以下為重點!!!!!*
首先要注意的是util、redis、model、db是獨立的module,login-dao、login-service 都依賴那幾個獨立的module,login-controller自帶啟動類,可以運行(需要依賴以上幾個module),login-controller的pom中需要添加啟動類(不然會報找不到主類(mainClass)錯誤);
這是前期的準(zhǔn)備工作,在準(zhǔn)備完成后,直接對父類進(jìn)行maven的構(gòu)建操作:先clean 在install;
最后完美解決問題!!!
另外這里寫幾個多module項目maven構(gòu)建時候需要注意的問題:
1、如果你的項目中有jsp,那么你不能打jar包,需要打war包,因為打jar包會找不到j(luò)sp資源;
2、如果你的項目都是純后臺的代碼,那么父類的pom對應(yīng)的packaging為pom,子類的pom的packaging為jar
1
2
3
4
5
|
< groupId >com.ccx</ groupId > < artifactId >ccx-XXX-server</ artifactId > < version >1.0-SNAPSHOT</ version > <!-- 父project的pom文件:不是jar,是pom--> < packaging >pom</ packaging > |
1
2
3
4
5
|
< groupId >com.ccx</ groupId > < artifactId >ccx-XXX-server</ artifactId > < version >1.0-SNAPSHOT</ version > <!-- 父project的pom文件:不是jar,是pom--> < packaging >pom</ packaging > |
3、不要把所有的jar包的依賴都添加到父類的pom.xml文件中;因為你在對子類工程進(jìn)行打包的時候,maven會把父類pom.xml中所有的jar包都打到子類的jar包中,這樣就多打了n遍不需要的jar包,所以,一個module需要哪個jar包就將這個jar包放到對應(yīng)的module中,不要一股腦全部放在父工程的pom.xml中;
4、注意jdk、maven和springboot的版本,我本地jdk1.8,springboot1.5.13、maven3.6,環(huán)境要一直,打包才不會出現(xiàn)錯誤。
5、對于上面出現(xiàn)的maven打包報錯的問題,我有兩點思考:
項目現(xiàn)狀:項目所有的jar包依賴都添加到了父類的pom中,正常來講,父類pom中僅僅規(guī)定版本號問題,不應(yīng)該將jar包添加到父類的pom中;另外,正常情況下打包也應(yīng)該是先打底層的包,在打上層的包,對于我目前的項目現(xiàn)狀來說,最底層打一次包,父pom中所有的jar包都會被打進(jìn)去,再網(wǎng)上一層打包的時候,父pom中的所有jar包又會被打一遍,而且還包括最底層的那個module,所以就會出問題,肯定打包不成功;
正確的方法應(yīng)該是:父類pom中規(guī)定好依賴jar包的版本號(僅限于版本信息);子類module依賴哪個jar包就將這個jar包添加到該子類module的pom文件中;打包時候從最底層的開始打,然后一層層網(wǎng)上打包;這樣就應(yīng)該不會出現(xiàn)我這個問題了。
我覺得以上兩點才是最重要的,如有錯誤,請指正,and歡迎交流
到此這篇關(guān)于詳解Maven多模塊打包遇到的問題解決方法的文章就介紹到這了,更多相關(guān)Maven多模塊打包內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/hp_yangpeng/article/details/80801060