前提:jenkins和springboot运行在同一台机器
springboot 通过winsw部署为服务
winsw 下载地址:https://github.com/kohsuke/winsw/releases
winsw的使用比较简单,下载上面两个文件,和springboot项目jar包一个文件夹下。
将exe和xml修改成同样的名字
xml文件内容如下:
<configuration> <!-- ID of the service. It should be unique accross the Windows system-->
<id>market-serverService</id>
<!-- Display name of the service -->
<name>market-serverService</name>
<!-- Service description -->
<description>贷款超市jar</description> <!-- Path to the executable, which should be started -->
<executable>java</executable> <!--Xmx256m 代表堆内存最大值为256MB -jar后面的是项目名-->
<arguments>-Xmx256m -jar market-server-1.0-SNAPSHOT.jar</arguments>
<!--日志模式-->
<logmode>rotate</logmode> </configuration>
打开命令行,注意需要管理员身份打开,否则会执行会报错
D:\java\market>marketService.exe stop
-- ::, INFO - Stopping the service with id 'market-serverService'
-- ::, FATAL - WMI Operation failure: AccessDenied
WMI.WmiException: AccessDenied
在 WMI.WmiRoot.BaseHandler.CheckError(ManagementBaseObject result)
在 WMI.WmiRoot.InstanceHandler.Invoke(Object proxy, MethodInfo method, Object[] args)
在 winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
在 winsw.WrapperService.Main(String[] args)
WMI.WmiException: AccessDenied
在 WMI.WmiRoot.BaseHandler.CheckError(ManagementBaseObject result)
在 WMI.WmiRoot.InstanceHandler.Invoke(Object proxy, MethodInfo method, Object[] args)
在 winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
在 winsw.WrapperService.Main(String[] args)
注册服务:
D:\java\market>marketService.exe install
-- ::, INFO - Installing the service with id 'market-serverService'
启动服务:
D:\java\market>marketService.exe start
-- ::, INFO - Starting the service with id 'market-serverService'
关闭服务:
D:\java\market>marketService.exe stop
-- ::, INFO - Stopping the service with id 'market-serverService'
卸载服务:
D:\java\market>marketService.exe uninstall
-- ::, INFO - Uninstalling the service with id 'market-serverService'
重启服务:
D:\java\market>marketService.exe restart
-- ::, INFO - Restarting the service with id 'market-serverService'
jenkins配置参考之前记录
新建一个maven项目,主要配置如下:
构建主要逻辑是:
1.git拉取代码到本地
2.maven编译打包
3.复制springboot项目jar包到winsw的目录下覆盖,重启服务
批处理命令:
copy /y .\target\market-server-1.0-SNAPSHOT.jar d:\java\market\
cd D:\java\market
market-server-1.0-SNAPSHOTService.exe restart