62

I am struggling with an error with a multi-modules project, the struture is simple, it looks like this :

 root 
   module a
   module b
   module c
   pom.xml

After using the maven command line : clean sonar:sonar deploy

I have this error :

Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.3.0.603:sonar (default-cli) on project X : Please provide compiled classes of your project with sonar.java.binaries property -> [Help 1]

EDIT : Here is the structure of my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <groupId>groupeId</groupId>
    <artifactId>artifactId</artifactId>
    <version>version</version>
    <packaging>pom</packaging>
    <name>${project.artifactId}-parent</name>
    <description>description</description>
    <build>
        <plugins>
            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.3.0.603</version>
            </plugin>
        </plugins>
    </build>
    <modules>
        <module>module a</module>
        <module>module b</module>
        <module>module c</module>
    </modules>
</project>
CommonPeople
  • 700
  • 1
  • 6
  • 16

12 Answers12

73

You're running your Maven steps in the wrong order:

  • clean - delete all previous build output
  • sonar:sonar - run analysis (which requires build output)
  • deploy - build &etc...

Try this instead:

mvn clean deploy sonar:sonar

Now if you're about to object that you don't want to actually "deploy" the jar until/unless the changed code passes the Quality Gate, well... that requires a different workflow:

mvn clean package sonar:sonar
// check quality gate status
// if (qualityGateOk) { deploy }

The particulars of those last two steps will depend on your CI infrastructure. But for Jenkins, step #2 is well documented

G. Ann - SonarSource Team
  • 22,346
  • 4
  • 40
  • 76
56

I got the same error while invoking Standalone SonarQube Analysis as a Jenkins job pre-build step, which I fixed adding sonar.java.binaries=**/target/classes along with other SonarQube Analysis properties, as follows:

sonar.projectKey=TEST-PROJECT
sonar.projectName=TEST-PROJECT
sonar.projectVersion=1.0
sonar.sources=src/main/java/
sonar.language=java
sonar.java.binaries=**/target/classes  
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
6

I had a seme problem. I did below steps Added Invoke top-level maven target from build steps (It should be the first build step) added clean install.

and also added below properties to my Analysis properties under Execute SonarQube scanner.

sonar.projectVersion=1.0
sonar.sources=src/main/java
sonar.sourceEncoding=UTF-8
sonar.language=java
sonar.java.binaries=target/classes
Rajitha Bhanuka
  • 714
  • 10
  • 11
5

In my case works with:

mvn clean install sonar:sonar -Dsonar.projectKey=groupId:artifactId -Dsonar.host.url=http://localhost:9000 -Dsonar.login=loginHASH -Dsonar.sources=src/main/java/ -Dsonar.java.binaries=target/classes

rewrite:

mvn clean install sonar:sonar 
-Dsonar.projectKey=groupId:artifactId
-Dsonar.host.url=http://localhost:9000
-Dsonar.login=loginHASH
-Dsonar.sources=src/main/java/
-Dsonar.java.binaries=target/classes
joseluisbz
  • 1,491
  • 1
  • 36
  • 58
2

I had the same problem but with BitBucket pipelines, so need to setup variables in bitbucket-pipelines.yml

  - mvn compile
  - pipe: sonarsource/sonarcloud-scan:1.1.0
    variables:
      EXTRA_ARGS: -Dsonar.java.binaries=\"target/classes\"
GetoX
  • 4,225
  • 2
  • 33
  • 30
2

mvn sonar:sonar did not work in Jenkins although it worked from the command line.

mvn compile sonar:sonar worked.

Tony BenBrahim
  • 7,040
  • 2
  • 36
  • 49
1

For Java, the binaries are in the target folder. That's why you should use mvn clean install sonar:sonar to make sure your project is compiled and inside the target folder.

Sonar scans your binary classes.

Gene
  • 10,819
  • 1
  • 66
  • 58
1

I have added the following properties to my Analysis properties under Execute SonarQube scanner section and my Java codes were scanned.

sonar.projectKey=investor2
sonar.sources=src/main/java/
sonar.language=java
sonar.java.binaries=.

Jenkins SonarQube properties to scan java code Sonarqube Output

1

Step 1 :- SonarQube File Location

C:\Program Files\sonar-scanner-4.7.0.2747-windows\conf\sonar-scanner.properties

Step 2 :- Open and Add this line

#----- Default source code encoding
#sonar.sourceEncoding=UTF-8

sonar.java.binaries=**/src/main/java

Step Final :- Run the command on cmd

sonar-scanner.bat -D"sonar.projectKey=<YOUR PROJECT KEY>" -D"sonar.sources=." -D"sonar.host.url=<YOUR HOSTING URL>" -D"sonar.login=<YOUR LOGIN KEY>"

Example or Other configuration settings:-

#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=https://codecheck.harriersys.com/

#----- Default source code encoding
#sonar.sourceEncoding=UTF-8

#sonar.exclusions=**/*.java
#sonar.projectKey=com.example
#sonar.projectName=App Name
#sonar.projectVersion=1.0
#sonar.sources=D://App Name
#sonar.language=java
#sonar.java.binaries=D:/App Name/app/src/main/java

sonar.java.binaries=**/src/main/java
Ashfaque
  • 179
  • 4
0

You can fix that by passing sonar.java.binaries by maven tool.

mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=faeef1e48b8d00290a0f3cc00021720baf1ca4dd -Dsonar.java.binaries=D:\aiwb_s**

SAN
  • 107
  • 7
0

In my case, after a refactoring, a submodule was mistakenly changed to <packaging>pom</packaging> even though it contained Java sources and should be built as Jar.

Reverting that fixed the issue.

Didier L
  • 18,905
  • 10
  • 61
  • 103
-1

This is a parent pom and has "pom" packaging. It doesn't contain compiled code.

You should put move the sonar.maven.plugin to the module poms that compile code.