以下是我整个打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: Could not transfer artifact org
.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包!!!
山高路远,坑多难填。撸起袖子加油干,入坑岂有回头时!