14

I have a project that has the following package structure

src/main/proj
    -com.company.package
        -appName
            -morepackages

        -appVersion2
            -morepackages

sonar-runner.properties
sonarBuild.sh
sonar-runner-project2.properties
sonarBuildProject2.sh

As it stands, with the default properties file i can run the sonar report and it will generate a review for the whole project

sonar.projectKey=KEY
sonar.projectName=PROJNAME
sonar.projectVersion=1.0

sonar.host.url=someurl

#Path for sonar sources
sonar.sources=src/main/java

#Path for libraries
sonar.libraries=target/lib/*.jar

#Path for binaries
sonar.binaries=target/classes

#--------Default database
sonar.jdbc.url=someurl
sonar.jdbc.driver=com.mysql.jdbc.Driver

#----- Global database settings
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- Default directory layout
sonar.java.source=1.6
sonar.java.target=1.6

sonar.sourceEncoding=UTF-8

Ideally however I would like to separate the two reports so i have one for the original package and one for the appVersion2 package. Now - as mentioned above I have created a separate properties file for each. Should i just point sonar.sources to the respective packages for each job to analyse or is there a better way to do this all in one property file?

Thanks

EDIT

My multi module attempt properties file looks as follows

sonar.projectKey=rootkey
sonar.projectName=rootname
sonar.projectVersion=1.0
sonar.host.url=rooturl
sonar.sources=src/main/java/
sonar.libraries=target/lib/*.jar
sonar.modules=module1,module2
sonar.sourceEncoding=UTF-8

#----- Global database settings
sonar.jdbc.username=user
sonar.jdbc.password=pass

sonar.java.source=1.7
sonar.java.target=1.7


#--------Default database
sonar.jdbc.url=url
sonar.jdbc.driver=com.mysql.jdbc.Driver


module1.sonar.projectName=proja
module2.sonar.projectName=projb

module1.sonar.projectKey=projakey
module2.sonar.projectKey=projbkey

#Path for sonar sources
module1.sonar.sources=src/main/java/app1code
module2.sonar.sources=src/main/java/app2code

#Path for binaries
module1.sonar.binaries=target/classes/app1binaries
module2.sonar.binaries=target/classes/app2binaries

I get an error in the output saying....

Exception in thread "main" org.sonar.runner.RunnerException: org.sonar.runner.RunnerException: The base directory of the module 'module1' does not exist: patthtoapp/module1
    at org.sonar.runner.Runner.delegateExecution(Runner.java:288)
    at org.sonar.runner.Runner.execute(Runner.java:151)
    at org.sonar.runner.Main.execute(Main.java:84)
    at org.sonar.runner.Main.main(Main.java:56)
Caused by: org.sonar.runner.RunnerException: The base directory of the module 'module1' does not exist: pathtoapp/module1
    at org.sonar.runner.internal.batch.SonarProjectBuilder.setProjectBaseDir(SonarProjectBuilder.java:279)
    at org.sonar.runner.internal.batch.SonarProjectBuilder.loadChildProject(SonarProjectBuilder.java:191)
    at org.sonar.runner.internal.batch.SonarProjectBuilder.defineChildren(SonarProjectBuilder.java:169)
    at org.sonar.runner.internal.batch.SonarProjectBuilder.generateProjectDefinition(SonarProjectBuilder.java:122)
    at org.sonar.runner.internal.batch.Launcher.execute(Launcher.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.sonar.runner.Runner.delegateExecution(Runner.java:285)
Srijani Ghosh
  • 3,326
  • 3
  • 28
  • 56
Biscuit128
  • 4,716
  • 20
  • 79
  • 137

3 Answers3

29

You have to specify the projectBaseDir if the module name doesn't match you module directory.

Since both your module are located in ".", you can simply add the following to your sonar-project properties:

module1.sonar.projectBaseDir=.
module2.sonar.projectBaseDir=.

Sonar will handle your modules as components of the project:

Result of Sonar analysis

EDIT

If both of your modules are located in the same source directory, define the same source folder for both and exclude the unwanted packages with sonar.exclusions:

module1.sonar.sources=src/main/java
module1.sonar.exclusions=app2code/**/*

module2.sonar.sources=src/main/java
module2.sonar.exclusions=app1code/**/*

More details about file exclusion

schnatterer
  • 6,520
  • 6
  • 53
  • 71
Raphaël
  • 3,160
  • 23
  • 23
  • this approach leads to a new error = Caused by: org.sonar.squid.api.AnalysisException: Got an exception - org.sonar.squid.api.AnalysisException: The source directory does not correspond to the package declaration com.company.package, file : pathtofile/myclass.java, line : 0 – Biscuit128 Feb 24 '14 at 12:54
  • If your modules sources are in the same folder, try specifying the correct source folder for both modules and excludes the unwanted package with 'sonar.exclusions'. – Raphaël Feb 24 '14 at 12:57
  • I edited my answer to address the AnalysisException. – Raphaël Feb 24 '14 at 14:13
  • just trying it out now - project takes a while to build :p – Biscuit128 Feb 24 '14 at 14:22
3

You can define a Multi-module project structure, then you can set the configuration for sonar in one properties file in the root folder of your project, (Way #1)

Rytek
  • 563
  • 8
  • 22
vzamanillo
  • 9,465
  • 1
  • 31
  • 54
0

Do the build job on Jenkins first without Sonar configured. Then add Sonar, and run a build job again. Should fix the problem

amigo21
  • 291
  • 3
  • 15