You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
slms/scripts/run_sonar.bat

192 lines
4.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

@echo off
chcp 65001 >nul
echo ========================================
echo SLMS SonarQube 代码质量分析
echo ========================================
echo.
REM 配置 SonarQube 服务器信息(已安装并运行)
set SONAR_HOST_URL=http://localhost:9000
set SONAR_PROJECT_KEY=slms:smart-library-management-system
set SONAR_TOKEN=
REM 检查是否配置了 Token
if "%SONAR_TOKEN%"=="" (
echo 警告: 未配置 SonarQube Token
echo.
echo 请按以下步骤配置:
echo 1. 访问 %SONAR_HOST_URL%
echo 2. 登录(默认: admin/admin
echo 3. 进入 My Account ^> Security
echo 4. 生成 Token
echo 5. 将 Token 填入本脚本的 SONAR_TOKEN 变量
echo.
set /p SONAR_TOKEN=请输入 SonarQube Token或按回车跳过:
echo.
)
echo 选择分析方式:
echo 1. 快速分析(仅编译和分析)
echo 2. 完整分析(清理、编译、测试、分析)
echo 3. 仅查看配置
echo 4. 启动 SonarQube 服务器Docker
echo 5. 打开 SonarQube 仪表板
echo 0. 退出
echo.
set /p choice=请输入选项 (0-5):
if "%choice%"=="1" goto quick
if "%choice%"=="2" goto full
if "%choice%"=="3" goto config
if "%choice%"=="4" goto start_server
if "%choice%"=="5" goto dashboard
if "%choice%"=="0" goto end
goto end
:quick
echo.
echo ========================================
echo 快速分析
echo ========================================
echo.
echo [1/2] 编译项目...
call mvn clean compile -q
if %errorlevel% neq 0 (
echo ✗ 编译失败!
pause
exit /b 1
)
echo ✓ 编译成功
echo.
echo [2/2] 运行 SonarQube 分析...
if "%SONAR_TOKEN%"=="" (
call mvn sonar:sonar -Dsonar.host.url=%SONAR_HOST_URL%
) else (
call mvn sonar:sonar -Dsonar.host.url=%SONAR_HOST_URL% -Dsonar.login=%SONAR_TOKEN%
)
if %errorlevel% neq 0 (
echo ✗ 分析失败!
echo.
echo 可能的原因:
echo 1. SonarQube 服务器未启动
echo 2. Token 不正确
echo 3. 网络连接问题
pause
exit /b 1
)
echo ✓ 分析完成
goto success
:full
echo.
echo ========================================
echo 完整分析
echo ========================================
echo.
echo [1/4] 清理项目...
call mvn clean -q
echo ✓ 清理完成
echo.
echo [2/4] 编译项目...
call mvn compile -q
if %errorlevel% neq 0 (
echo ✗ 编译失败!
pause
exit /b 1
)
echo ✓ 编译成功
echo.
echo [3/4] 运行测试...
call mvn test -q
if %errorlevel% neq 0 (
echo ⚠ 测试失败,但继续分析...
)
echo ✓ 测试完成
echo.
echo [4/4] 运行 SonarQube 分析...
if "%SONAR_TOKEN%"=="" (
call mvn sonar:sonar -Dsonar.host.url=%SONAR_HOST_URL%
) else (
call mvn sonar:sonar -Dsonar.host.url=%SONAR_HOST_URL% -Dsonar.login=%SONAR_TOKEN%
)
if %errorlevel% neq 0 (
echo ✗ 分析失败!
pause
exit /b 1
)
echo ✓ 分析完成
goto success
:config
echo.
echo ========================================
echo 当前配置
echo ========================================
echo.
echo SonarQube 服务器: %SONAR_HOST_URL%
echo Token 状态: %SONAR_TOKEN:~0,10%...
echo.
echo 项目配置文件: sonar-project.properties
echo.
type sonar-project.properties | findstr /B "sonar.projectKey sonar.projectName sonar.projectVersion sonar.sources"
echo.
goto end
:start_server
echo.
echo ========================================
echo SonarQube 服务器状态
echo ========================================
echo.
echo SonarQube 已安装并运行在:
echo %SONAR_HOST_URL%
echo.
echo 如需访问 SonarQube:
start %SONAR_HOST_URL%
echo.
goto end
:dashboard
echo.
echo 正在打开 SonarQube 仪表板...
start %SONAR_HOST_URL%/dashboard?id=%SONAR_PROJECT_KEY%
goto end
:success
echo.
echo ========================================
echo 分析成功!
echo ========================================
echo.
echo 查看报告:
echo %SONAR_HOST_URL%/dashboard?id=%SONAR_PROJECT_KEY%
echo.
echo 主要指标:
echo • 可靠性Bugs
echo • 安全性Vulnerabilities
echo • 可维护性Code Smells
echo • 覆盖率Coverage
echo • 重复代码Duplications
echo.
set /p open=是否打开仪表板?(Y/N):
if /i "%open%"=="Y" (
start %SONAR_HOST_URL%/dashboard?id=%SONAR_PROJECT_KEY%
)
:end
echo.
pause