Perform Java code analysis with Team Foundation Server and SONAR

Perform Java code analysis with Team Foundation Server and SONAR

Rate This
  • Comments 4

One of our ALM Rangers Hosam Kamel from our Center of Expertise has recently just finished a large project that involved integrating several different technologies and platforms.  In this project he made several key learning's and wanted to share them with the ALM Community!

Thanks Hosam!!

 

****************************

 

Overview

Quality is not something that can be easily added later. Problems that are too complex, too obscure, or are discovered too late in the product cycle are usually not fixed, the Code Analysis feature of Visual Studio performs static code analysis on code to help developers identify potential design, globalization, interoperability, performance, security, and a host of other categories of potential problems. (In Visual Studio 2013 a lot of new enhancement has been added you can check this blog post for more details What is New in Code Analysis for Visual Studio 2013)

Many organizations use different development tools to target different software platforms while Visual Studio is often the tools of choice for the Widows platform, Eclipse-based Integrated Development Environments (IDEs) are almost invariably the tools of choice, TFS is Microsoft’s collaboration platform for software development teams. It provides services such as source control, management of work items, request management, a test platform, and build and release management and it helps the organizations to avoid having multiple Application Lifecycle Management (ALM) tools and methodologies.

In this post I’ll focus more on how you can use Team Explorer Everywhere to build Java project and run code analysis using SONAR as a build step.

Tools

Multiple tools will be integrated together to achieve this scenario.

· Eclipse

· Team Explorer Everywhere

· Microsoft Visual Studio Team Foundation Server 2012 Build Extensions: The Team Foundation Server Build Extensions provide the ability to execute Ant or Maven builds from Team Foundation Server and publish the results of the build along with any associated JUnit test results back to Team Foundation Server.

· Apache Ant: The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications.

· SonarQube (previously called Sonar) is an open source quality management platform, dedicated to continuously analyze and measure technical quality, from project portfolio to method. If you wish to extend the SonarQube platform with open source plugins.

· SonarQube Ant Task: The SonarQube Ant Task allows integration of SonarQube analysis into an Apache Ant build script.

Integration Scenario

Developers work with Eclipse and integrated with Team Foundation Server. When developers want to build their changes, they can push their changes to Team Foundation Server and either setup a continuous integration build or kick-off the build manually.

As a part of build process, SONAR code analysis will be kicked-off to run the code analysis on the latest pushed code on TFS and the result will then get published to SONAR portal.

Java developers can check the code analysis from SOANR portal fixing the issues locally and re-run the build again if needed.

Install and configure Microsoft Visual Studio Team Foundation Server 2012 Build Extensions

· Download the step file from Visual Studio Extension gallery, You must have the following installed on the same server that is hosting the Team Foundation Build Agent:

  • Java JDK (the latest one available from your JDK vendor is recommended)
  • Ant (if Ant support is required).

Install and configure SONAR Ant Task

Create a new build definition based on Ant

· Click on “New Build Definition

· Follow the wizard

· Make sure on the “Project File” step to pick “MSBuild Configuration from Existing Ant Buildfile

image

·image

  • Point to your build.xml file

 

Modify the build script to kick-off SONAR

  • Open the build.xml file and add the following lines

<!-- Define the SonarQube target -->
    <target name="sonar">
        <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpath="/path/sonar-ant-task.jar">
            <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
            <classpath path="$ANT_HOME/lib/sonar-ant-task-2.1.jar" />
        </taskdef>
        <!-- Execute Sonar -->
    <sonar:sonar />

  • The typical configuration should be as below

Kick-off the new build based on the build definition we created

image

  • Wait for the build to complete

image

  • Once the build succeeded you can navigate now to SONAR portal (typically http://localhost:9000) to check the projects and the latest analysis reports.

image

  • Select the project by clicking on the project name and you will get access java code analysis reports for that project

image

In Closing

Visual Studio provide a great tool to ensure quality with enhanced code review workflows to facilitate the collaboration among developers and provide a rich environment to review code and propose changes.

In Team Foundation Server TFS we put more focus on the Cross-Platform Development, You use the languages that are right for your projects, and you target the platforms that let you reach your customers. Platforms like Windows Phone, Windows, Android, iOS, Linux, and more, with data and services in the cloud, As far as TFS is concerned, all code is created equal. You can use TFS to share code and to build continuously across all your platforms. And TFS can help you collaborate within your team, and with your customers.

About the Author:

clip_image001

Hosam Kamel is a regional technology solution professional working for MEA Center Of Expertise specializing in Visual Studio ALM focusing on helping software professionals and organizations build better applications and solutions using Microsoft Application Lifecycle Management technologies, practices and tools working with development teams eliminate traditional silos between development, testing, and project management to establish cohesive processes with the Visual Studio ALM tools.

Hosam is also an active Visual Studio ALM Ranger contributing to a lot of the ALM Ranger projects.

Leave a Comment
  • Please add 7 and 2 and type the answer here:
  • Post
  • I am successfully able to do TFS Build by integrating Eclipse.

    But, I am facing the error on Executing Sonar in Ant Build.xml. (I am not familiar with Java )

    "The prefix "sonar" for element "sonar:sonar" is not bound."  

    FYI.

    <!-- Define the SonarQube target -->

       <target name="sonar">

           <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpath="/path/sonar-ant-task.jar">

               <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->

               <classpath path="$ANT_HOME/lib/sonar-ant-task-2.1.jar" />

           </taskdef>

           <!-- Execute Sonar -->  

       <sonar:sonar />  "Error at this line"

  • Add Namespace as follows to solve the error "The prefix "sonar" for element "sonar:sonar" is not bound."

    xmlns:sonar="antlib:org.sonar.ant.

    No your code should look like.  

    <!-- Define the SonarQube target -->

      <target name="sonar" xmlns:sonar="antlib:org.sonar.ant">

          <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpath="/path/sonar-ant-task.jar">

              <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->

              <classpath path="$ANT_HOME/lib/sonar-ant-task-2.1.jar" />

          </taskdef>

          <!-- Execute Sonar -->  

      <sonar:sonar />

  • Hi Hosam,

    This is a very informative blog for us to understand the extensibility of TFS. Followed the blog to achieve the same but couldn't. their's is an error in Build xml file for executing sonar.

    Error @ <sonar:sonar />. I think we will have to pass some parameters for sonar to execute.

    Any pointers on this error will be helpful for us.

    thanks in advance. :-) :-)

  • Hi Hosam,

    I could implement the target name sonar on build.xml! Perfect!!!

    In my case, I have a "sonar-project.properties" file on the same folder of my App. Can I execute this file with the sonar command?

    On my build server, I have the environment variable called SONAR_RUNNER_HOME that contains the sonar path installation. On the bin folder of this installation, I have a file "sonar-runner.bat". Can I execute this file on build.xml?

    Thanks.

Page 1 of 1 (4 items)