maven hits the war package to the specified directory and tomcat

from

http://blog.csdn.net/qh_java/article/details/52205448

First, maven packs the war package to the specified directory

Preliminary solution:
In maven, you can use the <build> subdirectory <directory> to change the target directory, but < directory> can only be the directory relative to the current project.

Although the war package can also be typed into the directory outside the project, there will be a strange and deep directory under the project, which is a bit inappropriate, so use the following plugin to achieve it.

Use the maven-dependency-plugin plugin to type the war package to the specified path:


<build> 
        <plugins> 
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-war-plugin</ artifactId> 
                <version>2.1.1</version> 
                <configuration> 
                <!--Specify the path to web.xml --> 
                    <webXml> 
                    <!--指定jsp、js、css的路劲  --> 
                    <warSourceDirectory>WebRoot</warSourceDirectory> 
                </configuration> 
            </plugin> 
            <!--打war包到指定的目录下 -->  
            <plugin>   
                <groupId>org.apache.maven.plugins</groupId>   
                <artifactId>maven-dependency-plugin</artifactId>   
                <version>2.8</version>   
                <executions>   
                    <execution>   
                        <id>copy-war</id>   
                        <phase>package</phase>   
                        <goals>   
                            <goal>copy</goal>   
                        </goals>   
                        <configuration>   
                            <artifactItems>   
                                <artifactItem>   
                                    <groupId>${project.groupId}</groupId>   
                                    <artifactId>${project.artifactId}</artifactId>   
                                    <version>${project.version}</version>   
                                    <type>${project.packaging}</type>   
                                </artifactItem>   
                            </artifactItems>   
                          <!--   <outputDirectory>${dist.console.war.dir}</outputDirectory>   --> 
                          <!--指定war包保存地址--> 
                            <outputDirectory>D:\Javaee\mypackage</outputDirectory>   
                            <includes>    
                                <include>*.war</include>    
                            </includes>   
                        </configuration>   
                    </execution>   
                </executions>   
            </plugin> 
        </plugins> 
    </build> 



二、项目打包到tomcat下
环境:apache-maven-3.0.5. The

configuration process of apache-tomcat-7.0.39 is as follows:

1. apache-tomcat-7.0.39 configures C:\Program Files\apache-tomcat-7.0.39\conf\tomcat-users.xml, because tomcat7 does not configure manager access rights by default, so it needs to be in tomcat-users .xml add users and permissions


[html] view plain copy print? View code snippet on CODE Derive to my code snippet
<tomcat-users> 
    <role rolename="admin-gui"/> 
    <role rolename="admin-script "/> 
    <role rolename="manager-gui"/> 
    <role rolename="manager-script"/> 
    <role rolename="manager-jmx"/> 
    <role rolename="manager-status"/> 
    <user username ="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/> 
</tomcat-users> 
2. apache-maven-3.0.5 configures C:\Program Files\apache-maven-3.0.5\conf\settings.xml. In order to allow maven to access tomcat permissions, you need to add the user created above to settings. xml, as follows

[html] view plain copy print? View the code snippet on CODE Derived to my code snippet
<servers> 
    <!-- Configure tomcat-/manager/text access rights--> 
    <server> 
      <id>tomcat </id> 
      <username>admin</username> 
      <password>admin</password> 
    </server> 
  </servers> 
3. Add the pom.xml file in the project directory, add build, and configure the maven plugin of tomcat7, as follows configure

[html] view plain copy print? view snippet on CODE derived to my snippet
<build> 
        <finalName>myApp</finalName> 
        <!-- directory by default points to target -->  
        <!-- <  directory>${basedir}/target</directory>--> 
        <plugins> 
            <plugin> 
                <groupId>org.apache.tomcat.maven</groupId> 
                <artifactId>tomcat7-maven-plugin</artifactId> 
                <version>2.2</version> 
                <configuration> 
                    <url>http://localhost:8080/manager/text</url> 
                    <!-- server、username、password对应maven的setting下的配置 --> 
                    <server>tomcat</server> 
                    <username>admin</username> 
                    <password>admin</password> 
                    <path>/${project.build.finalName}</path> 
                    <!-- war file path points to target by default --> 
                    <!--<warFile>${basedir}/target/${project.build.finalName}.war</warFile>--> 
                </configuration> 
            </plugin> 
        </plugins> 
    </build> 
 ${project .build.finalName} This is marked

ok according to the xml path. The configuration is over here. Let's see how to package it under tomcat:
Command deployment:


1. Before deployment, you must start the tomcat7 service, C:\Program Files\ apache-tomcat-7.0.39\bin\startup.bat

  Find the root directory of the project file to be deployed, and execute the following maven command

  > mvn clean:install //clean is to clean up the output files, install compiles and packages, must be done before each package Execute clean to ensure that the latest file is released

  > mvn tomcat7:redeploy //The first release of tomcat7:deploy, and the second release of tomcat7:redeploy


2. Error problem

Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/deploy?path=%2FmyApp&war=

 


There are two reasons for the above problem:

  A. Because maven does not have permission to access http: //localhost:8080/manager/text, so you need to increase user permissions in tomcat-users.xml under apache-tomcat, and configure it in the maven setting file

  B. Due to the maven-tomcat plugin problem, through http://search .maven.org/ Search tomcat-maven-plugin, after finding the latest version, and finally execute > mvn tomcat:redeploy, the above error will always be displayed, if it is tomcat7 here, it is recommended to go directly through http://search.maven.org/ Search for the tomcat7-maven-plugin plugin, execute > mvn tomcat7:redeploy, and the deployment is successful; if tomcat6 directly searches for tomcat6-maven-plugin through http://search.maven.org/,

  so here you need to pay attention to tomcat7-maven- The introduction of the plugin plugin, the correct introduction will solve the above problems

  

  <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>

  > mvn tomcat7: redeploy
  can complete the deployment

maven package and deploy to tomcat The article comes from:

http://www.cnblogs.com/candle806/p/3785708.html

http://www.yiibai.com/maven/deploy-maven -based-war-file-to-tomcat.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988338&siteId=291194637