springboot 以jar包方式在windows服务中运行的初始配置

1.在任务管理器中可以看到java运行的进程名

2.初始化winsw的名称及winsw.xml配置信息

 

@echo off

setlocal & pushd

set /p project_name=Jar名称(英文):

set /a sum=0

 set /a jarName=null

echo Jar名称:%project_name%

set filepath=%cd%

if not exist "%filepath%%project_name%_java.exe" (   
    goto copy 
) else ( 
    goto check_winsw 
)

:copy
    echo -----------------------------重命名java.exe----------------------------------
    echo 项目名称对应的java文件不存在
    echo 复制Java.exe 到当前文件夹并命名
    copy "%JAVA_HOME%binjava.exe" "%filepath%%project_name%_java.exe"
    if not exist "%filepath%%project_name%_java.exe" ( 
        echo 文件复制失败,JAVA_HOME环境变量不存在 
        echo --------------------------------------------------------------------------
        pause
        goto :eof
    ) else ( 
        echo --------------------------------------------------------------------------
        goto :check_winsw 
    )


:check_winsw
    echo -----------------------------检查winsw.exe----------------------------------
    if not exist "%filepath%%project_name%_win.exe" if not exist "%filepath%winsw.exe" ( 
        echo --------------------------------winsw 文件不存在------------------------------
        pause
        goto :eof
    )
    if not exist "%filepath%%project_name%_win.exe" ( 
        echo --------------------------------%project_name%_win.exe 文件不存在------------
        echo --------------------------------重命名winsw.exe 文件不存在------------
        ren "%filepath%winsw.exe" "%project_name%_win.exe"
        pause
        goto :check_winsw
    )
    goto :check_winsw_xml

:check_winsw_xml
    echo -----------------------------检查winsw.xml----------------------------------
    if not exist "%filepath%%project_name%_win.xml" if not exist "%filepath%winsw.xml" ( 
        echo --------------------------------winsw.xml 文件不存在------------------------------
        pause
        goto :eof
    )
    if not exist "%filepath%%project_name%_win.xml" ( 
        echo --------------------------------%project_name%_win.xml 文件不存在------------
        echo --------------------------------重命名winsw.xml 文件不存在------------
        ren "%filepath%winsw.xml" "%project_name%_win.xml"
        pause
        goto :check_winsw_xml
    )
    goto :renameJar

:renameJar
    for /r "%filepath%" %%a in (*.jar) do (
        SET "jarName=%%~nxa"
        echo %%~dpa%%~nxa
        echo %%~dpa
        echo %%~nxa
        set /a sum +=1
    )
    echo -------------------------------- 共%sum% 个jar------------------------------
    if %sum% NEQ 1 (
        echo --------------------------------Jar 包不存在或者数量大于1------------------------------
        pause
        goto :eof
    ) else (
        ren "%filepath%%jarName%" "%project_name%.jar"
        echo --------------------------------Jar 包重命名成功------------------------------
    )
    goto :configXml

:configXml
    echo -------------------------------------配置xml文件--------------------------------------------
    setlocal enabledelayedexpansion

    for /f "skip=2 tokens=1-4 delims=^<^>" %%i in ('find /i "id" %project_name%_win.xml') do (
        set id=%%k
        echo id当前值为!id!
    )

    for /f "skip=2 tokens=1-4 delims=^<^>" %%i in ('find /i "name" %project_name%_win.xml') do (
        set name=%%k
        echo name当前值为!name!
    )

    for /f "skip=2 tokens=1-4 delims=^<^>" %%i in ('find /i "description" %project_name%_win.xml') do (
        set description=%%k
        echo description当前值为!description!
    )

    for /f "skip=2 tokens=1-4 delims=^<^>" %%i in ('find /i "executable" %project_name%_win.xml') do (
        set executable=%%k
        echo executable当前值为!executable!
    )

    for /f "skip=2 tokens=1-4 delims=^<^>" %%i in ('find /i "arguments" %project_name%_win.xml') do (
        set arguments=%%k
        echo arguments当前值为!arguments!
    )

    pause
    chcp 65001
    for /f "tokens=* delims=" %%a in (%project_name%_win.xml) do (
        set "content=%%a"
        call set "content=%%content:<id>!id!</id>=<id>!project_name!</id>%%"
        call set "content=%%content:<name>!name!</name>=<name>!project_name!</name>%%"
        call set "content=%%content:<description>!description!</description>=<description>!project_name!_winsw</description>%%"
        call set "content=%%content:<executable>!executable!</executable>=<executable>!project_name!_java</executable>%%"
        call set "content=%%content:<arguments>!arguments!</arguments>=<arguments>-Dfile.encoding=utf8 -jar !project_name!.jar</arguments>%%"
        echo !content!>>result.xml
    )
    del %project_name%_win.xml
    ren result.xml %project_name%_win.xml
    chcp 936

endlocal & popd
pause

 

运行后输入项目名称,要求是英文,不要有空格
执行过程:

1.复制JAVA_HOME 中的java.exe到当前目录,并将名称修改成项目名称
2.检查winsw.exe信息,并进行命名
3.检查winsw.xml信息,并进行命名
4.判断当前目录jar包的数量,有且只允许有一个
5.重命名当前的jar文件
6.修改.xml文件中的信息

 

下面就是 winsw install 及winsw start

WINSW 的使用注意事项

1.WINSW 的xml 文件最好为ANSI编码(windows电脑上)

2.WINSW使用时最好重新命名下winsw.exe和winsw.xml。相同名称情况下可能会有冲突,我有两个没有改名字,第三个服务死活启动不了。