winsw打包jar


參考:

https://www.jianshu.com/p/fc9e4ea61e13

https://blog.csdn.net/qq_28566071/article/details/80882503

spring官方推薦使用winsw來將springboot項目作為服務運行,參考https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#deployment-windows

1.把java程序打包為jar包

2.下載winsw

winsw是一款可將可執行程序安裝成Windows Service的開源工具,github:https://github.com/kohsuke/winsw/releases

前言
由於我們公司java應用是部署在windows服務器上的,jenkins在部署spring boot時會一直等待直到超時才結束,這個主要原因是因為spring boot 啟動一直是在前台運行(控制台一直在打印),所以jenkins會認為這個任務沒有執行完成,就會一直在執行。如果是linux系統可以使用nohup命令,但windows就不行,沒這個命令,所以我把java 應用做成windows服務,每次部署成功只要重啟服務即可。

winsw制作windows服務器
winsw(github地址源碼地址,exe下載地址,官方英文安裝文檔)是一個開源的專門用於制作windows服務的小工具,我們可以利用它來講java程序做成windows服務。
在下載地址里下載最新版winsw。
准備三樣東西

xxx.jar
xx.xml
xxx.exe
以MyApp.jar為例,注意xml和exe名字要保持一致。新建一個project文件夾

創建MyApp.xml文件
<service>
<id>MyApp</id>
<name>MyApp</name>
<description>This runs Spring Boot as a Service.</description>
<env name="MYAPP_HOME" value="%BASE%"/>
<executable>java</executable>
<arguments>-Xmx256m -jar "%BASE%\MyApp.jar"</arguments>
<logmode>rotate</logmode>
</service>

上面的MYAPP_HOME是系統環境變量,我們需要像設置JAVA-HOME一樣設置。地址指向jar文件地址,當然如果我們不使用環境變量的方式,那就直接在xml里指定jar的目錄地址。我就是這樣的方式。所以我的MyApp.xml文件為

<configuration>

<!-- ID of the service. It should be unique accross the Windows system-->
<id>OSCAR</id>
<!-- Display name of the service -->
<name>OSCAR(powered by WinSW)</name>
<!-- Service description -->
<description>這是winsw生成的服務</description>

<!-- Path to the executable, which should be started -->
<executable>java</executable>
<arguments>-jar mingpeng-0.0.1-SNAPSHOT.jar</arguments>
<!-- 開機啟動 -->
<startmode>Automatic</startmode>

<!-- 日志配置 -->
<logpath>logs/service</logpath>

<logmode>rotate</logmode>

</configuration>

arguments是java命令后的一些參數,是自定義的。
id和name就是注冊到windows服務的名字,建議和文件名保持一致,這里就是MyApp

創建MyApp.exe
其實不是重新創建而是重命名,將下載的winsw.exe重命名為MyApp.exe。注意名字要和MyApp.xml保持一致
官方這么說的:you have to rename the winsw.exe to MyApp.exe so that its name matches with the MyApp.xml,注意是必需

創建MyApp.jar
將准備好的jar重命名為MyApp.jar,當然,這個可以不是MyApp,也可以是其它的。比如MyApp.xml里的配置是這樣的

<arguments>-Xmx256m -jar "myApp.jar" --spring.profiles.active=stage --spring.datasource.dbname=donglitestdb --server.port=9982</arguments>
1
是小寫。

安裝服務
至些就制作完成,就可以使用install/uninstall命令了
安裝服務:MyApp.exe install
卸載服務:MyApp.exe uninstall
檢查狀態:MyApp.exe status
啟動服務:MyApp.exe start或net start MyApp
停止服務:MyApp.exe stop或net stop MyApp
重啟服務:MyApp.exe restart或
windows下沒有重啟服務,我們可以先stop服務再start服務。批處理如下
restart.bat

net stop %1
net start %1
1
2
重啟服務:restart 服務名

問題
如果報“WMI Operation failure: AccessDenied”那就創建一個批處理文件,然后以管理員權限運行,
比如如下installDongLi.bat:

D:\tmp\dongliService\DongLi.exe install
pause
1
2
測試
按如下方式測試結果如下

終於成功了

winsw做包裝其它程序
winsw也可以包裝其它exe文件,或可啟動文件。做成windows服務。以nginx為例
將winsw.exe改名為NginxService.exe
創建NginxService.xml如下

<service>
<id>NginxService</id>
<name>NginxService</name>
<description>This runs Spring Boot as a Service.</description>
<executable>D:\soft\nginx\nginx-1.15.2\nginx.exe</executable>
</service>

將上面文件放到nginx安裝文件包下
創建installService.bat批處理文件

D:\soft\nginx\nginx-1.15.2\NginxService.exe install
配置完成后,命令行進入winsw所在的文件夾,執行“spider-1.0.exe install”,就注冊服務了。

如果不再需要這個服務,使用spider-1.0.exe uninstall即可卸載服務。start啟動和stop關閉服務


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM