Note
  • Introduction
  • JS
    • JS / JQuery 代码、框架收藏
    • 用Mock.js + AngularJS来提高开发效率
    • 在JavaScript中监听 IME 键盘输入事件
    • 如何组织大型JavaScript应用中的代码?
    • 深入理解javascript原型和闭包
      • 1. 一切都是对象
      • 2. 函数和对象的关系
      • 3. prototype原型
      • 4. 隐式原型
      • 5. instanceof
      • 6. 继承
      • 7. 原型的灵活性
      • 8. 简述【执行上下文】上
      • 9. 简述【执行上下文】下
      • 10. this
      • 11. 执行上下文栈
      • 12. 简介【作用域】
      • 13. 【作用域】和【上下文环境】
      • 14. 从【自由变量】到【作用域链】
      • 15. 闭包
      • 16. 补充:上下文环境和作用域的关系
    • AngularJS
      • AngularJS开发一些经验总结
      • AngularJS Controller 间通信机制
      • AngularJS 中的一些坑
      • 如何解决AngularJs在IE下取数据总是缓存的问题
      • AngularJS之$timeout指令
      • angular 代码日记
      • AngularJS Directive 隔离 Scope 数据交互
      • 在Angular指令中使用NgModelController做数据绑定
      • AngularJS的ngTransclude
      • 使用Angularjs的ng-cloak指令避免页面乱码
      • Directive - Compile vs. Link
    • NodeJS
      • npm的配置管理及设置代理
    • Angular
      • Angular2 - Control Validation on blur
      • Angular2 - how to call component function from outside the app
  • Go
    • How to Test Local Changes with Go Mod
  • Java
    • Java中根据字体得到字符串高度和长度
    • JAVA 判断一个字符串是不是一个合法的日期格式
    • JVM上的随机数与熵池策略
    • tomcat7+jdk的keytool生成证书 配置https
    • Spring
      • SpringMVC中用@ParamVariable传递的参数包含斜杠(/)时,匹配不了报404错误的解决方案
      • SpringMVC 中HttpMessageConverter简介和Http请求415 Unsupported Media Type的问题
      • Spring Boot 打包,分离依赖jar,配置文件
      • SpringBoot启动后 Stopping service [Tomcat]
      • spring-boot打包时排除第三方依赖
    • 第一个Eclipse插件
    • Eclipse 使用技巧
    • HighChart利用servlet导出中文PNG图片乱码问题解决
    • JDK中文字体在Linux操作系统的设置方案
    • Shiro
      • Shiro-Authentication(身份验证)
      • Shiro-Authorization(授权)
    • 详谈再论JAVA获取本机IP地址
  • Python
    • pip国内镜像源的配置
  • Linux
    • Shell脚本 bad interpreter:No such file or directory & /bin/bash^M: bad interpreter错误解决方法
    • 10个重要的Linux ps命令实战
    • Linux 下执行定时任务 crontab 命令详解
    • CentOS 7.x设置自定义开机启动,添加自定义系统服务
    • CentOS 7 下使用 Firewall
    • 在Ubuntu 12.04安装和设置SSH服务
    • 配置CENTOS YUM更新源
    • Linux下终端利器tmux
    • scp命令
    • Linux系统管理常用命令
    • 让你提升命令行效率的 Bash 快捷键 [完整版]
    • SELinux
    • Linux下 RabbitMQ的安装与配置
    • SELinux下更改mysql端口
    • centos 7 升级后yum install出现Exiting on user cancel
    • Linux Shell 通配符、元字符、转义符使用实例介绍
  • Other
    • Docker
      • Docker Proxy
    • Swagger
      • Spring MVC 集成 Swagger
    • Maven
      • maven plugin的execution出错
      • nexus repair或update index 没反应 速度慢 手动配置nexus index
      • nexus 搭建 maven 私服
      • Maven类包冲突终极解决小技若干
      • org.codehaus.plexus.archiver.jar.Manifest.merge(org.codehaus.plexus.archiver.jar.Manifest)
      • Maven 生命周期
      • wagon-maven-plugin
      • Maven中-DskipTests和-Dmaven.test.skip=true的区别
    • MySQL
      • is not allowed to connect to this MySQL server 解决办法
      • MySQL备份--mysql dump
      • MySQL启动中 InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes 的问题
      • MySQL修改root密码的多种方法
      • MySQL长事务导致的Table Metadata Lock
      • com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
      • 解决mysql插入数据时出现Incorrect string value: '\xF0\x9F...' for column 'name' at row 1的异常
    • HTTP
      • HTTP 协议中的 Transfer-Encoding
      • 四种常见的 POST 提交数据方式
      • HTTP 请求头中的 X-Forwarded-For
      • 传统轮询、长轮询、服务器发送事件与WebSocket
    • Raspberry
      • 系统安装配置
    • VIM
      • vim常用配置
      • 在vim中使用查找命令查找指定字符串
      • Vim缩进有关的设置总结
    • Git
      • github 同步一个 fork
      • Git 如何 clone 非 master 分支的代码
      • git提示error setting certificate verify locations解决办法
      • git设置和取消代理
      • How to Delete Commit History in Github
      • Windows下使用Beyond Compare作为git的比对与合并工具
    • 解决 Virtualbox 共享文件夹 cannot create symlink error 问题
    • nginx 403 forbidden 二种原因
    • VirtualBox的四种网络连接方式
    • 在Windows的CMD中如何设置支持UTF8编码?
    • nginx使用ssl模块配置HTTPS支持
    • 怎么查看WINDOWS端口被哪个程序占用
    • VMWare 随系统启动指定虚拟机
    • 使用命令行启动VirtualBox虚拟机
    • 该死的^M
    • install home-assistant mosquitto-broker and node-red on android
    • SSH原理与运用:远程登录、远程操作与端口转发
    • SVN mime-type 笔记
    • VirtualBox虚拟机网络配置(NAT + Host-only - Bridged)
    • VirtualBox绿色版的桥接网卡驱动安装
    • OAuth的改变
    • MavenActionUtil.getMavenProject(e.getDataContext()) is Null when developing a intellij plugin
    • Cygwin的安装和配置apt-cyg源
    • Zookeeper运维常用四字命令
    • svn sqlite[S11]: database disk image is malformed
Powered by GitBook
On this page
  • 文件上传到服务器
  • 单个文件
  • 多个文件
  • 在服务器上执行Linux命令
  • 运行jar文件
  • 上传war包并启动Tomcat
  • 配置execution
  • 服务器独立配置

Was this helpful?

  1. Other
  2. Maven

wagon-maven-plugin

PreviousMaven 生命周期NextMaven中-DskipTests和-Dmaven.test.skip=true的区别

Last updated 5 years ago

Was this helpful?

<dependency>  
    <groupId>org.codehaus.mojo</groupId>  
    <artifactId>wagon-maven-plugin</artifactId>  
    <version>1.0</version>  
</dependency>

主要提供如下几个goal

  • uploads the specified file to a remote location.

  • uploads the specified set of files to a remote location.

  • downloads the specified file from a remote location.

  • downloads the specified set of files from a remote location.

  • lists the content of a specified location in a remote repository.

  • copies a set of files under a Wagon repository to another.

  • merges , including metadata, a Maven repository to another.

  • Executes a set of commands at remote SSH host.

使用前需要在build中加入

<build>
    [...]
    <extensions>  
        <extension>  
            <groupId>org.apache.maven.wagon</groupId>  
            <artifactId>wagon-ssh</artifactId>  
            <version>2.8</version>  
        </extension>  
    </extensions> 
    [...]
</build>

文件上传到服务器

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>
                <fromFile>target/test.jar</fromFile>
                <url>scp://user:password@server/home/xxg/Desktop</url>
            </configuration>
        </plugin>
    </plugins>
</build>

多个文件

<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>
                <fromDir>target</fromDir>
                <includes>lib/*,Hermes-service.jar</includes>
                <!-- <excludes>*</excludes> -->
                <url>scp://user:password@server</url>
                <toDir>/home/xxg/Desktop</toDir>
            </configuration>
        </plugin>
    </plugins>
</build>

fromDir是要上传的文件所在的目录 includes是要上传的文件的规则 excludes是无需上传的文件的规则 toDir是上传到服务器的所在目录 fromFile是要上传到服务器的文件,一般来说是jar或者war包 url配置服务器的用户、密码、地址以及文件上传的目录。

配置完成后,运行命令:

mvn clean package wagon:upload-single

package、wagon:upload-single分别对项目进行打包和上传操作。命令运行结束后,文件就会成功上传到Linux服务器。

在服务器上执行Linux命令

部署项目不仅要把包传上服务器,而且还需要执行一些指令来启动程序。在程序启动之前,可能还需要将原来的程序关闭。

运行jar文件

启动jar包通常会使用java -jar test.jar命令,可以将命令配置在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>
                <fromFile>target/test.jar</fromFile>
                <url>scp://user:password@server/home/xxg/Desktop</url>
                <commands>
                    <!-- 杀死原来的进程 -->
                    <command>pkill -f test.jar</command>
                    <!-- 重新启动test.jar,程序的输出结果写到nohup.out文件中 -->
                    <command>nohup java -jar /home/xxg/Desktop/test.jar > /home/xxg/Desktop/nohup.out 2>&1 &</command>
                </commands>
                <!-- 显示运行命令的输出结果 -->
                <displayCommandOutputs>true</displayCommandOutputs>
            </configuration>
        </plugin>
    </plugins>
</build>

配置完成后,运行命令:

mvn clean package wagon:upload-single wagon:sshexec

package、wagon:upload-single、wagon:sshexec分别对项目进行打包、上传、运行command命令的操作。命令运行结束后,在服务器上查看进程ps -ef|grep test.jar,或者查看nohup.out文件,就可以看到Java程序在服务器上已经启动。

上传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>
                <fromFile>target/javawebdeploy.war</fromFile>
                <!-- 上传到Tomcat的webapps目录下 -->
                <url>scp://user:password@server/coder/tomcat/apache-tomcat-7.0.55/webapps</url>
                <commands>
                    <!-- 重启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>

完成以上配置后,同样可通过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>
                        <fromFile>${project.build.directory}/${project.build.finalName}.${project.packaging}</fromFile>
                        <url>scp://user:password@server/coder/tomcat/apache-tomcat-7.0.55/webapps</url>
                        <commands>
                            <command>
                                <![CDATA[
                                    export JAVA_HOME=/opt/jdk1.8.0_121
                                    export PATH=$JAVA_HOME/bin:$PATH
                                    export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
                                    cd /opt/mircroService/pcs && ./run.sh start
                                ]]>
                            </command>
                            <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>

配置完成后,即可使用mvn clean package来代替mvn clean package wagon:upload-single wagon:sshexec。

服务器独立配置

需要在Maven的配置文件settings.xml中配置好server的用户名和密码。

<server>  
    <id>webserver</id>  
    <username>hadoop</username>  
    <password>123</password>  
<!--
<configuration>
    diabled `The authenticity of host 'xxx' can't be established.Are you sure you want to continue connecting? (yes/no):`
    <strictHostKeyChecking>no</strictHostKeyChecking>
    or
    <knownHostsProvider implementation="org.apache.maven.wagon.providers.ssh.knownhost.NullKnownHostProvider">
        <hostKeyChecking>no</hostKeyChecking>
    </knownHostsProvider>
</configuration>
-->
</server>

然后configuration里面加上serverId,并且去掉url里面的用户名和密码。

<configuration>
    [...]
    <serverId>webserver</serverId>
    <url>scp://server</url>
    [...]
<configuration>

引用:

wagon-maven-plugin
wagon:upload-single
wagon:upload
wagon:download-single
wagon:download
wagon:list
wagon:copy
wagon:merge-maven-repos
wagon:sshexec
http://blog.csdn.net/mn960mn/article/details/49560003
http://xxgblog.com/2015/10/23/wagon-maven-plugin/