使用Maven和Ant上传文件到Linux服务器

使用Maven和Ant上传文件到Linux服务器

在Maven项目中结合Ant任务上传文件到Linux服务器可以通过以下几种方式实现:

1. 使用maven-antrun-plugin的SCP任务

这是最常见的方式,需要配置maven-antrun-plugin并使用Ant的SCP任务:

xml 复制代码
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>3.1.0</version>
            <dependencies>
                <!-- 添加JSch依赖以支持SCP -->
                <dependency>
                    <groupId>com.jcraft</groupId>
                    <artifactId>jsch</artifactId>
                    <version>0.1.55</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>scp-upload</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <!-- 定义SCP任务 -->
                            <taskdef name="scp" classname="org.apache.tools.ant.taskdefs.optional.ssh.Scp"
                                     classpathref="maven.plugin.classpath"/>
                            
                            <!-- 执行文件上传 -->
                            <scp file="${project.build.directory}/${project.build.finalName}.jar"
                                 todir="${server.username}:${server.password}@${server.host}:${server.deploy.path}"
                                 trust="true" verbose="true"/>
                                 
                            <!-- 或者上传整个目录 -->
                            <scp todir="${server.username}:${server.password}@${server.host}:${server.deploy.path}" 
                                 trust="true" verbose="true">
                                <fileset dir="${project.build.directory}/dist"/>
                            </scp>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. 使用SSHExec执行命令

如果需要在上传后执行命令(如重启服务),可以使用SSHExec任务:

xml 复制代码
<target>
    <taskdef name="sshexec" classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
             classpathref="maven.plugin.classpath"/>
    
    <sshexec host="${server.host}"
             username="${server.username}"
             password="${server.password}"
             command="mkdir -p ${server.deploy.path}"
             trust="true"/>
</target>

3. 配置服务器信息

建议在Maven的settings.xml或pom.properties中配置服务器信息:

xml 复制代码
<properties>
    <server.host>your.server.com</server.host>
    <server.port>22</server.port>
    <server.username>deployuser</server.username>
    <server.password>deploypass</server.password>
    <server.deploy.path>/opt/app/deploy</server.deploy.path>
</properties>

更安全的做法是在settings.xml中配置:

xml 复制代码
<servers>
    <server>
        <id>deployment-server</id>
        <username>deployuser</username>
        <password>deploypass</password>
    </server>
</servers>

然后在pom.xml中引用:

xml 复制代码
<properties>
    <server.host>your.server.com</server.host>
    <server.deploy.path>/opt/app/deploy</server.deploy.path>
</properties>

4. 使用密钥认证(推荐)

更安全的方式是使用SSH密钥认证而非密码:

xml 复制代码
<target>
    <scp file="${project.build.directory}/${project.build.finalName}.jar"
         todir="${server.username}@${server.host}:${server.deploy.path}"
         keyfile="${user.home}/.ssh/id_rsa"
         passphrase=""
         trust="true"/>
</target>

5. 完整示例

xml 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.1.0</version>
    <dependencies>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.55</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>deploy-to-server</id>
            <phase>deploy</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <!-- 定义SSH任务 -->
                    <taskdef name="scp" classname="org.apache.tools.ant.taskdefs.optional.ssh.Scp"
                             classpathref="maven.plugin.classpath"/>
                    <taskdef name="sshexec" classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
                             classpathref="maven.plugin.classpath"/>
                    
                    <!-- 1. 创建远程目录 -->
                    <sshexec host="${server.host}"
                             username="${server.username}"
                             keyfile="${user.home}/.ssh/id_rsa"
                             passphrase=""
                             command="mkdir -p ${server.deploy.path}"
                             trust="true"/>
                    
                    <!-- 2. 上传文件 -->
                    <scp file="${project.build.directory}/${project.build.finalName}.jar"
                         todir="${server.username}@${server.host}:${server.deploy.path}"
                         keyfile="${user.home}/.ssh/id_rsa"
                         passphrase=""
                         trust="true"/>
                    
                    <!-- 3. 设置权限 -->
                    <sshexec host="${server.host}"
                             username="${server.username}"
                             keyfile="${user.home}/.ssh/id_rsa"
                             passphrase=""
                             command="chmod 755 ${server.deploy.path}/${project.build.finalName}.jar"
                             trust="true"/>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

注意事项

  1. 安全性:不要在pom.xml中硬编码密码,建议使用Maven的settings.xml或环境变量
  2. 依赖:确保添加了JSch依赖以支持SCP/SSH功能
  3. 网络:确保构建服务器可以访问目标Linux服务器
  4. 权限:确保SSH用户有目标目录的写入权限
  5. 防火墙:确保目标服务器的SSH端口(默认22)开放
  6. 日志:添加verbose="true"有助于调试上传问题

替代方案

如果不想使用Ant任务,也可以考虑:

  1. wagon-maven-plugin:Maven官方的传输插件
  2. cargo-maven-plugin:专门用于部署的插件
  3. exec-maven-plugin:直接调用rsync/scp命令

以上方法都可以实现将Maven构建产物上传到Linux服务器的需求,选择最适合你项目的方式即可。

相关推荐
JJJennie77714 分钟前
MAI Gateway技术揭秘:大模型网关能做什么?从原理到落地
linux·服务器·网络·ai网关
风哥2号2 小时前
数据库教程FGMT02‑生产环境Linux+Oracle19c安装配置与项目实战
linux·数据库·ffmpeg
wuminyu6 小时前
Kafka中sendfile与mmap实现机制解析
java·linux·c语言·jvm·c++
新时代牛马6 小时前
嵌入式网络完整篇:从LwIP/以太网驱动到 Linux netdev 与排障
linux·网络·php
猿与禅6 小时前
Linux(Ubuntu)入门到实战:系统管理、常用命令与权限体系完整指南
linux·ubuntu·apt·进程管理·系统运维·用户权限·vi/vim
前端世界7 小时前
Linux服务器实战:NTP时间同步、SELinux权限与rsyslog日志管理,一次搞懂三大运维问题
linux·运维·服务器
Android系统攻城狮7 小时前
Linux Gstreamer深度解析之gst_audio_converter_new调用流程与实战(二十)
linux·运维·服务器·gstreamer音视频·音视频进阶
captain3767 小时前
▲网络原理(2)-TCP
java·服务器·网络·tcp/ip·java-ee
言乐68 小时前
Python加速器4跨境网络加速器
运维·服务器·开发语言·网络·python
xinjia_ctrl10 小时前
暑假实习总结
git·后端·spring·maven·intellij-idea