Friday, December 7, 2012

Sonar Setup in local machine


Sonar is an open platform to manage code quality.it will helpful to follow some coding standards and to reduce code review comments.


Installation Steps
1. Install sonar server http://dist.sonar.codehaus.org/sonar-3.0.zip
2. Extract it in at your choice of directory
3. Create database called "sonar" in mysql
4. Open sonar-3.0\conf\sonar.properties

5. Provide your database password (e.g. as below)
sonar.jdbc.username: root
sonar.jdbc.password: root

6. Find the below lines related to mySQL and uncomment it
comment the default derby database config and enable the below properties for MySQL
sonar.jdbc.validationQuery: select 1
sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.driverClassName: com.mysql.jdbc.Driver

7. Start sonar by Running " sonar-3.0\bin\windows-x86-32\StartSonar.bat"
8. In console "Started SelectChannelConnector@0.0.0.0:9000" message will be displayed
9. Open browser and access http://localhost:9000
10. Login using admin/admin
11. Go to Configuration by clicking configuration link in top menu
12. Click on Restore Profile link
13. You ca change the Rules for the sonar if you want.if you created your own rules for your company.(e.g as below)
14. Choose newly created "xxxx_Liferay_Projects"
15. Click on Restore Profile Button, this will install xxxxx Rules in Sonar
16. Now you can see "XXXX Liferay Projects" in Java Profiles Table

After installation, we required Build Setup.
1. Copy attached "sonar-ant-task-1.3.jar" in <Apache Ant Home dir>\lib
If any issues also keep the jar file in you eclipse installed folder at below path
D:\eclipse\plugins\org.apache.ant_1.7.0.v200803061910\lib

2. Replace attached build.xml to your plugin projects build.xml.
3. Open build.xml and change following two properties with your mysql database username and password
<property name="sonar.jdbc.username" value="root" />
<property name="sonar.jdbc.password" value="root" />
 and also give the value of <property name="sonar.sources, <property name="sonar.binaries",<property name="sonar.libraries" for your portlet,hook etc.
4. Build your portlet and hook using ant command "ant deploy" or "ant compile"
5. Then generate sonar report using "ant sonar". For this you have to be at SDK level in command line and then fire 'ant sonar' command.
6. Open http://localhost:9000 , you will see a project "encore-portal"
7. That’s All

How to exclude a java file from Sonar
lets assume you want to exclude the com.xxx.xxx.service.impl.xx.XXXXClass.java,com.xxx.xxx.service.persistence.**.*
Replace '.' with '/'.
you can exclude the single file or folder with wild card '*'.
Add the below propety in build.xml
<property name="sonar.exclusions" value="com/xxx/xxx/service/impl/xx/XXXXClass.java,com/xxx/xxx/service/persistence/**/*" />

For e.g build.xml will looks as

<?xml version="1.0"?>
<project name="sonar-portlet" basedir="." default="deploy">
<import file="../build-common-portlet.xml" />
<!-- Add the target -->
<target name="sonar">
<property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8" />
<property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" />
<!-- the username and password of your database -->
<property name="sonar.jdbc.username" value="root" />
<property name="sonar.jdbc.password" value="root" />
<!-- list of mandatories Sonar properties -->
<property name="sonar.sources" value="docroot/WEB-INF/src,docroot/html" />
<property name="sonar.binaries" value="docroot/WEB-INF/classes" />
<!-- <property name="sonar.exclusions" value="com/xxx/xxxx/service/impl/**/*,com/xxx/xxxx/service/persistence/**/*" /> -->
               <--- sonar.projectKey is used to identify the project in sonar,it should be unique otherwise it will over rides the previous project  ---->
<property name="sonar.projectKey" value="sonar-test" />
<sonar:sonar key="sonar-test" version="0.1-SNAPSHOT" xmlns:sonar="antlib:org.sonar.ant" />
</target>
</project>


for more info go through http://www.sonarsource.org/

5 comments:

  1. Nice and helpful post.

    I am facing one issue we i use the ant sonar command at that time getting error, ant sonar command is not working so please help me

    ReplyDelete
  2. How to add project in sonar.
    step 1 . add following in the pom.xml of your project .

    4.0.0
    sonar
    sonar
    war
    0.0.1-SNAPSHOT


    sonar

    true


    java
    jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
    com.mysql.jdbc.Driver
    root
    root

    http://localhost:9009






    maven-compiler-plugin
    3.1

    1.7
    1.7



    maven-war-plugin
    2.3

    WebContent
    false



    org.jacoco
    jacoco-maven-plugin
    0.6.4.201312101107


    default-prepare-agent

    prepare-agent



    default-prepare-agent-integration

    prepare-agent-integration



    default-report

    report



    default-report-integration

    report-integration



    default-check

    check





    BUNDLE



    COMPLEXITY
    COVEREDRATIO
    0.60














    step 2 . goto project root using cmd i.e project-root >mvn sonar:sonar . Press enter

    ReplyDelete
  3. I am integrating sonat4.4 to liferay.But when i am executing sonar ant task i am getting following error.can anybody clear this error
    E:\Liferay\Liferay 6.2\workspace\plugins\build.xml:305:
    org.sonar.batch.bootstrapper.BootstrapException: java.io.IOException:
    Server returned HTTP response code: 400 for URL: http://localhost:9000/sonar/batch/

    ReplyDelete
  4. You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this... affordable services arizona

    ReplyDelete
  5. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work. www.Everydaylocksmiths.com

    ReplyDelete