create-spring-boot-java-project

create-spring-boot-java-project

熱門

建立 Spring Boot Java 專案骨架

3.6萬星標
4556分支
更新於 2026/7/13
SKILL.md
readonlyread-only
name
create-spring-boot-java-project
description

建立 Spring Boot Java 專案骨架

建立 Spring Boot Java 專案提示

檢查 Java 版本

  • 在終端機執行以下指令,檢查 Java 版本
java -version

下載 Spring Boot 專案範本

  • 在終端機執行以下指令,下載 Spring Boot 專案範本
curl https://start.spring.io/starter.zip \
  -d artifactId=${input:projectName:demo-java} \
  -d bootVersion=3.4.5 \
  -d dependencies=lombok,configuration-processor,web,data-jpa,postgresql,data-redis,data-mongodb,validation,cache,testcontainers \
  -d javaVersion=21 \
  -d packageName=com.example \
  -d packaging=jar \
  -d type=maven-project \
  -o starter.zip

解壓縮下載的檔案

  • 在終端機執行以下指令,解壓縮下載的檔案
unzip starter.zip -d ./${input:projectName:demo-java}

刪除下載的壓縮檔

  • 在終端機執行以下指令,刪除下載的壓縮檔
rm -f starter.zip

切換到專案根目錄

  • 在終端機執行以下指令,切換到專案根目錄
cd ${input:projectName:demo-java}

加入額外依賴

  • pom.xml 檔案中插入 springdoc-openapi-starter-webmvc-uiarchunit-junit5 依賴
<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  <version>2.8.6</version>
</dependency>
<dependency>
  <groupId>com.tngtech.archunit</groupId>
  <artifactId>archunit-junit5</artifactId>
  <version>1.2.1</version>
  <scope>test</scope>
</dependency>

加入 SpringDoc、Redis、JPA 和 MongoDB 設定

  • application.properties 檔案中插入 SpringDoc 設定
# SpringDoc 設定
springdoc.swagger-ui.doc-expansion=none
springdoc.swagger-ui.operations-sorter=alpha
springdoc.swagger-ui.tags-sorter=alpha
  • application.properties 檔案中插入 Redis 設定
# Redis 設定
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.password=rootroot
  • application.properties 檔案中插入 JPA 設定
# JPA 設定
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=rootroot
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
  • application.properties 檔案中插入 MongoDB 設定
# MongoDB 設定
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=root
spring.data.mongodb.password=rootroot
spring.data.mongodb.database=test

加入包含 Redis、PostgreSQL 和 MongoDB 服務的 docker-compose.yaml

  • 在專案根目錄建立 docker-compose.yaml,並加入以下服務:redis:6postgresql:17mongo:8

    • redis 服務應有
      • 密碼 rootroot
      • 對應埠號 6379 到 6379
      • 掛載磁碟區 ./redis_data/data
    • postgresql 服務應有
      • 密碼 rootroot
      • 對應埠號 5432 到 5432
      • 掛載磁碟區 ./postgres_data/var/lib/postgresql/data
    • mongo 服務應有
      • 初始化資料庫 root 使用者名稱 root
      • 初始化資料庫 root 密碼 rootroot
      • 對應埠號 27017 到 27017
      • 掛載磁碟區 ./mongo_data/data/db

加入 .gitignore 檔案

  • .gitignore 檔案中插入 redis_datapostgres_datamongo_data 目錄

執行 Maven 測試指令

  • 執行 maven clean test 指令,確認專案是否正常運作
./mvnw clean test

執行 Maven 執行指令(選擇性)

  • (選擇性)執行 docker-compose up -d 啟動服務,./mvnw spring-boot:run 執行 Spring Boot 專案,docker-compose rm -sf 停止服務。

讓我們逐步進行