Search code examples
jenkinsgradlesonarqubejenkins-pipeline

'.' is not recognized as an internal or external command for Gradle command in Jenkins when gradlew script is executed


I try to run SonarQube analysis for a Gradle project in a Jenkins Pipeline using the following code:

stage('SonarQube') {
  withGradle {
     withSonarQubeEnv('SonarQube Env') { 
       bat "./gradlew sonarqube"
     }
  }
}

The Gradle plugin is installed in Jenkins but I am getting the following error:

05:15:05  D:\*\*\*\*\*\*>./gradlew sonarqube 
05:15:05  '.' is not recognized as an internal or external command,

Solution

  • Two things are incorrect in your code. On Windows machines you have to:

    1. use backslashes instead of slashes in paths (./command.\command)
    2. execute script written for Windows (gradlew is a Unix script, gradlew.bat is a Windows script)

    This code should work:

    stage('SonarQube') {
      withGradle {
        withSonarQubeEnv('SonarQube Env') { 
          bat '.\\gradlew.bat sonarqube'
        }
      }
    }
    

    Gradle Wtapper by default is provided with two script gardlew and gradlew.bat. If your project doesn't have the gradlew.bat file, execute on your Unix machine ./gradlew wrapper. The missing file will be generated.

    Btw. You don't need the Jenkins Gradle plugin, when you use Gradlew Wrapper. The plugin is required when you want to provide Gradle installations for jobs, example:

    stage('SonarQube') {
      withGradle {
        withSonarQubeEnv('SonarQube Env') { 
          bat "${tool(name: 'toolId', type: 'gradle')}\\bin\\gradle.bat sonarqube"
        }
      }
    }
    

    toolId must much the identifiers used in the Jenkins Global Tool Configuration, examples: gradle-6.X, gradle-6.8.3 etc.