一个基于FastAPI的Web服务,通过克隆Git仓库并分析其中的pom.xml文件来扫描Maven项目的依赖关系。
- 从Git仓库URL克隆项目
- 解析Maven
pom.xml文件 - 提取项目元数据和依赖信息
- RESTful API,返回结构化JSON响应
- 完善的错误处理和验证
- 自动清理临时文件
- 支持多模块项目
- Python 3.7 或更高版本
- Git(必须在系统PATH中可用)
- 互联网连接(用于克隆仓库)
# 克隆项目
git clone <项目地址>
cd scan-maven
# 运行配置脚本
chmod +x setup_env.sh
./setup_env.sh# 克隆项目
git clone <项目地址>
cd scan-maven
# 运行配置脚本
setup_env.bat# 创建虚拟环境
python3 -m venv venv
# 激活虚拟环境
# Linux/MacOS:
source venv/bin/activate
# Windows:
venv\Scripts\activate
# 安装依赖
pip install -r requirements.txtpython main.pyuvicorn main:app --host 0.0.0.0 --port 8000 --reload扫描Maven项目依赖:
curl -X POST "http://localhost:8000/scan" \
-H "Content-Type: application/json" \
-d '{"url": "https://github.com/spring-projects/spring-boot.git"}'健康检查:
curl http://localhost:8000/health扫描Maven项目依赖
请求格式:
{
"url": "https://github.com/spring-projects/spring-boot.git"
}响应格式:
{
"project_name": "spring-boot",
"group_id": "org.springframework.boot",
"artifact_id": "spring-boot",
"version": "3.2.0-SNAPSHOT",
"dependencies": [
{
"group_id": "org.springframework",
"artifact_id": "spring-core",
"version": "6.1.0",
"scope": "compile",
"type": null
}
],
"total_dependencies": 150
}错误响应:
{
"detail": "错误描述信息"
}健康检查端点
响应:
{
"status": "healthy",
"service": "maven-dependency-scanner"
}使用提供的测试脚本:
# 确保服务已启动
python test_api.py服务启动后,可以访问:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
scan-maven/
├── main.py # FastAPI主应用
├── requirements.txt # Python依赖
├── setup_env.sh # Linux/MacOS环境配置脚本
├── setup_env.bat # Windows环境配置脚本
├── start.sh # Linux/MacOS启动脚本
├── start.bat # Windows启动脚本
├── test_api.py # 测试脚本
└── README.md # 中文文档
API会处理以下错误情况:
- 无效的URL格式
- 仓库中找不到pom.xml文件
- XML格式错误
- Git克隆失败
- 网络超时
- 权限问题
- 使用Python类型注解
- 遵循PEP 8编码规范
- 添加适当的错误处理
- 编写清晰的文档字符串
A: 目前只支持公开仓库。私有仓库需要添加认证支持。
A: 建议单个项目不超过500MB,克隆超时时间为5分钟。
A: 当前版本会扫描所有pom.xml文件,但只返回主项目的依赖。
A: 支持所有使用标准pom.xml格式的Maven项目。
MIT License