Files
java21-template/init.sh
T
Bruno GaoandQwen-Coder eefe2c5b34 M2: Add complete Java21 template with Clean Architecture implementation
- Add four-layer modular structure (domain, application, infrastructure, api)
- Implement complete CRUD example with Order entity
- Add ArchUnit architecture compliance tests
- Add Maven configuration with proper dependency management
- Add Dockerfile for containerization
- Add Kustomize deployment templates
- Add initialization script (init.sh)
- Add CI/CD workflow configuration
- Add comprehensive documentation and configuration files

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-02-11 23:54:21 +08:00

636 lines
11 KiB
Bash
Executable File

#!/bin/bash
# Java21 Template 初始化脚本
# 用于将模板仓库初始化为具体的服务仓库
set -e
echo "Java21 Template 初始化脚本"
echo "=========================="
# 默认值
DEFAULT_COMPANY="com.example"
DEFAULT_SERVICE_NAME="my-service"
DEFAULT_GROUP="com.example.my-service"
# 显示帮助信息
show_help() {
echo "用法: $0 --name <service-name> --group <group-id> --org <organization>"
echo ""
echo "选项:"
echo " --name <service-name> 服务名称 (默认: my-service)"
echo " --group <group-id> Maven groupId (默认: com.example.my-service)"
echo " --org <organization> 组织名称 (默认: example)"
echo " --harbor <harbor-url> Harbor 仓库地址 (默认: harbor.example.com)"
echo " --help 显示此帮助信息"
echo ""
echo "示例:"
echo " $0 --name order-service --group com.company.order --org myteam"
}
# 解析命令行参数
SERVICE_NAME="$DEFAULT_SERVICE_NAME"
GROUP_ID="$DEFAULT_GROUP"
ORGANIZATION="example"
HARBOR_URL="harbor.example.com"
while [[ $# -gt 0 ]]; do
case $1 in
--name)
SERVICE_NAME="$2"
shift 2
;;
--group)
GROUP_ID="$2"
shift 2
;;
--org)
ORGANIZATION="$2"
shift 2
;;
--harbor)
HARBOR_URL="$2"
shift 2
;;
--help)
show_help
exit 0
;;
*)
echo "未知参数: $1"
show_help
exit 1
;;
esac
done
# 验证必要参数
if [[ -z "$SERVICE_NAME" || -z "$GROUP_ID" ]]; then
echo "错误: 服务名称和服务组ID是必需的"
show_help
exit 1
fi
echo "参数确认:"
echo " 服务名称: $SERVICE_NAME"
echo " Group ID: $GROUP_ID"
echo " 组织: $ORGANIZATION"
echo " Harbor URL: $HARBOR_URL"
echo ""
read -p "确认继续? [y/N]: " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "操作已取消"
exit 1
fi
# 提取公司和项目名称
COMPANY=$(echo "$GROUP_ID" | cut -d. -f1,2 | tr '.' '/')
PROJECT=$(echo "$GROUP_ID" | cut -d. -f3-)
echo "开始初始化..."
# 1. 重命名模块目录
echo "1. 重命名模块目录..."
for dir in tmpl-domain tmpl-application tmpl-infrastructure tmpl-api; do
if [[ -d "$dir" ]]; then
NEW_DIR_NAME="${SERVICE_NAME}-${dir#tmpl}"
echo " 重命名 $dir -> $NEW_DIR_NAME"
mv "$dir" "$NEW_DIR_NAME"
fi
done
# 2. 替换所有文件中的占位符
echo "2. 替换占位符..."
# 查找所有需要替换的文件
FILES_TO_UPDATE=$(find . -type f \( -name "*.java" -o -name "*.xml" -o -name "*.yml" -o -name "*.yaml" -o -name "*.md" -o -name "*.txt" -o -name "Dockerfile" \) -not -path "./target/*" -not -path "./.git/*")
for file in $FILES_TO_UPDATE; do
if [[ -f "$file" ]]; then
# 替换占位符
sed -i.bak "s/com\.example\.tmpl/$GROUP_ID/g" "$file" 2>/dev/null || true
sed -i.bak "s/tmpl-service/$SERVICE_NAME/g" "$file" 2>/dev/null || true
sed -i.bak "s/tmpl-domain/${SERVICE_NAME}-domain/g" "$file" 2>/dev/null || true
sed -i.bak "s/tmpl-application/${SERVICE_NAME}-application/g" "$file" 2>/dev/null || true
sed -i.bak "s/tmpl-infrastructure/${SERVICE_NAME}-infrastructure/g" "$file" 2>/dev/null || true
sed -i.bak "s/tmpl-api/${SERVICE_NAME}-api/g" "$file" 2>/dev/null || true
sed -i.bak "s/example\.tmpl/$PROJECT/g" "$file" 2>/dev/null || true
sed -i.bak "s/harbor\.example\.com/$HARBOR_URL/g" "$file" 2>/dev/null || true
# 删除备份文件
rm -f "$file.bak"
fi
done
# 3. 更新 pom.xml 文件中的模块名称
echo "3. 更新 pom.xml 模块名称..."
if [[ -f "pom.xml" ]]; then
sed -i.bak "s/tmpl-domain/${SERVICE_NAME}-domain/g" pom.xml
sed -i.bak "s/tmpl-application/${SERVICE_NAME}-application/g" pom.xml
sed -i.bak "s/tmpl-infrastructure/${SERVICE_NAME}-infrastructure/g" pom.xml
sed -i.bak "s/tmpl-api/${SERVICE_NAME}-api/g" pom.xml
rm -f pom.xml.bak
fi
# 4. 更新 Dockerfile
echo "4. 更新 Dockerfile..."
if [[ -f "Dockerfile" ]]; then
sed -i.bak "s/tmpl-api-.*\.jar/${SERVICE_NAME}-api-\*\.jar/g" Dockerfile
rm -f Dockerfile.bak
fi
# 5. 创建 .gitignore 如果不存在
echo "5. 确保 .gitignore 存在..."
if [[ ! -f ".gitignore" ]]; then
cat > .gitignore << 'EOF'
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
.mwj.tmp/
.mch.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
# Maven
target/
!target/README
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# Maven wrapper
.wrapper/downloads/
.wrapper/gradle/
# Compilation node.js
Gruntfile.js
.npm/
# Coverage directory used by tools like istanbul
coverage/
*.lcov
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Istanbul output
/index.html
/coverage
# JSHint
jshint-report.xml
# JSLint
jslint.xml
# Mocha
mochawesome-report/
# Rome
_dist/
# RequireJS build files
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# JET required cache (.cache directories)
.cache
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
out/
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# See https://www.gatsbyjs.org/docs/global-css/#adding-global-styles-as-a-css-file
public/.cache/
public/static/
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
# Java
*.iml
*.ipr
*.iws
.idea/
# Target directory for Maven builds
target/
# Environment variable files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# Docker
.docker/
# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# IDE
.vs/
.vscode/
*.swp
*.swo
*~
# Logs
logs
*.log
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Various things for Meteor
.meteor/local/
# Git book
_book
# s2i
.s2i/
# Eclipse
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
# Eclipse Core
.binary
# External API docs
.externalToolBuilders/
.externalNativeBuild/
# Gradle
.gradle
.gradle/
!gradle/wrapper/gradle-wrapper.jar
# Leiningen
.classpath
.libs/
.log
.lein-deps-sum
.lein-native-sum
.lein-plugins/
.lein-repl-history
.revlibs
.nrepl-port
# sbt
target/
lib_managed/
src_managed/
project_target/
scalaboot.properties
# Lift web
.lift.properties
# Lein cljs
.cljscache/
# Play Framework
lib_managed/
project/boot/
target/
tmp/
template Masquerade/
# Simple Build Tool
project/build/
project/plugins/
# Scala
*.class
*.jar
*.war
*.ear
.scala_dependencies
.worksheet
.generated
.target
# sbteclipse
.sbtconfig
.sbtserver
# IntelliJ IDEA
*.iml
*.ipr
*.iws
.idea/
# NetBeans
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
# VS Code
.vscode/
# Mac
.DS_Store
.AppleDouble
.LSOverride
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# Windows
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# End
EOF
fi
# 6. 创建 README.md
echo "6. 创建 README.md..."
cat > README.md << EOF
# $SERVICE_NAME
基于 Java 21 和 Clean Architecture 的微服务模板。
## 项目结构
- \`${SERVICE_NAME}-domain\`: 领域层
- \`${SERVICE_NAME}-application\`: 应用层
- \`${SERVICE_NAME}-infrastructure\`: 基础设施层
- \`${SERVICE_NAME}-api\`: 接入层
## 快速开始
### 环境要求
- Java 21
- Maven 3.6+
- Docker (可选)
### 本地运行
\`\`\`bash
# 编译
mvn clean compile
# 运行测试
mvn test
# 打包
mvn package
# 运行应用
java -jar ${SERVICE_NAME}-api/target/${SERVICE_NAME}-api-*.jar
\`\`\`
### API 文档
访问 http://localhost:8080/swagger-ui.html 查看 API 文档。
## 配置
应用支持以下环境变量:
- \`SPRING_DATASOURCE_URL\`: 数据库连接URL
- \`SPRING_DATASOURCE_USERNAME\`: 数据库用户名
- \`SPRING_DATASOURCE_PASSWORD\`: 数据库密码
- \`REDIS_HOST\`: Redis主机地址
- \`REDIS_PORT\`: Redis端口
## 构建 Docker 镜像
\`\`\`bash
docker build -t $HARBOR_URL/$ORGANIZATION/$SERVICE_NAME:\$(git rev-parse --short HEAD) .
\`\`\`
## 许可证
MIT License
EOF
# 7. 创建 .editorconfig
echo "7. 创建 .editorconfig..."
cat > .editorconfig << 'EOF'
# EditorConfig is awesome: http://EditorConfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4
[*.md]
trim_trailing_whitespace = false
[*.yml]
indent_size = 2
[*.yaml]
indent_size = 2
EOF
# 8. 完成
echo ""
echo "✅ 初始化完成!"
echo ""
echo "接下来的步骤:"
echo "1. 检查生成的文件是否正确"
echo "2. 运行 mvn clean compile 验证编译"
echo "3. 运行 mvn test 验证测试"
echo "4. 根据需要进一步定制代码"
echo ""
echo "服务名称: $SERVICE_NAME"
echo "Group ID: $GROUP_ID"
echo "项目路径: $(pwd)"