使用tomcat7-maven-plugin部署Web项目

 

一、环境准备
我使用的环境是:Window 10、Tomcat 8.0.36、maven3、tomcat7-maven-plugin 2.2版本。
二、设置环境变量
安装Tomcat8.0.36和maven之后设置环境变量,Tomcat设置环境变量时,key为必须为CATALINA_HOME。
1.设置maven环境变量
MAVEN =D:Program Files (x86)JetBrainsIntelliJ IDEA 2016.1.3pluginsmavenlibmaven3in
(我直接引用了InteljiIDEA中的maven)
2、设置TOMACAT环境变量
CATALINA_HOME=E: omcatapache-tomcat-8.0.36
3、将他们添加到PATH中
PATH=%MAVEN%;%CATALINA_HOME%in;

三、在Tomcat中配置用户权限
在tomcat的配置文件tomcat_user.xml(%CATALINA_HOME%conf omcat.user.xml)中的< tomcat-users >标签中添加

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

四、在Maven的settings.xml配置Server
在Maven的配置文件settings.xml(%MAVEN%confsettings.xml)中的< servers > 中加入

<server>
        <id>tomcat8</id>
        <username>username</username>
        <password>password</password>
    </server>
  • 1
  • 2
  • 3
  • 4
  • 5

这里的username 、password就填我们在tomcat中配置的那个用户名,和密码
这里的设置的 id为 tomcat8 我们将在项目的 pom.xml中填写。

五、在项目的pom.xml中配置tomcat7-maven-plugin插件
在< build> 中引入插件:

 <build>
        <pluginManagement>
            <plugins>
              ...
                <!-- 配置tomcat 插件 -->
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <url>http://localhost:8080/manager/text</url>
                        <server>tomcat8</server>
                        <path>/test</path>
                        <update>true</update>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        ....
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

注意
1、这里的 < server> 中填写的就是为们在%MAVEN%confsettings.xml配置中的那个id
即 tomcat8.
2、由于我用的tomcat8这里的< url> 必须配置成 xxx/manager/text否则会部署不成功

3、由于部署时会下载一些东西最还是在 pom.xml 的< project >标签下加入以下代码:

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

六、deploy
1、开启tomcat
2、在pom.xml所在的目录执行 mvn tomcat7:deploy 命令部署项目。

七、遇到的问题
1、在Window系统下执行在执行 mvn tomcat7:undeploy时,会有残留在tomcat目录下
解决方法:在tomcat的配置文件context.xml中 的< Context >标签中添加属性:antiJARLocking=”true” antiResourceLocking=”true”

<Context antiJARLocking="true" antiResourceLocking="true">




一个基本maven项目的pom.xml配置

标签: mavenpom.xml插件配置
分类:

继续之前创建的test项目,一个基本项目的pom.xml文件,通常至少有三个部分

第一部分,项目坐标,信息描述等

  1. <modelVersion>4.0.0</modelVersion>  
  2. <groupId>com.company.project</groupId>  
  3. <artifactId>module</artifactId>  
  4. <packaging>war</packaging>  
  5. <version>0.0.1-SNAPSHOT</version>  
  6. <name>test Maven Webapp</name>  
  7. <url>http://maven.apache.org</url>  

modelVersion:pom文件的模型版本

关于group id和artifact id,为了便于多人多模块协同开发管理(以后会讲),建议使用以下命名规范

group id:com.公司名.项目名

artifact id:功能模块名

packaging:项目打包的后缀,war是web项目发布用的,默认为jar

version:     artifact模块的版本

name和url:相当于项目描述,可删除

group id + artifact id +version :项目在仓库中的坐标

 

第二部分,引入jar包

  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>junit</groupId>  
  4.         <artifactId>junit</artifactId>  
  5.         <version>3.8.1</version>  
  6.         <scope>test</scope>  
  7.     </dependency>  
  8. </dependencies>  

这是创建项目时自动生成的,将junit-3.8.1.jar引入到项目中。

dependency:引入资源jar包到本地仓库,要引入更多资源就在<dependencies>中继续增加<dependency>

group id+artifact id+version:资源jar包在仓库中的坐标

scope:作用范围,test指该jar包仅在maven测试时使用,发布时会忽略这个包。需要发布的jar包可以忽略这一配置

刚开始本地仓库是空的,maven会从远程仓库自动下载这个jar到本地仓库,下载完后,就可以在项目中使用这个jar了

如果将<dependency>的内容删除,junit-3.8.1.jar也会自动消失,无法使用

第三部分,构建项目

  1. <build>  
  2.     <finalName>helloworld</finalName>  
  3.     <plugins>  
  4.         <plugin>  
  5.             <groupId>org.apache.maven.plugins</groupId>  
  6.             <artifactId>maven-compiler-plugin</artifactId>  
  7.             <version>3.5.1</version>  
  8.             <configuration>  
  9.                 <source>1.7</source>  
  10.                 <target>1.7</target>  
  11.             </configuration>  
  12.         </plugin>  
  13.         <plugin>  
  14.             <groupId>org.apache.maven.plugins</groupId>  
  15.             <artifactId>maven-resources-plugin</artifactId>  
  16.             <version>3.0.1</version>  
  17.             <configuration>  
  18.                 <encoding>UTF-8</encoding>  
  19.             </configuration>  
  20.         </plugin>  
  21.     </plugins>  
  22. </build>  

build:项目构建时的配置

finalName:在浏览器中的访问路径,如果将它改成helloworld,再执行maven--update,这时运行项目的访问路径是

                   http://localhost:8080/helloworld/   而不是项目名的  http://localhost:8080/test

plugins:插件,之前篇章已经说过,第一个插件是用来设置java版本为1.7,第二个插件是我刚加的,用来设置编码为utf-8

group id+artifact id+version:插件在仓库中的坐标

configuration:设置插件的参数值

 
原文地址:https://www.cnblogs.com/zhangtan/p/7649394.html