MyEclipse+Maven打可執行war包時遇到的一系列問題及解決方法詳解
以下是我整個打war包過程時遇到的一些問題以及我用到的解決方案,及時分享出來,給遇到同樣問題的小夥伴們予以借鑑,少走彎路。
先貼出來pom.xml中打war包需要的依賴
<build> <plugins> <!-- 打War外掛 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> </plugin> <!-- 編譯外掛 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <finalName>manageSystem</finalName> </build>
首先右鍵專案run as->maven install進行打包編譯時,報錯:
-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
解決方法:
設定一個環境變數M2_HOME指向你的maven安裝目錄,比如我的設定為:
M2_HOME=E:\Work\Maven\apache-maven-3.3.9(此路徑為你的maven的安裝目錄)
然後在回到MyEclipse中依次點選Window->Preference->Java->Installed JREs->Edit
在Default VM arguments中直接輸入 -Dmaven.multiModuleProjectDirectory=$M2_HOME。
第一個問題解決!
然後再次點選maven install時又跳出錯誤:
[ERROR] Plugin org.apache.maven.plugins:maven-war-plugin:2.6 or one of its d
ependencies could not be resolved: Failed to read artifact descriptor for org.ap
ache.maven.plugins:maven-war-plugin:2.6
.apache.maven.plugins:maven-war-plugin:2.6 from/to central (http://repo.
maven.apache.org/maven2): Connection to http://repo.maven.apache.org refused: Co
nnection timed out: connect -> [Help 1]
此問題很明顯是連線maven中央倉庫超時造成的,由於從maven中央倉庫往下拉資源過於緩慢,此時我們放棄去請求中央倉庫,改為連線國內的maven倉庫映象。我這裡用到的是阿里雲的映象倉庫地址。
具體做法是用編輯器開啟maven安裝包下conf資料夾裡的settings.xml檔案,找到<mirrors></mirrors>標籤,在該標籤之間加入以下配置:
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
這個時候再回到MyEclipse中再次執行maven install,發現可以順利地下載所需要的依賴資源了。
如果你其它地方沒有錯誤,此時再執行maven install應當是可以成功將專案進行編譯打包了。
打出的war包會生成在專案根路徑的target資料夾下。將打出來的war包直接扔到tomcat的wabapps目錄下就可以啟動tomcat訪問專案了。但是不要高興的太早,還有最後一個坑在等待著你。
啟動tomcat後你會發現雖然專案可以訪問,但是進去之後所有的資料都是空白,這時你開啟war編譯後的專案資料夾會發現竟然沒有mapper.xml對映檔案。有資料才怪!這是因為mapper.xml檔案存在於src/main/java路徑下,xml屬於資原始檔,預設是不會被打進war包的。
解決方法:在pom.xml檔案中<build></build>標籤里加入以下配置
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
問題解決!
執行maven clean先清空之前的編譯檔案,再maven install,終於打出一個可以完美執行的war包!!!
山高路遠,坑多難填。擼起袖子加油幹,入坑豈有回頭時!