当前位置: 首页 > news >正文

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

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

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

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

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

<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任务:

<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中配置服务器信息:

<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中配置:

<servers><server><id>deployment-server</id><username>deployuser</username><password>deploypass</password></server>
</servers>

然后在pom.xml中引用:

<properties><server.host>your.server.com</server.host><server.deploy.path>/opt/app/deploy</server.deploy.path>
</properties>

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

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

<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. 完整示例

<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服务器的需求,选择最适合你项目的方式即可。

http://www.xdnf.cn/news/502903.html

相关文章:

  • 交流学习 | 江西同为科技有限公司赴海尔总部考察交流
  • Vue3学习(组合式API——父、子组件间通信详解)
  • 大模型之RAG知识库
  • 实验三:计划任务和时钟同步
  • 经典算法 求C(N, K) % mod,保证mod是质数
  • 打造文本差异对比工具 TextDiffX:从想法到实现的完整过程
  • 嵌入式软件的分层架构
  • GitHub 趋势日报 (2025年05月16日)
  • H3C UIS 超融合管理平台原理解读以及日常运维实操与故障处理
  • Transformer 架构在目标检测中的应用:YOLO 系列模型解析
  • 便捷的批量打印工具推荐
  • PyQt5基本窗口控件(QSlider(滑动条))
  • 【计网】 ARP地址解析协议 [工作过程]
  • hyper-v 虚拟机怎么克隆一台一样的虚拟机?
  • NHANES指标推荐:FMI
  • 【Linux笔记】——Linux线程控制创建、终止与等待|动态库与内核联动
  • 软件测试的常用的面试题【带答案】
  • 【汇总】影视仓接口地址,影视仓最新配置接口【2025.5】
  • 常见图算法解析:TSP问题、最大团/独立集问题、图着色问题、哈密尔顿回路问题、顶点覆盖问题和最长路径问题
  • Ocean: Object-aware Anchor-free Tracking
  • 中级网络工程师知识点4
  • 【文本切割器】RecursiveCharacterTextSplitter参数设置优化指南
  • ORACLE RAC环境REDO日志量突然增加的分析
  • 【以及好久没上号的闲聊】Unity记录8.1-地图-重构与优化
  • SQL Server 常用函数
  • QT使用QXlsx读取excel表格中的图片
  • 【自然语言处理与大模型】大模型(LLM)基础知识④
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(23):受身形
  • mAP、AP50、AR50:目标检测中的核心评价指标解析
  • 开源项目实战学习之YOLO11:12.2 ultralytics-models-sam-decoders.py源码分析