欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用tomcat插件运行maven工程

程序员文章站 2024-01-11 16:46:52
...

笔者之前在网上浏览了各种各样的有关使用tomcat插件运行maven工程的例子,但是在搭建的过程中总是会遇到各种神奇的问题。最后笔者还是成功的搭建出来,过程很是曲折。所以呢笔者就总结前人的经验,加上自己的理解整理出一套比较简单可行的方法,方便以后使用tomcat插件运行maven工程的小伙伴们。

  1. setings.xml配置文件
    笔者之所选择将这部分作为讲解部分是因为笔者希望读者在搭建运行环境的过程中无需手动的添加任何JAR包,所有的jar均有项目自动生成并引入
    File->Settings…
    搜索输入maven,User settings file 为笔者的settings配置文件,Local repository为jar存放的路径(刚开始一个jar包都没有)
    使用tomcat插件运行maven工程
    settings.xml其中localRepository设置为本地存放jar包的路径
<settings>
  <localRepository>E:\jar\repository</localRepository>
</settings>

使用tomcat插件运行maven工程

  1. pom文件配置tomcat插件信息
 <url>http://maven.apache.org</url>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>

          <server>tomcat7</server>

          <port>80</port>

          <path>/test</path>
          <uriEncoding>UTF-8</uriEncoding>
          <contextReloadable>true</contextReloadable>
        </configuration>
      </plugin>
    </plugins>
  </build>

pom文件的url的作用非常大,我们之后用引用到的jar包都会从这上面下载下来

  1. 以springmvc为例,创建一个测试的controller和测试页面
    需要在pom文件引入spring依赖的jar包,笔者这里引的4.0.6版本的spring jar包这里写代码片

    pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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>
  <packaging>war</packaging>

  <name>spring</name>
  <groupId>com.bj.data</groupId>
  <artifactId>spring</artifactId>
  <version>1.0-SNAPSHOT</version>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springversion>4.0.6.RELEASE</springversion>
    <junitversion>3.8.1</junitversion>
    <velocityversion>1.7</velocityversion>
    <velocitytoolsversion>2.0</velocitytoolsversion>
  </properties>

  <url>http://maven.apache.org</url>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>

          <server>tomcat7</server>

          <port>80</port>

          <path>/spring</path>
          <uriEncoding>UTF-8</uriEncoding>
          <contextReloadable>true</contextReloadable>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity</artifactId>
      <version>${velocityversion}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity-tools</artifactId>
      <version>${velocitytoolsversion}</version>
      <!--<exclusions>-->
        <!--<exclusion>-->
          <!--<groupId>javax.servlet</groupId>-->
          <!--<artifactId>servlet-api</artifactId>-->
        <!--</exclusion>-->
      <!--</exclusions>-->
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.7.2</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${springversion}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>

  </dependencies>

</project>

测试controller,跳转到views下面的html文件:

package com.bj.data.spring.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

    @RequestMapping("test")
    public String test(){
        return "test";
    }
}

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<!--
  - This is the Cocoon web-app configurations file
  -
  - $Id$
  -->
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <!-- Servlet Filters ================================================ -->

  <!--
    - Declare a filter for multipart MIME handling
    -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <display-name>spring03</display-name>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:application-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application-*.xml</param-value>
  </context-param>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>

  </servlet-mapping>



</web-app>

spring配置文件application-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.bj.data.spring.controllers" />

    <!--  对模型视图名称的解析,即在模型视图名称添加前后缀 p:prefix中模板放置路径  -->
    <bean id="velocityConfig"
          class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/views/" />
        <property name="velocityProperties">
            <props>
                <prop  key="input.encoding">UTF-8</prop>
                <prop  key="output.encoding">UTF-8</prop>
            </props>
        </property>
    </bean>

    <!-- 配置后缀 -->
    <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="prefix" value="/" />
        <property name="contentType"><value>text/html;charset=UTF-8</value></property>
        <property name="suffix" value=".html" />
    </bean>




</beans>

这些文件配置完了之后,目录结构如下所示:
使用tomcat插件运行maven工程

4.更新pom.xml文件引入的jar包,这个过程项目会自动下载所需要的jar包
使用tomcat插件运行maven工程
这个过程可能比较慢,因为项目需要下载依赖的jar包,知道pom文件的红色错误信息消失即jar包下载完成,这时候就可以运行项目了

  1. 运行项目
    添加运行服务器
    使用tomcat插件运行maven工程
    点绿色加号,然后选择maven
    使用tomcat插件运行maven工程
    使用tomcat插件运行maven工程
    确定完了之后右上角就会有你刚刚配置好的服务器,选择你配置的运行服务器和运行方式(run 或 debug run),如果没有报错的话,说明你已经成功搭建好了,但是笔者这里遇到一个问题
    使用tomcat插件运行maven工程
    之所以产生这个问题的原因是笔者引的包重复了,所以需要把重复的包给去掉,笔者在一下地方重复引包javax.servlet,发生了冲突,
<dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity-tools</artifactId>
      <version>${velocitytoolsversion}</version>
</dependency>

因此只需要把重复的包给去掉即可,pom引入的方式改为以下:

<dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity-tools</artifactId>
      <version>${velocitytoolsversion}</version>
      <exclusions>
        <exclusion>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

保存之后,需要更新一下引用的jar包
使用tomcat插件运行maven工程

之后启动项目就能正常使用了
使用tomcat插件运行maven工程

然后使用浏览器访问:http://localhost/spring/test,能够正常访问到页面说明运行环境已经搭好
使用tomcat插件运行maven工程

笔者总结一下整个项目的搭建其实最关键的地方在于引用插件运行的jar包,具体如pom文件的build标签內的内容,因为其他引用的jar包方式我们都比较熟悉,还有一个地方就是重复引用出现的问题。到此为止,整个运行环境已经搭好,希望能够帮助给多的人。如有写错的地方,欢迎指出~