From:http://my.oschina.net/angel243/blog/178554apache
http://yuandingjiema.iteye.com/blog/1752544api
之前在项目中不多使用Maven,最近本身学习了一下maven,真的是很是强大的项目构建工具,对于依赖包的定义及版本,以及依赖包的集中管理(中央仓库)都让人惊喜(原谅个人大惊小怪,虽然Maven出来好久了,但小弟刚接触),但发现开发Web项目时,须要手动部署到Web服务器(Tomcat7),若是能自动部署到Web服务器,而不用每次手动把target下编译好的war包拷贝到Tomcat下就更好了。tomcat
下面是具体的使用方法:服务器
新建系统变量CATALINA_HOME,值为:C:\opensource\tomcat-7.0.34(Tomcat的安装目录),而后Path的最后面添加%CATALINA_HOME%\lib; %CATALINA_HOME%\lib\servlet-api.jar;%CATALINA_HOME%\lib\jsp-api.jareclipse
Tomcat7的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户。这里我把所用的权限都添加给admin用户了,具体代码以下: jsp
<role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="admin" password="admin" roles="manager-gui,manager-script"/>
在Maven的安装路径找到conf目录下的setting.xml文件,在<servers>节点中添加tomcat7下配置的用户信息(id能够任意填写,但username和password必须和步骤1一致): maven
<server>
<id>tomcat7</id>
<username>admin</username>
<password>admin</password>
</server>
在pom.xml的<plugins>节点中,添加tomcat7的maven插件ide
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0-SNAPSHOT</version> <configuration> <url>http://localhost:8080/manager/text</url> <server>tomcat7</server> <username>admin</username> <password>admin</password> </configuration> </plugin>
在project节点下,在插件仓库(plugin repositories)和普通仓库(repositories)中添加如下仓库到pom.xml,保证maven能够从仓库中下载到tomcat-maven-plugin插件,少添加了这段信息,会出现以下报错信息:工具
No plugin found for prefix 'tomcat' in the current project and in the plug……学习
<repositories> <repository> <id>people.apache.snapshots</id> <url> http://repository.apache.org/content/groups/snapshots-group/ </url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>apache.snapshots</id> <name>Apache Snapshots</name> <url> http://repository.apache.org/content/groups/snapshots-group/ </url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories>
在maven build的goals中输入命令tomcat7:deploy便可发布,run as --- run configuration下
eclipse中使用maven插件的时候,运行run as maven build的时候报错及解决方案:
1. -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
能够设一个环境变量M2_HOME指向你的maven安装目录
M2_HOME=D:\Apps\apache-maven-3.3.1
而后在Window->Preference->Java->Installed JREs->Edit,在Default VM arguments中设置
-Dmaven.multiModuleProjectDirectory=$M2_HOME
2. No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
解决方案:参考http://blog.csdn.net/fox_lht/article/details/16369147
jdk的设置请指定jdk目录,而非jre目录,不然会出现如上错误
3. tomcat7-maven-plugin:2.0-SNAPSHOT:deploy (default-cli) on project : Cannot find war file
解决方案:对于生成的war包文件命名为{project.build.finalName}
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <warName>${project.build.finalName}</warName> </configuration> </plugin>
Maven内置变量说明: