Pritish
Pritish

Reputation: 774

How to resolve "please provide compiled classes with sonar.java.binaries property"?

I tried all the possible solutions posted by other people but still I am struggling to resolve this issue. I believe in my case, it has got to do something with the agents. I will post 2 codes, the one code works but the other doesn't. Both the codes call the same groovy methods but still the second code snippet doesn't work.

The below code 1 works fine and executed the pipeline successfully:

pipeline{
        agent { label 'docker-kitchensink-slave' }

        stages{
            stage('Checkout') {
                steps{
                    checkout scm
                }
            }

            //Build and Unit Tests
            stage('Build and Unit Tests') {
                steps{
                    script{
                        if (buildType.buildSystem == 'npm'){
                            buildNpm(configuration)
                        } else {
                            build(configuration)
                        }
                    }
                }
            }

            // SonarQube Analysis
            stage('SonarQube analysis') {
                steps{
                    script{
                        if (buildType.buildSystem != 'npm'){
                            sonarQubeGating(configuration)
                        }
                    }
                }
            }

            // Build Docker Image and Push to Artifactory
            stage('Build Docker Image and Push to Artifactory') {
                steps{
                    artifactoryImagePush(configuration)
                }
            }

            // Approve DEV Deployment
            stage('Approve Dev Deployment') {
                agent none
                when {
                    anyOf {
                        expression {
                            return (env.GIT_BRANCH.equals('master') || env.GIT_BRANCH.startsWith('hotfix-'))
                        }
                    }
                }
                steps{
                    approveDeployment()
                }
            }
        }
    }

The below code 2 doesn't work:

pipeline{
        agent none

        stages{
            stage('Checkout') {
                agent { label 'docker-kitchensink-slave' }
                steps{
                    checkout scm
                }
            }

            //Build and Unit Tests
            stage('Build and Unit Tests') {
                agent { label 'docker-kitchensink-slave' }
                steps{
                    script{
                        if (buildType.buildSystem == 'npm'){
                            buildNpm(configuration)
                        } else {
                            build(configuration)
                        }
                    }
                }
            }

            // SonarQube Analysis
            stage('SonarQube analysis') {
                agent { label 'docker-kitchensink-slave' }
                steps{
                    script{
                        if (buildType.buildSystem != 'npm'){
                            sonarQubeGating(configuration)
                        }
                    }
                }
            }

            // Build Docker Image and Push to Artifactory
            stage('Build Docker Image and Push to Artifactory') {
                agent { label 'docker-kitchensink-slave' }
                steps{
                    unstash 'artifacts'
                    unstash 'artifacts'
                    artifactoryImagePush(configuration)
                }
            }

            // Approve DEV Deployment
            stage('Approve Dev Deployment') {
                agent none
                when {
                    anyOf {
                        expression {
                            return (env.GIT_BRANCH.equals('master') || env.GIT_BRANCH.startsWith('hotfix-'))
                        }
                    }
                }
                steps{
                    approveDeployment()
                }
            }
        }
    }

I get the error as below:

[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project xyz-service: Your project contains .java files, please provide compiled classes with sonar.java.binaries property, or exclude them from the analysis with sonar.exclusions property. -> [Help 1]

Below is my sonar code:

void call(Map optionParams = [:]) {
    script {
        try {
            String jacocoPath = optionParams.get('buildSystem').equals('gradle') ?
                    'build/JacocoReport/test/jacocoTestReport.xml' : 'target/site/jacoco/jacoco.xml'

            glSonarMavenScan gitUserCredentialsId: 'sonar-key', javaVersionForSonar: '11.0', mavenVersion: '3.5.4',
                    additionalProps: ['sonar.coverage.jacoco.xmlReportPaths' : jacocoPath]
        } catch (Exception e) {
            echo "The following Sonar exception thrown ${e}"
            //Stop build here, unless 'requireSonar' is set to False (String or Boolean)
            if (!optionParams.get('requireSonar').toString().equalsIgnoreCase('false')) {
                throw e
            }
        }
    }
}

Upvotes: 0

Views: 1073

Answers (1)

David M. Karr
David M. Karr

Reputation: 15225

I'm a little confused about what you're trying to achieve here. You are showing the code that works. Are you trying to understand WHY the first block works, compared to the second, or are you just trying to get it working? If the latter, clearly you are already done.

If the former, I'm only familiar with scripted pipeline, not declarative pipeline, but it seems possible to me that if there is more than one build node that satisfies that label, then each of those "agent" lines could potentially select a build node, and each one could potentially select a different one. If the build step executes a different node than the sonarqube scan is run on, you will find yourself in a workspace without any compiled classes.

Upvotes: 1

Related Questions