One-Click Spring Boot Deployment on Windows with Batch Scripts
Learn how to create self-contained batch scripts that initialize a PostgreSQL database, import SQL, and launch a Spring Boot JAR on Windows, enabling one‑click service startup without manual environment configuration, and see example directory layout and customization tips.
Introduction
When deploying a Spring Boot JAR, you often need to configure runtime dependencies such as a database and JDK. This guide shows how to package all required resources into a set of batch scripts that provide a one‑click start experience on Windows.
Environment Preparation
JRE : version 1.8 (or any version matching your development JDK).
PostgreSQL : version 15 (MySQL can be used as an alternative).
The PostgreSQL resources are used directly as executable files; no installer is required.
Code Steps
Initialize and Start the Database
Place the PostgreSQL resource files in a folder (e.g., pgsql) and the initialization SQL scripts in a sub‑folder sql.
Use the following batch script (save as config_db.bat) to initialize the database, start the service, create the target database, and import the SQL file.
@echo off
setlocal
set PG_HOME=.\pgsql
set DB_NAME=fire_qixia
set DB_PWD=12345
set DB_PORT=5433
set SQL_FILE=.\sql\pgsql.sql
echo [1/4] 正在初始化 PostgreSQL...
if not exist "%PG_HOME%\data\PG_VERSION" (
echo 正在初始化 PostgreSQL 数据库...
"%PG_HOME%\bin\initdb.exe" -D "%PG_HOME%\data" -U postgres -E UTF8 --locale=C -W
if errorlevel 1 (
echo 数据库初始化失败!
exit /b 1
)
echo 数据库初始化成功!
)
echo [2/4] 正在启动 PostgreSQL...
start "" "%PG_HOME%\bin\pg_ctl.exe" -D "%PG_HOME%\data" -l "%PG_HOME%\pg.log" start -o "-p %DB_PORT%"
rem 等待几秒确保服务启动
ping -n 10 127.0.0.1 >nul
rem ==== 3. 创建数据库 ====
echo [3/4] 正在创建数据库 "%DB_NAME%" ...
"%PG_HOME%\bin\createdb.exe" -U postgres -p %DB_PORT% %DB_NAME%
if errorlevel 1 (
echo 数据库可能已存在,继续...
)
rem ==== 4. 导入 SQL 文件 ====
if exist "%SQL_FILE%" (
echo [4/4] 正在导入 SQL 文件 "%SQL_FILE%" ...
"%PG_HOME%\bin\psql.exe" -U postgres -p %DB_PORT% -d %DB_NAME% -f "%SQL_FILE%"
if errorlevel 1 (
echo SQL 导入失败!
exit /b 1
)
echo SQL 导入成功!
) else (
echo 未找到 SQL 文件:%SQL_FILE%
)
endlocalStart the Spring Boot JAR
Copy the JRE resources and the built Spring Boot JAR into the same folder and use the following script (e.g., start_service.bat) to launch the application.
@echo off
setlocal
set JAVA_HOME=.\jdk
set SPRING_APP=.\打包的程序.jar
echo 启动 Spring Boot 应用...
"%JAVA_HOME%\bin\java.exe" -jar "%SPRING_APP%"
endlocalDeploy by copying the entire directory to a Windows machine and double‑clicking the custom batch file.
Example Directory Layout
├── config_db.bat ----- 初始化并启动数据库的脚本
├── ext-config.yml ----- 额外的配置文件
├── jdk ----- JRE 运行环境
├── pgsql ----- 数据库资源文件
├── 程序.jar ----- 打包的 Spring Boot 程序
├── sql ----- SQL 脚本文件
├── start_db.bat ----- 启动数据库脚本
├── start_service.bat ----- 启动服务脚本FAQ
How to add extra configuration?
Append the following option to the Java command line:
--spring.config.location=classpath:application.yml,file:./ext-config.ymlCreate an ext-config.yml file in the same folder to override defaults.
I don’t want to re‑initialize the database each time.
Create a separate batch file that only starts the PostgreSQL service (and optionally a stop script). Example:
@echo off
setlocal
set PG_HOME=.\pgsql
set DB_PORT=5433
echo 正在启动 PostgreSQL...
start "" "%PG_HOME%\bin\pg_ctl.exe" -D "%PG_HOME%\data" -l "%PG_HOME%\pg.log" start -o "-p %DB_PORT%"
rem 等待几秒确保服务启动
ping -n 10 127.0.0.1 >nul
endlocalConclusion
By following these steps you can create a portable, zero‑configuration deployment package for a Spring Boot service on Windows. The approach works for users with little technical background and can be adapted to other environments or customized further as needed.
Dunmao Tech Hub
Sharing selected technical articles synced from CSDN. Follow us on CSDN: Dunmao.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
