Building better software with maven and sonar

112
Building better software with Maven & Sonar By Rohit Ghatol

description

This presentation talks about using best practices to build a better Software. These practices talk about 1. CIT builds 2. Software Artifact management 3. TDD 4. Getting PMD, Findbugs etc Reports We will talk about how to use Maven to solve problems related to build and improve the build process and product quality. We will also talk about Sonar, a tool which collects and keeps tracks of numerous metrics related to design and code. Sonar can show us on a timline what improved and what declined in last six months, in a measurable manner. Best part is using Maven with Sonar is a breeze and it helps us improve quality significantly.

Transcript of Building better software with maven and sonar

Page 1: Building better software with maven and sonar

Building better software with Maven & Sonar

By Rohit Ghatol

Page 2: Building better software with maven and sonar

About Me

Page 3: Building better software with maven and sonar

What is Maven?

Build Tool Reporting Tool

Software Distribution Project

Management

For Java

Page 4: Building better software with maven and sonar

Maven Quick Demo

Page 5: Building better software with maven and sonar

Requirements for Maven

• JDK 6+• Maven 2.x

Page 6: Building better software with maven and sonar

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false-DgroupId=com.mycompany.app -DartifactId=my-app

Page 7: Building better software with maven and sonar

Directory Structure

Dir

src

main

java resources

test

java resources

pom.xml

com/mycompany/app/App.java com/mycompany/app/AppTest.java

Page 8: Building better software with maven and sonar

mvn install

Page 9: Building better software with maven and sonar

Directory Structure

Dir

src

main

java resources

test

java resources

pom.xml target

my-app-1.0-SNAPSHOT.jar

Page 10: Building better software with maven and sonar

Directory Name Description

src/main/java Application/Library sourcessrc/main/resources Application/Library resourcessrc/main/filters Resource filter filessrc/main/assembly Assembly descriptorssrc/main/config Configuration filessrc/main/webapp Web application sourcessrc/test/java Test sourcessrc/test/resources Test resourcessrc/test/filters Test resource filter filessrc/site SiteLICENSE.txt Project's licenseNOTICE.txt Notices and attributions required by libraries that the

project depends onREADME.txt Project's readme

Maven Directory Structure

Page 11: Building better software with maven and sonar

Why Maven?

Page 12: Building better software with maven and sonar

But Ma, I have Ant?

Page 13: Building better software with maven and sonar

Project Dependency in Ant

Project 1 Project 2

C:\project1 C:\project2

C:\Project2\dist

Build.xml Build.xmlUsing Relative Path

I only need Project 1, but I still need to checkout Project 2

Page 14: Building better software with maven and sonar

Checking in Dependencies

Project 1 Project 2 Project 3

libs libs libs

• project2.jar• servlet-api.jar

• project3.jar• commons.jar

• logging.jar• hamcrest.jar

What about CIT?3rd Party Dependencies are checked in!

Snapshot builds

Page 15: Building better software with maven and sonar

Convention Vs Configuration

Clean

Prepare

Compile

Jar

Test

Almost Every one is doing following?

Is it Time to define a

HighLevel Lifecyle ?

Page 16: Building better software with maven and sonar

Custom Ant TasksProject 1

libs

• project2.jar• servlet-api.jar• android-build.jar

Clean

Compile

Dex

APT

Jar

Build Cycle

What about

Reuse?

What about

Distribution?

When to call the

task Life cycle?

Page 17: Building better software with maven and sonar

Reporting

Test Run Report

Code Coverage Report

PMD

FindBugs

Change Log

……

Can I get these by

default?

Page 18: Building better software with maven and sonar

Software Distribution

Internet

Synerzip

RepoRepo

Repo1

Repo2

Repo2

Repo

Project1

Project2

Project3

SpringMaven

Xyz..

Page 19: Building better software with maven and sonar

Going Deeper into Maven

Page 20: Building better software with maven and sonar

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=”….> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>my-app</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies></project>

pom.xml

Page 21: Building better software with maven and sonar

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=”….> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>my-app</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies></project>

pom.xml

Page 22: Building better software with maven and sonar
Page 23: Building better software with maven and sonar

Understanding Repositories

~/.m2/repository

Maven Project

Central

Proxy/Internal Repo

Synerzip

mvn packageDownload needed dependencies e.g junit

mvn installInstall the artifact in to local repository (~/.m2/repository

mvn deployPush artifact to Internal/Central repository

Page 24: Building better software with maven and sonar

Maven Architecture

Maven Core

• Parsing Maven XML File• Maven LifeCycle• Basic Plugins

compilecompile

jarjar

surefiretest

…….

…….

Core Plugins

Page 25: Building better software with maven and sonar

Maven Concepts

Goals LifeCycle

mvn archetype:generate mvn install

mvn <<Plugin>>:<<Goal>> mvn <<LifeCycle Phase>>

Page 26: Building better software with maven and sonar

Maven Plugins & Goals

Plugin Goals

compilercompiletestCompile

jarjartest-jarsignSign-verify

surefiretest

Command: mvn help:describe –Dplugin:jar

Page 27: Building better software with maven and sonar

Maven LifeCycle

process-resources

compile

process-classes

process-test-resources

test-compile

test

prepare-package

package

Phases

resources:resources

compiler:compile

resources:testResources

compiler:testCompile

surefire:test

jar:jar

Goals

Page 28: Building better software with maven and sonar

mvn clean install

• maven-clean-plugin:2.4.1:clean• maven-resources-plugin:2.4.3:resources• maven-compiler-plugin:2.3.2:compile• maven-resources-plugin:2.4.3:testResources• maven-compiler-plugin:2.3.2:testCompile• maven-surefire-plugin:2.7.2:test• maven-jar-plugin:2.3.1:jar• maven-install-plugin:2.3.1:install

Page 29: Building better software with maven and sonar

Standalone Maven Project

Page 30: Building better software with maven and sonar
Page 31: Building better software with maven and sonar
Page 32: Building better software with maven and sonar
Page 33: Building better software with maven and sonar
Page 34: Building better software with maven and sonar
Page 35: Building better software with maven and sonar

Effective POM

Page 36: Building better software with maven and sonar
Page 37: Building better software with maven and sonar

Make Changes

• Add Employee Model Class• Add Employee Service (Use Map for

persistence)• Add Employee Test Cases• Add Reporting Plugins to generate site

Page 38: Building better software with maven and sonar

Maven Site Generation

Page 39: Building better software with maven and sonar

Maven Reporting<reporting>

<plugins><!-- surefire-reports --><plugin>

<groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-report-plugin</artifactId><version>2.6</version>

</plugin><!-- JavaDoc Plugin --><plugin> <groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-javadoc-plugin</artifactId><version>2.8</version>

</plugin><!-- jxrsource code browsing plugin --><plugin>

<groupId>org.codehaus.mojo</groupId> <artifactId>jxr-maven-plugin</artifactId> <version>2.0-beta-1</version>

</plugin></reporting>

Page 40: Building better software with maven and sonar

Maven Site

Page 41: Building better software with maven and sonar

Maven Site

Page 42: Building better software with maven and sonar

Maven Site

Page 43: Building better software with maven and sonar

Maven Site

Page 44: Building better software with maven and sonar

Maven Site

Page 45: Building better software with maven and sonar

Maven Site

Page 46: Building better software with maven and sonar

Maven Site

Page 47: Building better software with maven and sonar

Maven Site

Page 48: Building better software with maven and sonar

Maven Web App Project

Page 49: Building better software with maven and sonar
Page 50: Building better software with maven and sonar
Page 51: Building better software with maven and sonar
Page 52: Building better software with maven and sonar
Page 53: Building better software with maven and sonar
Page 54: Building better software with maven and sonar

Web.xml

Page 55: Building better software with maven and sonar

mvn package tomcat:run-war

Look Ma, No need to download Tomcat by myself

Page 56: Building better software with maven and sonar
Page 57: Building better software with maven and sonar

Maven Repositories

Page 58: Building better software with maven and sonar

Repository Options

• Archiva• Artifactory• Nexus

Page 59: Building better software with maven and sonar

Internal Maven Repositories

Page 60: Building better software with maven and sonar

<repositories> <repository> <id>archiva.internal</id> <name>Archiva Managed Internal Repository</name> <url>http://xyz:8080/archiva/repository/internal/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>archiva.snapshots</id> <name>Archiva Managed Snapshot Repository</name> <url>http://xyz:8080/archiva/repository/snapshots/</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository>

</repositories>

Page 61: Building better software with maven and sonar

Maven Distribution

Page 62: Building better software with maven and sonar

Distribution Management<distributionManagement> <repository> <id>archiva.internal</id> <name>Internal Release Repository</name> <url>dav:http://xyz:8080/archiva/repository/internal/</url> </repository> <snapshotRepository> <id>archiva.snapshots</id> <name>Internal Snapshot Repository</name> <url>dav:http://xyz:8080/archiva/repository/snapshots/</url> </snapshotRepository> <site> <id>website</id> <url>scp://xyz/var/www/mavensite/MavenTraining</url> </site>

</distributionManagement>

Page 63: Building better software with maven and sonar

Multi Module Projects

Page 64: Building better software with maven and sonar

Multi Module Project

employee

service

webapp

pom.xml

pom.xml

pom.xml

<project ….> <modelVersion>4.0.0</modelVersion> <groupId>com.technext.maven</groupId> <artifactId>parent-project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>service</module> <module>webapp</module> </modules>

</project>

$employee> mvn package[INFO] Scanning for projects...[INFO] Reactor build order: [INFO] Employee Parent Project[INFO] Employee-Backend-Services[INFO] Employee-WebApp

Page 65: Building better software with maven and sonar

Parent Child Relationship

Page 66: Building better software with maven and sonar

Parent Child Relationship

employee

service

webapp

pom.xml

pom.xml

pom.xml

<project …> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>employee-parent</artifactId> <groupId>com.technext.maven.multimodule</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.technext.maven.multimodule</groupId> <artifactId>employee-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Employee-Backend-Services</name></project>

Page 67: Building better software with maven and sonar

Maven Profiles

Page 68: Building better software with maven and sonar

How Often you customize your builds?

Production Vs Development

Developer Vs QA

Manager Vs Individual Contributer

Windows Vs Linux

Build Portability

Page 69: Building better software with maven and sonar

Welcome Maven Profiles

Page 70: Building better software with maven and sonar

Lets Declare a Maven Profile

Page 71: Building better software with maven and sonar
Page 72: Building better software with maven and sonar
Page 73: Building better software with maven and sonar
Page 75: Building better software with maven and sonar

Maven Settings.xml

Page 76: Building better software with maven and sonar

Scenario

Archiva Server(Repository Server)

CVS

pom.xml

pom.xml pom.xmlpom.xml

<project> ... <distributionManagement> <repository> <id>archiva.internal</id> <name>Internal Release Repository</name> <url>dav:http://reposerver.mycompany.com:8080/archiva/repository/internal/</url> </repository> <snapshotRepository> <id>archiva.snapshots</id> <name>Internal Snapshot Repository</name> <url>dav:http://reposerver.mycompany.com:8080/archiva/repository/snapshots/</url> </snapshotRepository> </distributionManagement> ... </project>

Where to specify Username andPassword?

<settings> ... <servers> <server> <id>deployment.webdav</id> <username>{archiva-deployment-user}</username> <password>{archiva-deployment-pwd}</password> </server> ... </servers> ... </settings>

settings.xml

Page 78: Building better software with maven and sonar

Sonar

Page 79: Building better software with maven and sonar

What is Sonar?

Code Quality Analysis tool

Page 80: Building better software with maven and sonar

Install Sonar

• Download Sonar - http://www.sonarsource.org/downloads/

• Change DB Settings if needed• $>sonar start• http://xyz:9000

Page 81: Building better software with maven and sonar

Using Sonar with Maven

• Provide Sonar DB Settings in maven’s Settings.xml

• $>mvn clean package sonar:sonar

• Visit http://xyz:9000/

Page 82: Building better software with maven and sonar
Page 83: Building better software with maven and sonar
Page 84: Building better software with maven and sonar

Sonar Report for EmployeeService

Page 85: Building better software with maven and sonar

Basic Metrics (Starter pack)

• Lines of Code/Classes/Methods• Rules Compliance Index & Violations• Comments and Duplicate Code• Package Tangle Index• Method/Class Complexity (Cyclometric)• LCOM4 and RFC• Code Coverage and Test Results

Page 86: Building better software with maven and sonar

Lines of Code/Classes/Method

Page 87: Building better software with maven and sonar

Lines of Code/Classes/Method

• General Demographics about– Total lines– Total lines of code– Total Statements– Total Packages– Total Classes– Total Methods

Page 88: Building better software with maven and sonar

Rules Compliance Index & Violations

Page 89: Building better software with maven and sonar

Rules Compliance Index & Violations

• Compare to PMD, Find Bugs, Code analysis tool

• Violations Categorized into– Blocker– Critical– Major– Minor

• This all is customizable

Page 90: Building better software with maven and sonar

Comments and Duplicate Code

Page 91: Building better software with maven and sonar

Comments and Duplicate Code

• How many public APIs are documented?• How many APIs are undocumented?• How much of code is commented?• How much code is duplicated?

Page 92: Building better software with maven and sonar

Package Tangle Index

Page 93: Building better software with maven and sonar

Package Tangle Index

• Architectural health• Detects Cyclic Dependency between Packages• Shows if Architecture is layered architecture

Page 94: Building better software with maven and sonar

Method/Class Complexity

Page 95: Building better software with maven and sonar

Method/Class Complexity

• Cyclometric Complexity• Complexity is 1 for empty function• Add 1 for every block• Addition of all this is complexity of the

method• More complex the method harder to test• Default complexity level = 10

Page 96: Building better software with maven and sonar

LCOM4 and RFC

Page 97: Building better software with maven and sonar

LCOM4

• Lets Start with SOLID Design Principle– S = Single Responsibility Principle

• A Class should have only one responsibility• If Class has more than one– Then break the class into smaller classes

• This ensures– Modularity– Reusability

Page 98: Building better software with maven and sonar

How to measure LCOM4

• If a class as 2+ sets of method totally disjoint, then we can very much say class has 2 responsibility

• http://www.sonarsource.org/clean-up-design-at-class-level-with-sonar/

Page 99: Building better software with maven and sonar

RFC – Response for Class

• Total number of methods/constructor invoked as a result of calling the method of a class

Page 100: Building better software with maven and sonar

Code Coverage and Test Results

Page 101: Building better software with maven and sonar

Code Coverage and Test Results

• Code Coverage is the paths of code covered by unit test

• Test Results is how many test cases passed or fail

Page 102: Building better software with maven and sonar

Time Machine

Page 103: Building better software with maven and sonar

Time Machine

• Compare any of the metrics over a period of time

• Instant Dashboard of – What’s improving– What’s degrading

Page 104: Building better software with maven and sonar

Design

Page 105: Building better software with maven and sonar

Design

Page 106: Building better software with maven and sonar

Design

• Tells about Cyclic Dependencies in Packages• Tells about the state of Layered Architecture

Page 107: Building better software with maven and sonar

Violations DrillDown

Page 108: Building better software with maven and sonar

Hotspots

Page 109: Building better software with maven and sonar

Hotspots

• One Place to see all risks area• Drill Down to the problem areas

Page 111: Building better software with maven and sonar

Sample Sonar Reports

• http://nemo.sonarsource.org/