105 lines
3.4 KiB
Python
105 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Java Spring Boot 后端项目初始化模块
|
|
"""
|
|
|
|
|
|
def init_spring_project(backend_dir, project_name):
|
|
"""初始化 Spring Boot 项目"""
|
|
print("\n🚀 初始化 Spring Boot 项目...")
|
|
|
|
# 创建基本的 Spring Boot 项目结构
|
|
src_main = backend_dir / "src" / "main"
|
|
src_main_java = src_main / "java" / "com" / project_name.replace("-", "")
|
|
src_main_resources = src_main / "resources"
|
|
|
|
src_main_java.mkdir(parents=True, exist_ok=True)
|
|
src_main_resources.mkdir(parents=True, exist_ok=True)
|
|
|
|
# 创建主应用类
|
|
package_name = project_name.replace("-", "")
|
|
app_java = src_main_java / "Application.java"
|
|
app_java.write_text(f'''package com.{package_name};
|
|
|
|
import org.springframework.boot.SpringApplication;
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
@SpringBootApplication
|
|
@RestController
|
|
@CrossOrigin(origins = "*")
|
|
public class Application {{
|
|
|
|
public static void main(String[] args) {{
|
|
System.out.println("🚀 服务器启动: http://localhost:8080");
|
|
System.out.println("📍 健康检查: http://localhost:8080/api/health");
|
|
SpringApplication.run(Application.class, args);
|
|
}}
|
|
|
|
@GetMapping("/api/health")
|
|
public Map<String, String> health() {{
|
|
Map<String, String> response = new HashMap<>();
|
|
response.put("status", "ok");
|
|
response.put("message", "Welcome to {project_name} API");
|
|
return response;
|
|
}}
|
|
}}
|
|
''', encoding='utf-8')
|
|
|
|
# 创建 application.properties
|
|
app_properties = src_main_resources / "application.properties"
|
|
app_properties.write_text(f'''server.port=8080
|
|
spring.application.name={project_name}
|
|
''', encoding='utf-8')
|
|
|
|
# 创建 pom.xml
|
|
pom_xml = backend_dir / "pom.xml"
|
|
pom_xml.write_text(f'''<?xml version="1.0" encoding="UTF-8"?>
|
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
|
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
<modelVersion>4.0.0</modelVersion>
|
|
|
|
<parent>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-starter-parent</artifactId>
|
|
<version>3.2.0</version>
|
|
<relativePath/>
|
|
</parent>
|
|
|
|
<groupId>com.{package_name}</groupId>
|
|
<artifactId>{project_name}-backend</artifactId>
|
|
<version>0.0.1-SNAPSHOT</version>
|
|
<name>{project_name}</name>
|
|
|
|
<properties>
|
|
<java.version>17</java.version>
|
|
</properties>
|
|
|
|
<dependencies>
|
|
<dependency>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-starter-web</artifactId>
|
|
</dependency>
|
|
</dependencies>
|
|
|
|
<build>
|
|
<plugins>
|
|
<plugin>
|
|
<groupId>org.springframework.boot</groupId>
|
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
|
</plugin>
|
|
</plugins>
|
|
</build>
|
|
</project>
|
|
''', encoding='utf-8')
|
|
|
|
print("\n✅ Spring Boot 项目初始化成功")
|
|
print("💡 启动命令: mvn spring-boot:run")
|
|
print("⚠️ 注意: 需要安装 Maven 和 JDK 17+ 才能运行此项目")
|