wagon-maven-plugin实现自动打包部署到服务器

https://dandelioncloud.cn/article/details/1489379549476671490

1.在maven中添加依赖

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

2.在pom的build节点添加wagon-ssh

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

3.在plugins下添加配置

<plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>wagon-maven-plugin</artifactId>
                <version>2.0.2</version>
                <executions>
                    <!-- ============ ============ ============ 测试 ============ ============ ============-->
                    <execution>
                        <id>devupload-one</id>
                        <!-- 运行package打包的同时运行upload-single和sshexec -->
                        <!--phase/阶段-->
                        <phase>package</phase>
                        <goals>
                            <goal>upload-single</goal>
                            <goal>sshexec</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                            <!-- 拷贝目录下(执行目录) target目录下的jar包 -->
                            <fromFile>target/lib/java-3.4.0.jar</fromFile>
                            <!-- 使用scp传输文件 指定服务端 用户名密码 ip 并指定目标文件夹-->
                            <url>scp://${user}:${password}@${ip}/home/soft/fultron/lib</url>
                            <!-- 命令列表 可以在传输完成后执行 -->
                            <commands>
                                <command>sh /home/soft/startsystem.sh restart</command>
                            </commands>
                            <!-- 显示运行命令的输出结果 -->
                            <displayCommandOutputs>true</displayCommandOutputs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>devupload-all</id>
                        <!-- 运行package打包的同时运行upload-single和sshexec -->
                        <phase>package</phase>
                        <goals>
                            <goal>upload</goal>
                            <goal>sshexec</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                            <!-- 拷贝目录下(执行目录) target目录下的jar包 -->
                            <fromDir>target/lib</fromDir>
                            <!-- 使用scp传输文件 指定服务端 用户名密码 ip 并指定目标文件夹-->
                            <url>scp://${user}:${password}@${ip}/home/soft/fultron/lib</url>
                            <commands>
                                <command>sh /home/soft/startsystem.sh restart</command>
                            </commands>
                        </configuration>
                    </execution>
                    <!-- ============ ============ ============ 生产 ============ ============ ============-->
                    <execution>
                        <id>produpload-one</id>
                        <!-- 运行package打包的同时运行upload-single和sshexec -->
                        <!--phase/阶段-->
                        <phase>package</phase>
                        <goals>
                            <goal>upload-single</goal>
                            <goal>sshexec</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                            <!-- 拷贝目录下(执行目录) target目录下的jar包 -->
                            <fromFile>target/lib/java-3.4.0.jar</fromFile>
                            <!-- 使用scp传输文件 指定服务端 用户名密码 ip 并指定目标文件夹-->
                            <url>scp://root:password@ip/data/soft/lib</url>
                            <!-- 命令列表 可以在传输完成后执行 -->
                            <commands>
                                <command>sh /data/soft/startsystem.sh restart</command>
                            </commands>
                            <!-- 显示运行命令的输出结果 -->
                            <displayCommandOutputs>true</displayCommandOutputs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>produpload-all</id>
                        <!-- 运行package打包的同时运行upload-single和sshexec -->
                        <phase>package</phase>
                        <goals>
                            <goal>upload</goal>
                            <goal>sshexec</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                            <!-- 拷贝目录下(执行目录) target目录下的jar包 -->
                            <fromDir>target/lib</fromDir>
                            <!-- 使用scp传输文件 指定服务端 用户名密码 ip 并指定目标文件夹-->
                            <url>scp://root:password@ip/data/soft/lib</url>
                            <commands>
                                <command>sh /data/soft/startsystem.sh restart</command>
                            </commands>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

4.说明

 <skip>true</skip> 来控制是否运行execution,true 表示不执行,false 表示执行

 <fromFile>target/lib/java-3.4.0.jar</fromFile>           本地目录,从哪上传,这里指定不是目录,是单个文件

 <fromDir>target/lib</fromDir>                                   本地目录,从哪上传,这里指定的是目录,不是单个文件

 <goal>upload</goal>  <goal>upload-single</goal> 表示上传,其中upload只能上传目录,upload-single只能上传单独的文件 

 <goal>sshexec</goal>                                             表示执行 服务器命令

 <excludes>logback-spring.xml</excludes>-->         过滤你不想要的文件

 <toDir>/home/hdkg/ATC/temp</toDir>                    如果没有toDir,url要写完整路

 <url>scp://root:password@ip/data/soft/lib</url>      服务器IP地址

 <displayCommandOutputs>true</displayCommandOutputs>     是否显示Shell命令

5.打包

打包命令 mvn clean install

如果没有以下配置

               <goals>
                            <goal>upload</goal>
                            <goal>sshexec</goal>
                        </goals>

则需要按配置改成下面

mvn wagon:upload-single -f upload.xml 这个是上传单独文件。例如jar包这样一类的

mvn wagon:upload -f upload.xml 这个是上传多文件,可以整个目录

6.参考

官网参考地址 http://www.mojohaus.org/wagon-maven-plugin/upload-single-mojo.html

主要提供如下几个goal

wagon:upload-single uploads the specified file to a remote location.

wagon:upload uploads the specified set of files to a remote location.

wagon:download-single downloads the specified file from a remote location.

wagon:download downloads the specified set of files from a remote location.

wagon:list lists the content of a specified location in a remote repository.

wagon:copy copies a set of files under a Wagon repository to another.

wagon:merge-maven-repos merges , including metadata, a Maven repository to another.

wagon:sshexec Executes a set of commands at remote SSH host.

posted @ 2022-09-28 10:51  韩憨  阅读(340)  评论(0编辑  收藏  举报
//看板娘