参考文章
http://blog.csdn.net/maosijunzi/article/details/21160965
我的环境是tomcat7.0.54 maven3.2.3
1eclipse配置maven
window-》prefrences-》maven-》user setting。如图
2配置tomcat和maven
进入tomcat_home/conf/tomcat_users.xml:修改如下:
<role rolename="tomcat"/><role rolename="role1"/>
<role rolename="manager"/>
<role rolename="admin"/>
<user username="manager" password="manager" roles="manager"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="admin" roles="admin,manager,tomcat,manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>
</tomcat-users>
注意admin用户的角色设置.
进入maven_home/conf/settings.xml:修改如下:
<server>
<id>tomcat</id>
<username>admin</username>
<password>admin</password>
</server>
eclipse创建maven web项目
这个简单,new-》other-》maven project-》next。这里注意groupid选org.apache.maven.archetypes ,artifactid 选maven-archetype-webapp,然后next,输入我们自己的groupid(com.test),artifactid(transition),然后finish,OK。如图所示:
修改pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>transition</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>transition Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>maven_remote_1</id>
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>transition</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<warFile>target/transition.war</warFile>
<server>tomcat</server>
<url>http://localhost:8080/manager/text</url>
<server>tomcat</server>
<username>admin</username>
<password>admin</password>
<path>/transition</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
运行maven项目
先进入tomcat目录启动tomcat,然后右键项目 run as-》run configrations:
Goals项输入:package tomcat:redeploy
Maven Runtime选择我们自己的maven。如图:
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.254 s
[INFO] Finished at: 2014-12-10T11:23:47+08:00
[INFO] Final Memory: 15M/105M
出现BUILD SUCCESS 则说明部署成功,浏览器输入:http://localhost:8080/transition/。显示Hello world..
注意:这样成功之后项目会直接打成war包,部署到tomcat下,每次build不需要重启tomcat。这样致命的缺点就是,不能调试。如果需要调试且在eclipse中启动tomcat的话。会发现右键项目没有run as server,有两种方法解决:
第一种:这时需要把项目转变成dynamic web module。右键项目-》properties-》project facets,然后右边选中dynamic web module。之后就会出现run as server了.
第二种:右键项目,run/debug as configrations 如上第8步骤的图。不过Goals中天上【tomcat:run】。然后run就OK了,注意这里不需要提前启动tomcat。