SonarQube Scanner 执行期间出错错误:“您必须为‘未知’定义以下强制属性:sonar.projectKey”

Posted

技术标签:

【中文标题】SonarQube Scanner 执行期间出错错误:“您必须为‘未知’定义以下强制属性:sonar.projectKey”【英文标题】:Error during SonarQube Scanner execution ERROR: "You must define the following mandatory properties for 'Unknown': sonar.projectKey" 【发布时间】:2020-11-03 16:01:09 【问题描述】:

我正在尝试通过 Jenkins 声明式管道执行 sonarqube 分析。

当我在 jenkinfile 中使用以下命令执行 sonarqube 分析时,它可以工作。但是当我在 jenkinsfile 中使用下面的函数时,它给了我错误。但是我想使用 withSonarQubeEnv() 函数进行声纳分析,因为我需要使用“waitForQualityGate”。

withSonarQubeEnv('sonarqube')


stage("Sonarqube Analysis") 
 steps 
  container('ibmcloudcli')  
    echo "Sonarqube Analysis."
    sh label: '', script: """
      /opt/sonarscanner/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner   
     -Dsonar.host.url=XXXX                                       -Dsonar.login=XXXX
-Dsonar.sources=/home/jenkins/agent/workspace/$JOB_NAME/src -Dsonar.projectBaseDir=/home/jenkins/agent/workspace/$JOB_NAME/src  -Dsonar.projectKey=$JOB_NAME 
-Dsonar.language=java 
-Dsonar.java.binaries=/home/jenkins/agent/workspace/$JOB_NAME/target -Dsonar.junit.reportPaths=/home/jenkins/agent/workspace/$JOB_NAME/target/surefire-reports 
-Dsonar.jacoco.reportPath=/home/jenkins/agent/workspace/$JOB_NAME/target/site/jacoco/jacoco.xml 
   """

SonarQube Scanner 3.2.0.1227(安装在 Jenkins slave pod 中)

SonarQube 服务器 7.9.3

JENKINS_VERSION - 2.204.5

仅供参考:sonar-scanner.properties 文件不包含任何信息 一切都是通过命令给出的

并遇到以下问题:

Sonarqube Analysis.
[Pipeline] withSonarQubeEnv
Injecting SonarQube environment variables using the configuration: sonarqube
[Pipeline] 
[Pipeline] sh
+ /opt/sonarscanner/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner
INFO: Scanner configuration file: /opt/sonarscanner/sonar-scanner-3.2.0.1227-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 4.15.0-106-generic amd64
INFO: User cache: /root/.sonar/cache
INFO: SonarQube server 7.9.3
INFO: Default locale: "en_US", source code encoding: "US-ASCII" (analysis is platform dependent)
WARN: SonarScanner will require Java 11 to run starting in SonarQube 8.x
INFO: Load global settings
INFO: Load global settings (done) | time=119ms
INFO: Server id: XXXX
INFO: User cache: /root/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=72ms
INFO: Load/download plugins (done) | time=373ms
INFO: Process project properties
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 2.205s
INFO: Final Memory: 8M/393M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey
ERROR: 
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.
[Pipeline] 
WARN: Unable to locate 'report-task.txt' in the workspace. Did the SonarScanner succeeded?
[Pipeline] // withSonarQubeEnv
[Pipeline] 
[Pipeline] // container
[Pipeline] 

这是 Jenkinsfile “声纳分析”阶段

stage("Sonarqube Analysis") 
           steps 
             container('ibmcloudcli')    
                echo "Sonarqube Analysis."
                withSonarQubeEnv('sonarqube') 
                  sh '''/opt/sonarscanner/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner
                        -Dsonar.host.url=http://XXXX:9000
                        -Dsonar.login=XXXX
            -Dsonar.sources=/home/jenkins/agent/workspace/$JOB_NAME/src 
            -Dsonar.projectBaseDir=/home/jenkins/agent/workspace/$JOB_NAME/src  
            -Dsonar.projectKey=myfirstproject
            //-Dsonar.organization=default-organization
            -Dsonar.projectName=myfirstproject
            -Dsonar.projectVersion=1.0.0-alpha
            -Dsonar.language=java 
            -Dsonar.sourceEncoding=UTF-8
            -Dsonar.java.binaries=/home/jenkins/agent/workspace/$JOB_NAME/target
            -Dsonar.junit.reportPaths=/home/jenkins/agent/workspace/$JOB_NAME/target/surefire-reports 
            -Dsonar.jacoco.reportPath=/home/jenkins/agent/workspace/$JOB_NAME/target/site/jacoco/jacoco.xml  
            -X
            '''
                           
             
           
               

【问题讨论】:

【参考方案1】:

您的扫描仪参数缺少反斜杠。因此 sh 会将它们视为单独的命令,而不是一个长命令。

stage("Sonarqube Analysis") 
           steps 
             container('ibmcloudcli')    
                echo "Sonarqube Analysis."
                withSonarQubeEnv('sonarqube') 
                  sh '''/opt/sonarscanner/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner \
                        -Dsonar.host.url=http://XXXX:9000 \
                        -Dsonar.login=XXXX \
                        -Dsonar.sources=/home/jenkins/agent/workspace/$JOB_NAME/src \
                        -Dsonar.projectBaseDir=/home/jenkins/agent/workspace/$JOB_NAME/src  \
                        -Dsonar.projectKey=myfirstproject \
                        //-Dsonar.organization=default-organization \
                        -Dsonar.projectName=myfirstproject \
                        -Dsonar.projectVersion=1.0.0-alpha \
                        -Dsonar.language=java  \
                        -Dsonar.sourceEncoding=UTF-8 \
                        -Dsonar.java.binaries=/home/jenkins/agent/workspace/$JOB_NAME/target \
                        -Dsonar.junit.reportPaths=/home/jenkins/agent/workspace/$JOB_NAME/target/surefire-reports \
                        -Dsonar.jacoco.reportPath=/home/jenkins/agent/workspace/$JOB_NAME/target/site/jacoco/jacoco.xml \
                        -X
                  '''
                
             
           
 

【讨论】:

【参考方案2】:

请添加 jenkins Prepare SonarQube Scanner 环境必须为真。

【讨论】:

这应该作为评论,这不是答案。

以上是关于SonarQube Scanner 执行期间出错错误:“您必须为‘未知’定义以下强制属性:sonar.projectKey”的主要内容,如果未能解决你的问题,请参考以下文章

sonar-scanner扫描代码出错 SonarQube svn: E170001

sonarqube+sonar-scanner-engine扫描引擎主要执行步骤

在 Jenkins 2 Pipeline 中执行 SonarQube Scanner

sonarqube+sonar_scanner+jenkins执行代码检查

sonarqube+sonar_scanner+jenkins执行代码检查

Sonarqube 扫描错误,行超出范围?