3
votes

I am working on a project to analyze the code with sonarqube in jenkins. The project structure under workspace is: /var/lib/jenkins/workspace/myproject-trunk-sonar/myproject/pom.xml, Module1, Module2, etc

My sonar-project.properties:

# Required metadata
sonar.projectKey=Myproject
sonar.projectName=Myproject
sonar.projectVersion=1.0

#Set modules IDs
sonar.modules=Module1, Module2
# Comma-separated paths to directories with sources (required)
sonar.sources=src/main/java

# Language
sonar.language=java

# Encoding of the source files
sonar.sourceEncoding=UTF-8

If I have the sonar-project.properties file under /var/lib/jenkins/workspace/myproject-trunk-sonar/myproject where my pom.xml and Modules are, I see the error below:

ERROR: Error during SonarQube Scanner execution
ERROR: You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.sources
ERROR: 
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.
ERROR: SonarQube scanner exited with non-zero code: 1 

If I have the sonar-project.properties file under /var/lib/jenkins/workspace/myproject-trunk-sonar, I see the error below:

ERROR: Error during SonarQube Scanner execution
java.lang.IllegalStateException: The base directory of the module 'Module1' does not exist: /var/lib/jenkins/workspace/myproject-trunk-sonar/Module1
    at org.sonarsource.scanner.cli.Conf.setModuleBaseDir(Conf.java:180)
    at org.sonarsource.scanner.cli.Conf.loadModuleConfigFile(Conf.java:172)
    at org.sonarsource.scanner.cli.Conf.loadModulesProperties(Conf.java:137)
    at org.sonarsource.scanner.cli.Conf.loadProjectProperties(Conf.java:111)
    at org.sonarsource.scanner.cli.Conf.properties(Conf.java:59)
    at org.sonarsource.scanner.cli.Main.execute(Main.java:68)
    at org.sonarsource.scanner.cli.Main.main(Main.java:61)
ERROR: 
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

ERROR: SonarQube scanner exited with non-zero code: 1

How can I let the sonar scanner look at the directory where the modules with java code is? When I have the properties file in the directory where the modules are it couldn't even find the properties file and complained about missing mandatory fields.

Thanks for any help.

3
If you have a Maven project, then you should be performing analysis with mvn sonar:sonar (after doing a compile, of course)G. Ann - SonarSource Team
Thanks for quick reply. I tried mvn sonar:sonar and I see the below error: [ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.0:sonar (default-cli) on project Myproject: Execution default-cli of goal org.codehaus.mojo:sonar-maven-plugin:2.0:sonar failed: A required class was missing while executing org.codehaus.mojo:sonar-maven-plugin:2.0:sonar: org/sonatype/aether/graph/DependencyFilter Thanks for the help.iranichai

3 Answers

2
votes

Thanks for all the replies. I was able to resolve the issue by modifying the directory structure of my project. I removed the directory 'myproject' and placed the sonar-project.properties file where all the modules are and was able to run the scanner successfully.

Thanks for the help.

0
votes

To let maven run the sonar analysis you have to add the sonar-maven plugin to your pom.

<pluginManagement>
    <plugins>
        ...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
    </plugins>
</pluginManagement>

And the run set jenkins to run maven with sonar goals mvn sonar:sonar.

About modules, the solution of the problem will depend on the code structure.

See here examples of multi-modules sonar analysis.

0
votes

Do a postjob Sonarqube action in jenkins. Forget sonar-project.properties. If your project is well build with maven package, sonarqube will analyse it with no problem. tell us. thx