Perform Java code analysis with Team Foundation Server and SONAR

Charles Sterling

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!!





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.


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



  • Point to your **build.xml **file


Modify the build script to kick-off SONAR

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



  • The typical configuration should be as below

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


  • Wait for the build to complete


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


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


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:


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.


Discussion is closed.

Feedback usabilla icon