Windows下Jenkins自动启动jar包

本来以为是挺简单的一件事,没想到折腾了这么久,遇到各种坑,记录一下:

set BUILD_ID=dontkillmyass

set port=9035

for /f "tokens=1-5" %%i in ('netstat -ano ^|findstr ":%port%"') do (

echo kill the process %%m who use the port

taskkill /pid %%m -t -f

goto q

)

:q

start /B D:\JDK\jdk-11.0.2\bin\java.exe -jar -Dfile.encoding=UTF-8 D:\jenkins\Home\workspace\Mall\target\mall-0.0.1-SNAPSHOT.jar

powershell -Command "Start-Sleep -Seconds 80"

逐个解释一下各语句以及遇到的坑:

1、set BUILD_ID=dontkillmyass

参考jenkins Process leaked file descriptors-CSDN博客 这篇文档。我这里使用的是Execute Windows batch command这个工具,也遇到同样的问题,加上后就没事了。主要就是"Jenkins默认会在Build结束后Kill掉所有的衍生进程"这句话的描述,导致怎么都起不来jar包。

2、for /f "tokens=1-5" %%i in ('netstat -ano ^|findstr ":%port%"') do (

echo kill the process %%m who use the port

taskkill /pid %%m -t -f

goto q

)

:q

用goto写这么别扭(可能还会极端情况下有问题),主要是因为不跳出去的话剩下的循环就会报错,知道jenkins的任务判定为Failure。

3、start /B D:\JDK\jdk-11.0.2\bin\java.exe -jar -Dfile.encoding=UTF-8 D:\jenkins\Home\workspace\Mall\target\mall-0.0.1-SNAPSHOT.jar

要后台运行,只能用start;要把日志打印到console上,要加/B;其他的正常运行即可;

4、powershell -Command "Start-Sleep -Seconds 80"

最后一句,要想等它输出日志多一些看到欢迎logo,就加上时间"硬控",时髦点说就是"没空硬等"。。。

相关推荐
武藤一雄8 小时前
C# 关于多线程如何实现需要注意的问题(持续更新)
windows·后端·microsoft·c#·.net·.netcore·死锁
2501_9061505610 小时前
开源问卷平台DWSurvey开发配置记录
运维·开源·jenkins
coding消烦员11 小时前
在 Windows 内网搭建 Git 仓库:共享普通仓库 vs 中心 bare 仓库
windows·git
一点晖光13 小时前
jenkins 流水线脚本
运维·jenkins
num_killer15 小时前
小白的Jenkins学习
运维·python·学习·jenkins
xiaoliuliu1234515 小时前
IE8-WindowsXP-x86-CHS_23253_BDdl.exe 安装步骤(XP 32位简体中文版)
windows
百事牛科技16 小时前
文件不想再加密了?取消Word 打开密码的方法
windows·word
love530love17 小时前
EPGF 新手教程 13在 PyCharm(中文版 GUI)中创建 Hatch 项目环境,并把 Hatch 做成“项目自包含”(工具本地化为必做环节)
开发语言·ide·人工智能·windows·python·pycharm·hatch
峰上踏雪17 小时前
Go(Golang)Windows 环境配置关键点总结
开发语言·windows·golang·go语言
lusasky18 小时前
在Windows上编译、安装Rust
开发语言·windows·rust