gradle项目编译的时候如何关联sonar进行代码质量检测?

  • Choerodon平台版本:0.17.0

  • 运行环境:自主搭建

  • 问题描述:

    请尽量详细的描述您遇到的问题,以便我们能更快速的提供解决办法。

    官方文档的描述如下

在.gitlab-ci.yml文件build阶段添加
- mvn --batch-mode verify sonar:sonar -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.gitlab.project_id=$CI_PROJECT_PATH -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME -Dsonar.analysis.serviceGroup=$GROUP_NAME -Dsonar.analysis.commitId=$CI_COMMIT_SHA -Dsonar.projectKey=${GROUP_NAME}:${PROJECT_NAME}

sonar.projectKey=${GROUP_NAME}:${PROJECT_NAME}不可更改;否则,在查看代码质量时将获取不到对应数据
GROUP_NAME和PROJECT_NAME是devops-service内置的环境变量, GROUP_NAME=当前项目所在组织编码-当前项目编码,PROJECT_NAME=当前应用编码

这个是针对mvn的项目编译,针对gradle的项目应该如何操作?

  • 执行的操作:
    我直接在gradle build之后加上官方文档的参数,但是编译的时候出错了
Cloning repository...
Cloning into '/operation-dongfang/jj-guahao'...
Checking out 2417e03c as dev...
Skipping Git submodules setup
$ http_status_code=`curl -o .auto_devops.sh -s -m 10 --connect-timeout 10 -w %{http_code} "${CHOERODON_URL}/devops/ci?token=${Token}&type=microservice"` # collapsed multi-line command
$ gradle build --batch-mode verify sonar:sonar -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.gitlab.project_id=$CI_PROJECT_PATH -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME -Dsonar.analysis.serviceGroup=$GROUP_NAME -Dsonar.analysis.commitId=$CI_COMMIT_SHA -Dsonar.projectKey=${GROUP_NAME}:${PROJECT_NAME}
Starting a Gradle Daemon, 9 busy and 2 incompatible and 1 stopped Daemons could not be reused, use --status for details

FAILURE: Build failed with an exception.

* What went wrong:
Problem configuring task :build from command line.
> Unknown command-line option '--batch-mode'.

* Try:
Run gradle help --task :build to get task usage details. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s
ERROR: Job failed: command terminated with exit code 1

去掉报错的2个参数,还是报错

$ gradle build sonar:sonar -Dsonar.host.url=$SONAR_URL -Dsonar.login=$SONAR_LOGIN -Dsonar.gitlab.project_id=$CI_PROJECT_PATH -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME -Dsonar.analysis.serviceGroup=$GROUP_NAME -Dsonar.analysis.commitId=$CI_COMMIT_SHA -Dsonar.projectKey=${GROUP_NAME}:${PROJECT_NAME}
Starting a Gradle Daemon, 9 busy and 1 incompatible and 3 stopped Daemons could not be reused, use --status for details

FAILURE: Build failed with an exception.

* What went wrong:
Project 'sonar' not found in root project 'jj-guahao'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 12s
ERROR: Job failed: command terminated with exit code 1

是在代码里要添加sonar相关的配置么?

  • 报错信息(请尽量使用代码块的形式展现):
  • 建议:

    提出您认为不合理的地方,帮助我们优化用户操作

后续能不能同时支持mvn和gradle两种方式,gradle的ci镜像之前没有,现在sonar也是没有gradle。

原理是一样的,我们当前的模板都是基于mvn的步骤进行编写的,后续会进行其他方式的扩充
gradle与sonar的连接可以参考sonar官方提供的文档进行ci编写,本质上ci只是执行你打包的脚本而已
https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-gradle/