Sonar Scanner inspect multi module java project (Mixed eclipse with maven style) from command line

Well, recently I was need to review legacy project to do meassuring the code quality java.

There are a project with multi module structure based on old eclipse syle mixed with maven structure, I have 3 modules (2 in eclipse and 1 in maven) all of them are used to build the final artifact and I needed to do code inspection with sonarqube.

For example:

\tmp\root
├───module1
├───module2
├───mvnmodule
    └───src

Fortunly sonar can manage this, let’s gets started.

Requirements

  • A sonarqube instance (version 6.6)
  • Sonar scanner tool (version 3.0.3)
  • Compiled classes for projects to analize

Steps

  1. Login into sonar instance and select your profile (at the top rigth corner) then click on My profile
  2. Create a new token on tab Security, write a name for the token and click on Generate and copy the token string, becareful but you won’t be able to see this string again.
  3. Conigure your sonar scanner, open the instalation folder (where you uncompress it) and edit the file conf/sonar-scanner.properties, add the host and login token string:
    #Configure here general information about the environment, such as SonarQube DB details for example
    #No information about specific project should appear here
    
    #----- Default SonarQube server
    sonar.host.url=http://localhost:9000
    
    #----- Default source code encoding
    sonar.sourceEncoding=UTF-8
    
    #----- Security (when 'sonar.forceAuthentication' is set to 'true')
    sonar.login=57e0bf00a0af633f5c0534fc72535c16f2f0fc3b
    
    
  4. Create a project configuration file into your source code folder, go to project folder and create a file named sonar-project.properties, set the properties to binaries (compiled classes) and the source code per module, previously you need it compile the project with their modules. I use eclipse to build each of them. The content of file is something like these:
    #Required project data fron sonar
    sonar.projectKey=com.abc:my-project
    sonar.projectName=abc-my-project
    sonar.projectVersion=1.0
    sonar.sourceEncoding=UTF-8
    
    #sonar.modules=PalacioHierro
    sonar.modules=module1,module2,mvnmodule
    
    sonar.java.source=1.7
    
    #Lib Dir (Opcional) ej: path/library.jar,path/to/classes/dir
    sonar.libraries=module2/lib
    
    # Project Language ( by default is Java)
    sonar.language=java
    
    # Properties can obviously be overriden for
    # each module - just prefix them with the module ID
    module1.sonar.projectName=my-project-m1
    module1.sonar.java.source=1.7
    module1.sonar.sources=src,WebContent
    module1.sonar.java.binaries=build/classes
    
    module2.sonar.projectName=my-projct-m2
    module2.sonar.java.source=1.7
    module2.sonar.sources=src
    module2.sonar.java.binaries=build/classes
    
    mvnmodule.sonar.projectName=mvn-webapp
    mvnmodule.sonar.java.source=1.7
    mvnmodule.sonar.sources=src/main/java,src/main/resources,src/main/webapp
    mvnmodule.sonar.java.binaries=target/classes
    
    sonar.skipDesign=true
    sonar.skipPackageDesign=true
    sonar.profile=my-profile
    
  5. Start the inspection, open an terminal and go to your root project folder and execute sonnar-scanner, this will start the code inspection (maybe take a long time, depend of  size of their projects) at the end you must see in the log something like this “ANALYSIS SUCCESSFUL, you can browse http://loaclhost:9000/dashboard/index/com.abc
  6. In the dashboard, on the code cactegory, you can see the result for the three modules.
    2018-01-25 19_06_14-Code - eph-tarjeta-palacio

That’s it.

Cheers!

Anuncios