The development process uses the Tomcat MAVEN plugin to continuously and quickly deploy Web projects

Source: Internet
Author: User

I used Hudson to deploy my Web project to tomcat on the test server in my usual work. The Hudson itself is integrated with SVN, Git, MAVEN, and supports the addition of various plugins. But if you use Hudson, I need to configure two tasks: one to package the project into a war, and the other to deploy the packaged war package to Tomcat on the target server. Although the task only needs to be configured once, each time you modify the code submission, you have to switch to the build Now button on the Hudson Build task page of the browser point, and then you have to jump to another page to see if there is an error, then click to perform the appropriate deployment task. It was annoying to find that the Tomcat MAVEN plugin supported the direct packaging and deployment of the project into Tomcat, which was finally taken care of and summarized here.

There are a lot of articles on this topic on the Internet, and the process is much the same, but in addition to the process, I want to record the pits I've stepped on and suggestions for such a deployment.

1. Preparatory work

Download the installation and configure Tomcat and Maven.

Prepare a MAVEN Web project.

2. MAVEN deployment Web project to Tomcat configuration

2.1. Configuring the Tomcat role

maven Automatic deployment is actually tuned to the manager feature in the Tomcat installation directory. In order to access the Http://localhost:8080/manager page normally, we need to modify the Tomcat-users.xml in the $tomcat_home/conf directory:

?
123456789 <tomcat-users>  <rolerolename="tomcat"/>  <rolerolename="manager"/>  <rolerolename="manager-gui"/>  <rolerolename="manager-script"/>  <rolerolename="admin-gui"/>  <userusername="tomcat"password="tomcat"roles="tomcat,manager,      manager-gui,manager-script,admin-gui" /></tomcat-users>

2.2. Modify Pom.xml add tomcat Maven Plugin

I use the tomcat7,pom.xml to add the following configuration:

?
1234567891011121314151617181920     <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <spring.version>3.2.2.RELEASE</spring.version>        <finalName>web-loab</finalName>    </properties>    <build>        <plugins>            <plugin>                <groupId>org.apache.tomcat.maven</groupId>                <artifactId>tomcat7-maven-plugin</artifactId>                <configuration>                    <url>http://localhost:8080/manager/text</url>                    <server>tomcat</server>                    <username>tomcat</username>                    <password>tomcat</password>                    <path>/${finalName}</path>                </configuration>            </plugin>        </plugins>    </build>

Above username, password from tomcat-users.xml. Server is the Tomcat server name. Path is the way to access the app. The URL specifies the Tomcat Management page path.

2.3. Modify MAVEN's Settings.xml

Locate Settings.xml in the $USER_HOME/.M2 directory and add the server node:

?
1234567 <servers>    <server>       <id>tomcat</id>       <username>tomcat</username>       <password>tomcat</password>    </server></servers>

the above username and password are still the same as in Tomcat-users.xml, with the same ID as the server in 2.2

2.4. Deploy the project to Tomcat

First make sure that the Tomcat server is started, then CD to the project root, and run the following command:

?
1 mvn clean tomcat7:redeploy

The deployment was successful as follows:

We can then locate the newly deployed war package under the WebApps directory of the Tomcat installation directory.

The first deployment is with the Tomcat7:deploy command, which can be redeployed with the Tomcat7:redeploy Command (which is recommended for unification), and the commands supported by the Tomcat Maven plugin include: Run, shutdown, run-war-only, Exec-war, standalone-war-only, deploy, Standalone-war, Undeploy, Run-war, redeploy, etc.

3. Step over the Pit

Looking at the above process, it seems to be very smooth, but the world is always not so smooth. Here are a few of the pits I've stepped on.

3.1, Windows system, redeploy process can not delete the old project directory

The error message is in the Catalina log file under $tomcat_home/logs, as follows:

?
123 信息: Undeploying context [/web-loab]十月 11, 2014 3:52:26 下午 org.apache.catalina.startup.ExpandWar deleteDir严重: [D:\tomcat\apache-tomcat-7.0.56\webapps\web-loab\WEB-INF] could not be completely deleted. The presence of the remaining files may cause problems

Probably because TOMCAT is still using this directory and cannot be deleted, $tomcat_home/conf/context.xml must be modified:

?
1 <ContextantiJARLocking="true"antiResourceLocking="true">

3.2, Servelt.class offending

This problem should not fall within the scope of this article, but it may cause the Web project to start up but can not access, the error message is as follows:

?
12 十月 11, 2014 3:46:29 下午 org.apache.catalina.loader.WebappClassLoader validateJarFile信息: validateJarFile(D:\tomcat\apache-tomcat-7.0.56\webapps\web-loab\WEB-INF\lib\servlet-api-6.0.29.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class

The reason is that there is servlet-api.jar in the Web-inf/lib directory for a Web project under the WebApps directory, delete it, and specify Servelt-api.jar's scope as provided in Pom.xml:

?
123456 <dependency>    <groupId>org.apache.tomcat</groupId>    <artifactId>servlet-api</artifactId>    <version>6.0.29</version>    <scope>provided</scope></dependency>

3.3. Version issues

Make sure that the Java build path for your Web project uses the JDK version, the compiled JDK version of Java compiler, and the Java version in project Facets.

If the TOMCAT6 is used, the Tomcat6-maven-plugin is configured in Pom.xml, and Tomcat7-maven-plugin is used if TOMCAT7 is used. or by default with Tomcat-maven-plugin.

4. Some suggestions for deploying a project using the Tomcat Maven plugin

This approach enables continuous and rapid deployment. But it has some limitations:

    • Requires direct access to the network segment of the Tomcat server from the local development environment

    • The history deployment package cannot be preserved

Therefore, the initial recommendation is to use this deployment method only in the development environment, and with the SVN, Git and other version control software to do two internal conventions:

    • All deployable version codes must be checked in to a branch named Deploy-xx, XX represents the currently deployable version, the Deploy branch code must be guaranteed to be deployable code, and then cut to the DEPLOY-XX branch redeploy project

    • When new features are added later, you need to create another deploy branch and increase the version number. This allows you to use version control software to help us preserve the various historical deployable code (which addresses the second limitation mentioned above). Especially with multiple project integrations, it is best to ensure that each project's deploy branch has the same version suffix as each time it is integrated. This facilitates collective rollback of individual project code

Finish! Reprint Please specify source: http://my.oschina.net/feichexia/blog/326893

The development process uses the Tomcat MAVEN plugin to continuously and quickly deploy Web projects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.