Top Banner
Building better software with Maven & Sonar By Rohit Ghatol
112

Building better software with Maven & Sonar

Feb 25, 2016

Download

Documents

hera

Building better software with Maven & Sonar. By Rohit Ghatol. About Me. For Java. Build Tool. Reporting Tool. What is Maven?. Software Distribution. Project Management. Maven Quick Demo. Requirements for Maven. JDK 6+ Maven 2.x. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Building better software with Maven & Sonar

Building better software with Maven & Sonar

By Rohit Ghatol

Page 2: Building better software with Maven & Sonar

About Me

Page 3: Building better software with Maven & Sonar

What is Maven?

Build Tool Reporting Tool

Software Distribution Project

Management

For Java

Page 4: Building better software with Maven & Sonar

Maven Quick Demo

Page 5: Building better software with Maven & Sonar

Requirements for Maven

• JDK 6+• Maven 2.x

Page 6: Building better software with Maven & Sonar

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

Page 7: Building better software with Maven & 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 & Sonar

mvn install

Page 9: Building better software with Maven & 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 & 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 & Sonar

Why Maven?

Page 12: Building better software with Maven & Sonar

But Ma, I have Ant?

Page 13: Building better software with Maven & 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 & 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 & 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 & 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 & 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 & Sonar

Software Distribution

Internet

Synerzip

RepoRepo

Repo1

Repo2

Repo2

Repo

Project1

Project2

Project3

SpringMaven

Xyz..

Page 19: Building better software with Maven & Sonar

Going Deeper into Maven

Page 20: Building better software with Maven & 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 & 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 & Sonar
Page 23: Building better software with Maven & 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 & 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 & Sonar

Maven Concepts

Goals LifeCycle

mvn archetype:generate mvn install

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

Page 26: Building better software with Maven & Sonar

Maven Plugins & Goals

Plugin Goals

compilercompiletestCompile

jarjartest-jarsignSign-verify

surefiretest

Command: mvn help:describe –Dplugin:jar

Page 27: Building better software with Maven & 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 & 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 & Sonar

Standalone Maven Project

Page 30: Building better software with Maven & Sonar
Page 31: Building better software with Maven & Sonar
Page 32: Building better software with Maven & Sonar
Page 33: Building better software with Maven & Sonar
Page 34: Building better software with Maven & Sonar
Page 35: Building better software with Maven & Sonar

Effective POM

Page 36: Building better software with Maven & Sonar
Page 37: Building better software with Maven & 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 & Sonar

Maven Site Generation

Page 39: Building better software with Maven & 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 & Sonar

Maven Site

Page 41: Building better software with Maven & Sonar

Maven Site

Page 42: Building better software with Maven & Sonar

Maven Site

Page 43: Building better software with Maven & Sonar

Maven Site

Page 44: Building better software with Maven & Sonar

Maven Site

Page 45: Building better software with Maven & Sonar

Maven Site

Page 46: Building better software with Maven & Sonar

Maven Site

Page 47: Building better software with Maven & Sonar

Maven Site

Page 48: Building better software with Maven & Sonar

Maven Web App Project

Page 49: Building better software with Maven & Sonar
Page 50: Building better software with Maven & Sonar
Page 51: Building better software with Maven & Sonar
Page 52: Building better software with Maven & Sonar
Page 53: Building better software with Maven & Sonar
Page 54: Building better software with Maven & Sonar

Web.xml

Page 55: Building better software with Maven & Sonar

mvn package tomcat:run-war

Look Ma, No need to download Tomcat by myself

Page 56: Building better software with Maven & Sonar
Page 57: Building better software with Maven & Sonar

Maven Repositories

Page 58: Building better software with Maven & Sonar

Repository Options

• Archiva• Artifactory• Nexus

Page 59: Building better software with Maven & Sonar

Internal Maven Repositories

Page 60: Building better software with Maven & 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 & Sonar

Maven Distribution

Page 62: Building better software with Maven & 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 & Sonar

Multi Module Projects

Page 64: Building better software with Maven & 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 & Sonar

Parent Child Relationship

Page 66: Building better software with Maven & 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 & Sonar

Maven Profiles

Page 68: Building better software with Maven & 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 & Sonar

Welcome Maven Profiles

Page 70: Building better software with Maven & Sonar

Lets Declare a Maven Profile

Page 71: Building better software with Maven & Sonar
Page 72: Building better software with Maven & Sonar
Page 73: Building better software with Maven & Sonar
Page 75: Building better software with Maven & Sonar

Maven Settings.xml

Page 76: Building better software with Maven & 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 & Sonar

Sonar

Page 79: Building better software with Maven & Sonar

What is Sonar?

Code Quality Analysis tool

Page 80: Building better software with Maven & 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 & 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 & Sonar
Page 83: Building better software with Maven & Sonar
Page 84: Building better software with Maven & Sonar

Sonar Report for EmployeeService

Page 85: Building better software with Maven & 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 & Sonar

Lines of Code/Classes/Method

Page 87: Building better software with Maven & 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 & Sonar

Rules Compliance Index & Violations

Page 89: Building better software with Maven & 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 & Sonar

Comments and Duplicate Code

Page 91: Building better software with Maven & 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 & Sonar

Package Tangle Index

Page 93: Building better software with Maven & Sonar

Package Tangle Index

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

Page 94: Building better software with Maven & Sonar

Method/Class Complexity

Page 95: Building better software with Maven & 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 & Sonar

LCOM4 and RFC

Page 97: Building better software with Maven & 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 & 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 & 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 & Sonar

Code Coverage and Test Results

Page 101: Building better software with Maven & 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 & Sonar

Time Machine

Page 103: Building better software with Maven & 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 & Sonar

Design

Page 105: Building better software with Maven & Sonar

Design

Page 106: Building better software with Maven & Sonar

Design

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

Page 107: Building better software with Maven & Sonar

Violations DrillDown

Page 108: Building better software with Maven & Sonar

Hotspots

Page 109: Building better software with Maven & Sonar

Hotspots

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

Page 111: Building better software with Maven & Sonar

Sample Sonar Reports

• http://nemo.sonarsource.org/