Maven之Tomcat6、Tomcat7-yellowcong

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>

转载于:https://my.oschina.net/u/1987703/blog/1617626

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值