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

相关文章:

  • Linux面试题集合(1)
  • C语言输入函数对比解析
  • 线性回归策略
  • STM32实战指南:SG90舵机控制原理与代码详解
  • javaDoc
  • 基于大疆Mini 3无人机和指定软件工具链的完整3D建模工作
  • STM32IIC协议基础及Cube配置
  • 小刚说C语言刷题—1230蝴蝶结
  • 虚拟主播肖像权保护,数字时代的法律博弈
  • 5.27本日总结
  • BiliTools v1.3.7 哔哩哔哩工具箱
  • QT+EtherCAT 主站协议库—SOEM主站
  • 机械元件杂散光难以把控?OAS 软件案例深度解析
  • 使用Python绘制Lorenz奇异吸引子轨道
  • 探秘 Java 字节缓冲流:解锁高效 IO 操作的进阶之路
  • Web安全基础:深度解析与实战指南
  • [STM32] 5-1 时钟树(上)
  • FastAPI简介
  • 【Python】魔法方法是真的魔法! (第二期)
  • pnpm 与 npm 的核心区别
  • 上海将建设万兆小区、园区及工厂,为模型训练数据的传输提供硬件支持
  • 试点首发进口消费品检验便利化措施,上海海关与上海商务委发文
  • 知名猎头公司创始人兼首席执行官庄华因突发疾病逝世,享年62岁
  • MSCI中国指数5月调整:新增5只A股、1只港股
  • 外企聊营商|特雷通集团:税务服务“及时雨”
  • “一码难求”的Manus开放注册但价格不菲,智能体距离“实用”还有多远