Maven Plugin Wagon-maven-plugin automates deployment of Java projects to Linux remote servers

Source: Internet
Author: User

Typically, deployment to a Linux server on-line after the development of a program may require a series of tedious steps such as packaging, uploading the package files to the server, SSH to the server, and knocking the command launcher. In fact, these steps can be accomplished via a MAVEN plugin wagon-maven-plugin.

Configure the Linux server user name and password

In order for the Wagon-maven-plugin plugin to ssh into a Linux server, you first need to configure the server's user name and password in MAVEN configuration file settings.xml.

<servers>    <server>        <id>mylinuxserver</id>        <username>xxg</username>        <password>123456</password>    </server></servers>
Uploading files to the server

The MAVEN project can be mvn package packaged using instructions, and after packaging is complete, the package is located in the target directory, and to deploy on the remote server, the package is first uploaded to the server.

Configure the Wagon-maven-plugin plugin in the Pom.xml of your project:

<build>    <extensions>        <extension>            <groupId>Org.apache.maven.wagon</groupId>            <artifactid>Wagon-ssh</artifactid>            <version>2.8</version>        </extension>    </Extensions>    <plugins>        <plugin>            <groupId>Org.codehaus.mojo</groupId>            <artifactid>Wagon-maven-plugin</artifactid>            <version>1.0</version>            <configuration>                <serverid>Mylinuxserver</ServerID>                <fromfile>Target/test.jar</fromfile>                <URL>Scp://192.168.20.128/home/xxg/desktop</URL>            </configuration>        </plugin>    </plugins></Build>

<serverId>mylinuxserver</serverId>The ID of the service configured in Settings.xml in the previous step is the <fromFile>target/test.jar</fromFile> file to upload to the server, typically a jar or war package, the <url>scp://192.168.20.128/home/xxg/Desktop</url> address of the configuration server, and the directory where the file was uploaded.

After the configuration is complete, run the command:

mvn clean package wagon:upload-single

package, wagon:upload-single packaging and uploading the items separately. After the command is finished, the file is successfully uploaded to the Linux server.

Execute the linux command on the server

Deploying a project not only passes the package on to the server, but also requires executing some instructions to start the program. You may also need to close the original program before the program starts.

Run the jar file

The boot jar package typically uses java -jar test.jar commands to configure the command in Pom.xml:

<build>    <extensions>        <extension>            <groupId>Org.apache.maven.wagon</groupId>            <artifactid>Wagon-ssh</artifactid>            <version>2.8</version>        </extension>    </Extensions>    <plugins>        <plugin>            <groupId>Org.codehaus.mojo</groupId>            <artifactid>Wagon-maven-plugin</artifactid>            <version>1.0</version>            <configuration>                <serverid>Mylinuxserver</ServerID>                <fromfile>Target/test.jar</fromfile>                <URL>Scp://192.168.20.128/home/xxg/desktop</URL>                <commands>                    <!--Kill the original process--                    <command>Pkill-f Test.jar</command>                    <!--Restart Test.jar, the output of the program is written to the Nohup.out file--                    <command>Nohup java-jar/home/xxg/desktop/test.jar >/home/xxg/desktop/nohup.out 2>&amp;1 &amp;</command>                </Commands>                <!--show the output of the Run command--                <displaycommandoutputs>True</displaycommandoutputs>            </configuration>        </plugin>    </plugins></Build>

After the configuration is complete, run the command:

mvn clean package wagon:upload-single wagon:sshexec

package, wagon:upload-single and the wagon:sshexec operation of packaging, uploading, and running command commands separately for the project. After the command finishes, view the process on the server, ps -ef|grep test.jar or view the Nohup.out file, you can see that the Java program has started on the server.

Upload the war pack and start Tomcat

If you are a web app, you can use Tomcat on the server to deploy it.

<build>    <extensions>        <extension>            <groupId>Org.apache.maven.wagon</groupId>            <artifactid>Wagon-ssh</artifactid>            <version>2.8</version>        </extension>    </Extensions>    <plugins>        <plugin>            <groupId>Org.codehaus.mojo</groupId>            <artifactid>Wagon-maven-plugin</artifactid>            <version>1.0</version>            <configuration>                <serverid>Mylinuxserver</ServerID>                <fromfile>Target/javawebdeploy.war</fromfile>                <!--uploaded to Tomcat's WebApps directory--                <URL>Scp://192.168.20.128/coder/tomcat/apache-tomcat-7.0.55/webapps</URL>                <commands>                    <!--restart Tomcat --                    <command>sh/coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>                    <command>Rm-rf/coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>                    <command>sh/coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>                </Commands>                <displaycommandoutputs>True</displaycommandoutputs>            </configuration>        </plugin>    </plugins></Build>

Once you have completed the above configuration, you can also mvn clean package wagon:upload-single wagon:sshexec deploy automatically by command.

Configure execution

If you think the mvn clean package wagon:upload-single wagon:sshexec command is too long to remember, you can configure execution to run Upload-single and sshexec while the package is running.

<build>    <extensions>        <extension>            <groupId>Org.apache.maven.wagon</groupId>            <artifactid>Wagon-ssh</artifactid>            <version>2.8</version>        </extension>    </Extensions>    <plugins>        <plugin>            <groupId>Org.codehaus.mojo</groupId>            <artifactid>Wagon-maven-plugin</artifactid>            <version>1.0</version>            <executions>                <execution>                    <ID>Upload-deploy</ID>                    < run Upload-single and sshexec!--run package Packages                    <phase>Package</phase>                    <goals>                        <goal>Upload-single</goal>                        <goal>Sshexec</goal>                    </goals>                    <configuration>                        <serverid>Mylinuxserver</ServerID>                        <fromfile>Target/javawebdeploy.war</fromfile>                        <URL>Scp://192.168.20.128/coder/tomcat/apache-tomcat-7.0.55/webapps</URL>                        <commands>                            <command>sh/coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>                            <command>Rm-rf/coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>                            <command>sh/coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>                        </Commands>                        <displaycommandoutputs>True</displaycommandoutputs>                    </configuration>                </Execution>            </executions>        </plugin>    </plugins></Build>

Once the configuration is complete, you can use it mvn clean package instead mvn clean package wagon:upload-single wagon:sshexec .

Original address: http://xxgblog.com/2015/10/23/wagon-maven-plugin/
Reprint need to indicate the source!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Maven Plugin Wagon-maven-plugin automates deployment of Java projects to Linux remote servers

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.