maven部署web项目到tomcat7(兼容tomcat8)

时间:2022-11-18 13:37:45

1.下载tomcat7并配置好JAVA_HOME,tomcat7\webapps目录除了manager之外,其它都可以删除(删除没用的,可加速tomcat的启动).

2.tomcat7\conf\tomcat-users.xml加入

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="username" password="password" roles="manager-gui,manager-script"/>

当然用户名和密码可以不一样,但没有manager-gui就不能通过浏览器去访问manage应用,没有manager-script就不能通过manage去上传war包达到部署的目的.

3.在web项目的pom.xml的一个应用例子(注:url也必须有/text,否则也上传不了war包)

<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>testweb</artifactId>
	<packaging>war</packaging>
	<version>1.0</version>
	<name>testweb Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.deploy>deploy</project.deploy>
		<project.tomcat.version>8.0.0-RC5</project.tomcat.version>
	</properties>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>

<!-- 
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.6</version>
				<executions>
					<execution>
						<id>copy-resources</id>
						<phase>process-resources</phase>
						<goals>
							<goal>resources</goal>
						</goals>
						<configuration>
							<encoding>UTF-8</encoding>
							<outputDirectory>${project.build.directory}/${project.deploy}</outputDirectory>
							<resources>
								<resource>
									<directory>src/main/resources/</directory>
									<includes>
										<include>*.xml</include>
									</includes>
								</resource>
							</resources>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<archive>
						<addMavenDescriptor>false</addMavenDescriptor>
					</archive>
					<outputDirectory>${project.build.directory}/${project.deploy}</outputDirectory>
				</configuration>
				<executions>
					<execution>
						<phase>process-classes</phase>
						<goals>
							<goal>jar</goal>
						</goals>
						<configuration>
							<excludes>
								<exclude>*.properties</exclude>
							</excludes>
						</configuration>
					</execution>
				</executions>
			</plugin>
 -->

			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<url>http://localhost:8080/manager/text</url>
					<username>username</username>
					<password>password</password>
					<path>/${project.artifactId}</path>
				</configuration>
			</plugin>

		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-servlet-api</artifactId>
			<version>${project.tomcat.version}</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
</project>


4.tomcat7要先启动,在命令行转到项目目录下输入d:\maven3\bin\mvn tomcat7:deploy看结果.(注:不是tomcat:deploy)

在eclipse配置好外部的maven,在eclipse的run configurations建一个Maven Build:Name随便起一个名字(如tomcat7_redeploy),Base directory选项目目录${workspace_loc:/testweb},goals填tomcat7:deploy.或tomcat7:redeploy

deploy:如果已经部署过,再deploy就会失败.去tomcat7的webapps删除已经部署过的就行了.

redeploy:即使没部署过也不会失败.不知道build的速度会不会明显减慢??

至于选deploy还是redeploy,自己看着办吧!


直至今天(2013-11-11)还没看到有tomcat8-maven-plugin,但tomcat7-maven-plugin的2.2版已经兼容tomcat8.

只到2.2:https://repository.apache.org/content/repositories/maven-staging-group/org/apache/tomcat/maven/

snapshots看到有2.3:https://repository.apache.org/content/repositories/snapshots/org/apache/tomcat/maven/