在项目开发过程中,经常需要部署代码到开发环境,每天可能有好多次,每次都需要mvn clean install,然后上传到服务器。实际上这些繁琐的步骤可以通过一个Maven插件wagon-maven-plugin来自动完成。
配置Linux服务器用户名和密码

为了让wagon-maven-plugin插件能SSH连上Linux服务器,首先需要在Maven的配置文件settings.xml中配置好server的用户名和密码。

<servers>
    <server>
        <id>linux-server-dev</id>
        <username>root</username>
        <password>123456</password>
    </server>
</servers>

文件上传到服务器

Maven项目可使用 mvn package 指令打包,打包完成后包位于target目录下,要想在远程服务器上部署,首先要将包上传到服务器。在项目的pom.xml中配置wagon-maven-plugin插件:
 

<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>linux-server-dev</serverId>
                <!-- serverId即上一步中在settings.xml中配置的service的id -->
                <fromFile>target/project_name.jar</fromFile>
                <!-- 要上传到服务器的文件,一般来说是jar或者war包 -->
                <url>scp://192.168.1.1/home/project_name/</url>
                <!-- 配置服务器的地址以及文件上传的目录。 -->
            </configuration>
        </plugin>
    </plugins>
</build>

配置完成后,运行命令:

mvn clean package wagon:upload-single

重启服务

部署项目不仅要把包传上服务器,而且还需要执行一些指令来重启服务。通常我们会把重启命令写入到一个脚本中,然后通过执行脚本来重启服务。
新建 restart.sh 文件,一般把改文件放在当前项目下,因为每个项目的启动脚本都不一样
#!/bin/bash

ps -ef | grep a8admin | grep qa |awk '{print $2}' |xargs kill -9
sleep 1
nohup java -jar -Dspring.profiles.active=qa -Dserver.port=8083 a8admin-api-1.0-RELEASE.jar >> 8083.log 2>&1 &
tail -f 8083.log

 

<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>linux-server-dev</serverId>
                <fromFile>target/project_name.jar</fromFile>
                <url>scp://192.168.20.128/home/project_name/</url>
                <commands>
                    <command>sh ./restart.sh</command>
                </commands>
                <!-- 显示运行命令的输出结果 -->
                <displayCommandOutputs>true</displayCommandOutputs>
            </configuration>
        </plugin>
    </plugins>
</build>

配置完成后,运行命令:

mvn clean package wagon:upload-single wagon:sshexec -f pom.xml

上传war包并启动Tomcat

如果是Web应用,可使用服务器上的Tomcat来部署。
 

<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>linux-server-dev</serverId>
                <fromFile>target/project_name.war</fromFile>
                <!-- 上传到Tomcat的webapps目录下 -->
                <url>scp://192.168.20.128/home/project_name/</url>
                <commands>
                    <!-- 重启Tomcat -->
                    <command>sh /home/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
                    <command>rm -rf /home/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
                    <command>sh /home/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
                </commands>
                <displayCommandOutputs>true</displayCommandOutputs>
            </configuration>
        </plugin>
    </plugins>
</build>

完成以上配置后,同样可通过

mvn clean package wagon:upload-single wagon:sshexec

命令自动部署。
配置execution

如果你觉得 mvn clean package wagon:upload-single wagon:sshexec 命令太长了不好记,那么可以配置execution,在运行package打包的同时运行upload-single和sshexec。
 

<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>
                    <!-- 运行package打包的同时运行upload-single和sshexec -->
                    <phase>package</phase>
                    <goals>
                        <goal>upload-single</goal>
                        <goal>sshexec</goal>
                    </goals>
                    <configuration>
                        <serverId>linux-server-dev</serverId>
                        <fromFile>target/project_name.war</fromFile>
                        <url>scp://192.168.20.128/home/tomcat/apache-tomcat-7.0.55/webapps</url>
                        <commands>
                            <command>sh /home/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
                            <command>rm -rf /home/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
                            <command>sh /home/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
                        </commands>
                        <displayCommandOutputs>true</displayCommandOutputs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

配置完成后,即可使用

mvn clean package

来代替

mvn clean package wagon:upload-single wagon:sshexec

 

还有可以直接对svn,git上的代码编译,使用:Maven中为我们集成了软件配置管理的(SCM:Software Configuration Management)功能

scm命令:

scm:branch - branch the project(创建项目的分支)
scm:validate - validate the scm information in the pom(校验SCM的配置信息)
scm:add - command to add file(增加一个文件)
scm:unedit - command to stop editing the working copy(停止编辑当前COPY)
scm:export - command to get a fresh exported copy(拉一个全新的分支)
scm:bootstrap - command to checkout and build a project(checkout并编译工程)
scm:changelog - command to show the source code revisions(显示源码版本)
scm:list - command for get the list of project files(列出工程的文件)
scm:checkin - command for commiting changes(提交变更)
scm:checkout - command for getting the source code(获取源码)
scm:status - command for showing the scm status of the working copy(获取本地项目的状态)
scm:update - command for updating the working copy with the latest changes(从服务器获取最新的版本)
scm:diff - command for showing the difference of the working copy with the remote one(比较本地与远程服务器的差异)
scm:update-subprojects - command for updating all projects in a multi project build(更新子项目)
scm:edit - command for starting edit on the working copy(编辑)
scm:tag - command for tagging a certain revision(打标签)

配置及使用

<project>
  ...
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SCM Sample Project</name>
  <url>http://somecompany.com</url>
  <scm>
    <connection>scm:svn:http://somerepository.com/svn_repo/trunk</connection>
    <developerConnection>scm:svn:https://somerepository.com/svn_repo/trunk</developerConnection>
    <url>http://somerepository.com/view.cvs</url>
  </scm>
  ...
</project>

SCM支持的连接类型

SCM支持两种连接类型:connection 及 developerConnection。

以下是一个连接类型为connection的配置示例:

<project>
  ...
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-scm-plugin</artifactId>
        <version>1.8.1</version>
        <configuration>
          <connectionType>connection</connectionType>
        </configuration>
      </plugin>
      ...
    </plugins
    ...
  </build>
  ...
</project>

以下是一个连接类型为developerConnection的配置示例:

<project>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-scm-plugin</artifactId>
        <version>1.8.1</version>
        <configuration>
          <connectionType>developerConnection</connectionType>
        </configuration>
      </plugin>
      ...
    </plugins
    ...
  </build>
  ...
</project>

完成以上配置后,同样可通过下面的命令来发布代码

mvn scm:update package wagon:upload-single wagon:sshexec -f pom.xml