Deploy war package to remote Tomcat server with Maven

Want to do: Deploy directly to the server with one line of command.

The first thing to do is to introduce this plugin in Maven (POM.XML):

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

This plugin called "tomcat7-maven-plugin" uses Tomcat's "manager" module to implement war package deployment, so you must make sure that Tomcat has the Manager module installed, (see if there is a Manager directory under webapp) Although its name has " tomcat7", but Tomcat8 also works.

Next, we configure the local Tomcat's manager module, open the local Tomcat's tomcat-users.xml file, and add the following configuration:

<user username="deployer" password="654321" roles="manager-script"/>

This user named "deployer" has the role set to "manager-script", indicating that he can use the background script management of Tomcat's manager module. If the role is "manager-gui", it means that this user can use the front end of the manager module. Web page management.

 

Next, improve the configuration of 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>

Configuration-Server

The server in Configuration is a variable called tomcat.deploy.server. It is not written to death because we want to deploy the WAR package to different servers, and this variable is determined by the previous properties. We can see that in the properties, tomcat .deploy.server is assigned to localTestServer, which is our default value, but by adjusting Maven's running parameters, we can change it to other values, so what does localTestServer mean? As mentioned earlier, the password of the server is set by yourself, and I don’t want others to know it. Other developers can decide their own local server password, but they don’t need to know my password, so the relevant content of this localTestServer is defined in In "~/.m2/settings.xml", it is usually in "C:\Users\(username)\.m2\settings.xml" under Windows7. Open this configuration file and add two servers to the servers, that 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>

Configuration-Server

The url in Configuration is also a variable. Obviously, it cannot be written to death, but it is not as sensitive as username and password, so it doesn't matter if it appears directly in POM.XML. By default, its value is "http:/ /localhost/manager/text", this address is the background script entry of Tomcat's Manager module, and the value of url can also be adjusted through Maven's running parameters.

Configuration-path

Where to deploy the WAR package? To specify a path, if the path is "/", that is to deploy as the legendary ROOT.WAR, this time we specify a path named MyWebAppDemo. After successful deployment, it can be accessed through http://localhost/MyWebAppDemo.

profile parameter

It is the parameter of Maven mentioned above. Here we set a parameter called deploy2production. If this parameter is brought when running mvn, it will be replaced by tomcat.deploy.server and tomcat.deploy.serverUrl in the parameter. Default value.

OK, all the work is done, it's time to deploy. let's start:

mvn tomcat7:redeploy

Both redeploy and deploy are fine.

Here is mvn with parameters:

mvn tomcat7:redeploy -Pdeploy2production

In this way, our program is deployed to the official server.

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

 

http://www.cnblogs.com/guogangj/p/5505228.html

Guess you like

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