Deploy the war package to a remote tomcat server with MAVEN

Source: Internet
Author: User

We used to publish a Java Web program as a common practice is to make it into a war package, and then use the SSH tool to upload it to the server, and put it in the corresponding directory, let Tomcat automatically unpack, complete the deployment.

Obviously, it's not easy to do this, and we're probably going to get it wrong when we pull the file up with SSH. (Of course, the manufacturers will not have such a problem, because they have the OPS team dedicated to do this thing, haha)

Now I want to be: a line of command deployed to the local server, test it locally, no problem, a line of command deployed to the official server, in addition to the official server password only I know, only I can perform this deployment ( Other development team members do not know the official server password).

OK, let's start with Maven (POM. XML) introduced in this plugin:

    <Build>        <Plugins>            <plugin>                <groupId>Org.apache.tomcat.maven</groupId>                <Artifactid>Tomcat7-maven-plugin</Artifactid>                <version>2.2</version>            </plugin>        </Plugins>    </Build>

This plug-in called "Tomcat7-maven-plugin" uses Tomcat's "manager" module for the deployment of the war package, so it's important to ensure that Tomcat has the Manager module installed (see if there's a manager directory under WebApp) Although it has the name "Tomcat7", but Tomcat8 also applies, I use is Tomcat8. Next we configure the local Tomcat Manager module to open the local Tomcat tomcat-users.xml file and add this configuration:

<username= "Deployer"  password= "654321"  roles= " Manager-script "/>

This "deployer" user, the role set to "Manager-script", indicates that he can use the Tomcat Manager module of the background script management, BTW, if the role is "Manager-gui" Indicates that this user can use the front-end Web page management of the manager module. Then make a similar configuration to the official server.

Next, refine the configuration of the Tomcat7-maven-plugin:

    <Properties>        <Warpackagename>Mywebappdemo</Warpackagename>        <Tomcat.deploy.server>Localtestserver</Tomcat.deploy.server>        <TOMCAT.DEPLOY.SERVERURL>Http://localhost/manager/text</TOMCAT.DEPLOY.SERVERURL>    </Properties>    <Profiles>        < Profile>            <ID>Deploy2production</ID>            <Properties>                <Tomcat.deploy.server>ProductionServer</Tomcat.deploy.server>                <TOMCAT.DEPLOY.SERVERURL>Http://120.26.93.30:8080/manager/text</TOMCAT.DEPLOY.SERVERURL>            </Properties>        </ Profile>    </Profiles>    <Build>        <Finalname>${warpackagename}</Finalname>        <Plugins>            <plugin>                <groupId>Org.apache.tomcat.maven</groupId>                <Artifactid>Tomcat7-maven-plugin</Artifactid>                <version>2.2</version>                <Configuration>                    <Server>${tomcat.deploy.server}</Server>                    <URL>${tomcat.deploy.serverurl}</URL>                    <Path>/${warpackagename}</Path>                </Configuration>            </plugin>        </Plugins>    </Build>

OK, now to explain:

Configuration-server

The server in the configuration is a variable, Called Tomcat.deploy.server, I did not write dead, because we want to deploy the war package to a different server, and this variable is determined by the previous properties, we can see that the properties, Tomcat.deploy.server is assigned to the local TestServer, this is our default value, but by adjusting Maven's running parameters, we can change it to something else, so what does localtestserver mean? I said earlier, the server password is self-determined, and do not want others to know, other developers can decide their own local server password, but they do not need to know my password, so this localtestserver related content is defined in the "~/.m2/settings.xml "In, under Windows7 is usually in" C:\Users\ (username) \.m2\settings.xml "here. Open this configuration file and add two servers to the servers, which is the two servers we want to deploy the program to.

<Servers>    <Server>        <ID>ProductionServer</ID>        <username>Deployer</username>        <Password>123456</Password>    </Server>    <Server>        <ID>Localtestserver</ID>        <username>Deployer</username>        <Password>654321</Password>    </Server></Servers>

This seems to have nothing to say, this configuration is very self-explanatory.

Configuration-url

The URL in the configuration is also a variable, obviously, I can not write it dead, but it is not as sensitive as user name and password, so it does not matter whether it appears directly in Pom.xml, by default, its value is "http://localhost/manager/ Text ", this address is the background script entry for the Tomcat Manager module, and the value of the URL can also be adjusted using MAVEN's operating parameters.

Configuration-path

Where do you want to deploy the war package? To specify a path, if path is "/", that is, to be deployed as legendary Root.war, this time we have specified a path named Mywebappdemo. After successful deployment, it can be accessed through Http://localhost/MyWebAppDemo.

Profile parameters

As mentioned in the previous MAVEN parameters, here we set a parameter, called Deploy2production, when running MVN with this parameter, The default value is replaced with the Tomcat.deploy.server,tomcat.deploy.serverurl in the parameter.

OK, all the work is done and the rest is deployed. Let's go:

MVN Tomcat7:redeploy

Why redeploy instead of deploy? Because with the deploy word in my here will appear some problems, specific what problem will not say, perhaps you there normal, if no problem, use deploy also line AH. After this is done, our program is deployed to the local tomcat server. Again, this is the MVN with parameters:

MVN tomcat7:redeploy-pdeploy2production

So, our program is deployed to the official server.

If you want to remove the program, it's simple, just use undeploy instead of redeploy.

Deploy the war package to a remote tomcat server with MAVEN

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.