Maven这个东西说实话挺好用的,在做Tomcat使用的时候,发生不少情况,菊花疼,Tomcat6不支持JDK7,Tomcat8支持Jdk7,所以有些莫名奇妙的坑啊,还有tomcat还有两个版本,一个是apache官方的版本,另一个版本是org.codehaus.mojo,本宝宝表示只用过官方的,另一个版本貌似已经停止更新了
pom.xml
在Maven做web开发的时候,需要添加jsp-api、servlet-api、jstl的jar依赖,然后需要添加Tomcat的插件以及打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>yellowcong</groupId>
<artifactId>users</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>users Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- 导入jsp -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<!-- 导入servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<!-- 共用的配置说明,比如spring版本, 项目名称, jdk版本等 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.2.3.RELEASE</spring.version>
</properties>
<build>
<finalName>users</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- 配置war包的名称 -->
<warName>users</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0-beta-1</version>
<configuration>
<url>http://localhost:8080/users</url>
<httpPort>8080</httpPort>
<uriEncoding>${project.build.encoding}</uriEncoding>
<useBodyEncodingForURI>true</useBodyEncodingForURI>
</configuration>
</plugin>
</plugins>
</build>
</project>
Tomcat插件常用命令
下面的配置是针对tomcat6的,如果是tomcat7,需要使用 tomcat7:run
命令 | 描述 |
---|---|
tomcat:deploy | 部署一个web war包 |
tomcat:reload | 重新加载web war包 |
tomcat:start | 启动tomcat |
tomcat:stop | 停止tomcat |
tomcat:undeploy | 停止一个war包 |
tomcat:run | 启动嵌入式tomcat ,并运行当前项目 |
Tomcat插件常用配置
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0-beta-1</version>
<configuration>
<path>/users</path>
<url>http://localhost:8080</url>
<httpPort>8080</httpPort>
<uriEncoding>${project.build.encoding}</uriEncoding>
<useBodyEncodingForURI>true</useBodyEncodingForURI>
</configuration>
</plugin>
参数 | 描述 |
---|---|
url | 地址 |
path | 路径 ,这个可以和url配置在一起 |
port | 端口 |
httpPort | 端口 |
uriEncoding | 设定uri编码 |
useBodyEncodingForURI | 设置url是否编码 |
Tomcat版本
org.codehaus.mojo这个版本的tomcat好像都停止维护了
不论是tomcat7还是tomcat6,只要用到其中一种插件就行了.但是这两种插件还是有区别的:
第一种是apache官方的插件,支持deploy命令,如果已经部署到tomcat容器,第二次部署的时候就会报错,提示该项目已经部署.
第二种是第三方的插件,可以使用redeploy命令,可以重复部署同时,请在<project>
节点下增加仓库配置,不然可能插件找不到,导致报错:
Tomcat 7
<plugins>
<!-- 第一种方式: apache官方tomcat插件,支持deploy -->
<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>admin</server>
</configuration>
</plugin>
<!-- 第二种方式: 第三方tomcat插件,支持redeploy -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>admin</server>
<ignorePackaging>true</ignorePackaging>
</configuration>
</plugin>
</plugins>
Tomcat 6
<plugins>
<!-- apache官方tomcat插件,支持deploy -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
<configuration>
<url>http://localhost:8080/manager/html</url>
<server>admin</server>
</configuration>
</plugin>
<!-- 第三方tomcat插件,支持redeploy -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<url>http://localhost:8080/manager</url>
<server>admin</server>
<ignorePackaging>true</ignorePackaging>
</configuration>
</plugin>
</plugins>
对于不是管网的tomcat插件,需要配置仓库
<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>