Your SlideShare is downloading. ×
Building better software with maven and sonar
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Building better software with maven and sonar

4,858
views

Published on

This presentation talks about using best practices to build a better Software. …

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.


0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,858
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
159
Comments
0
Likes
6
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Building better software with Maven & Sonar
    By Rohit Ghatol
  • 2. About Me
  • 3. What is Maven?
    For Java
    Build Tool
    Reporting Tool
    Software Distribution
    Project
    Management
  • 4. Maven Quick Demo
  • 5. Requirements for Maven
    JDK 6+
    Maven 2.x
  • 6. mvnarchetype:generate-DarchetypeArtifactId=maven-archetype-quickstart-DinteractiveMode=false-DgroupId=com.mycompany.app-DartifactId=my-app
  • 7. Directory Structure
    com/mycompany/app/App.java
    com/mycompany/app/AppTest.java
  • 8. mvn install
  • 9. Directory Structure
  • 10. Maven Directory Structure
    Directory NameDescription
    src/main/java Application/Library sources
    src/main/resourcesApplication/Library resources
    src/main/filters Resource filter files
    src/main/assembly Assembly descriptors
    src/main/configConfiguration files
    src/main/webappWeb application sources
    src/test/javaTest sources
    src/test/resourcesTest resources
    src/test/filters Test resource filter files
    src/siteSite
    LICENSE.txtProject's license
    NOTICE.txtNotices and attributions required by libraries that the
    project depends on
    README.txtProject's readme
  • 11. Why Maven?
  • 12. But Ma, I have Ant?
  • 13. Project Dependency in Ant
    C:project1
    C:project2
    Project 1
    Project 2
    I only need Project 1, but I still need to checkout Project 2
    C:Project2dist
    Build.xml
    Build.xml
    Using Relative Path
  • 14. Checking in Dependencies
    Snapshot builds
    Project 1
    Project 2
    Project 3
    libs
    libs
    libs
    3rd Party Dependencies are checked in!
    What about CIT?
  • 20. Convention Vs Configuration
    Almost Every one is doing following?
    Is it Time to define a
    HighLevel
    Lifecyle ?
    Clean
    Prepare
    Compile
    Jar
    Test
  • 21. Custom Ant Tasks
    Build Cycle
    Project 1
    Clean
    libs
    When to call the task Life cycle?
    Compile
    • project2.jar
    • 22. servlet-api.jar
    • 23. android-build.jar
    Jar
    Dex
    APT
    What about Reuse?
    What about Distribution?
  • 24. Reporting
    Test Run Report
    Code Coverage Report
    PMD
    Can I get these by default?
    FindBugs
    Change Log
    ……
  • 25. Software Distribution
    Maven
    Spring
    Repo
    Repo
    Repo1
    Repo2
    Repo2
    Repo
    Xyz..
    Synerzip
    Project1
    Internet
    Project2
    Project3
  • 26. Going Deeper into Maven
  • 27. pom.xml
    <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>
  • 28. pom.xml
    <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>
  • 29.
  • 30. Understanding Repositories
    Synerzip
    Central
    Proxy/Internal Repo
    mvn package
    mvn install
    ~/.m2/repository
    mvn deploy
    Maven Project
    Download needed dependencies e.gjunit
    Install the artifact in to local repository (~/.m2/repository
    Push artifact to Internal/Central repository
  • 31. Maven Architecture
    Plugins
    Core
    Maven Core
    compile
    jar
    surefire


    • Parsing Maven XML File
    • 32. Maven LifeCycle
    • 33. Basic Plugins
    compile
    jar
    test
    ….
    ….
  • 34. Maven Concepts
    Goals
    LifeCycle
    mvnarchetype:generate
    mvn install
    mvn <<Plugin>>:<<Goal>>
    mvn <<LifeCycle Phase>>
  • 35. Maven Plugins & Goals
    Plugin
    Goals
    compiler
    jar
    surefire
    jar
    test-jar
    sign
    Sign-verify
    compile
    testCompile
    test
    Command: mvnhelp:describe –Dplugin:jar
  • 36. Maven LifeCycle
    Phases
    Goals
    process-resources
    resources:resources
    compile
    compiler:compile
    process-classes
    process-test-resources
    resources:testResources
    test-compile
    compiler:testCompile
    test
    surefire:test
    prepare-package
    package
    jar:jar
  • 37. 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
  • 38. Standalone Maven Project
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44. Effective POM
  • 45.
  • 46. Make Changes
    Add Employee Model Class
    Add Employee Service (Use Map for persistence)
    Add Employee Test Cases
    Add Reporting Plugins to generate site
  • 47. Maven Site Generation
  • 48. Maven Reporting
    <reporting>
    <plugins>
    <!-- surefire-reports -->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-report-plugin</artifactId>
    <version>2.6</version>
    </plugin>
    <!-- JavaDocPlugin -->
    <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>
  • 49. Maven Site
  • 50. Maven Site
  • 51. Maven Site
  • 52. Maven Site
  • 53. Maven Site
  • 54. Maven Site
  • 55. Maven Site
  • 56. Maven Site
  • 57. Maven Web App Project
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63. Web.xml
  • 64. mvn package tomcat:run-war
    Look Ma, No need to download Tomcat by myself
  • 65.
  • 66. Maven Repositories
  • 67. Repository Options
    Archiva
    Artifactory
    Nexus
  • 68. Internal Maven Repositories
  • 69. <repositories>
    <repository>
    <id>archiva.internal</id>
    <name>ArchivaManagedInternalRepository</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>ArchivaManagedSnapshotRepository</name>
    <url>http://xyz:8080/archiva/repository/snapshots/</url>
    <releases>
    <enabled>false</enabled>
    </releases>
    <snapshots>
    <enabled>true</enabled>
    </snapshots>
    </repository>
    </repositories>
  • 70. Maven Distribution
  • 71. 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>InternalSnapshotRepository</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>
  • 72. Multi Module Projects
  • 73. Multi Module Project
    employee
    <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
    pom.xml
    service
    pom.xml
    webapp
    pom.xml
  • 74. Parent Child Relationship
  • 75. Parent Child Relationship
    employee
    <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>
    pom.xml
    service
    pom.xml
    webapp
    pom.xml
  • 76. Maven Profiles
  • 77. How Often you customize your builds?
    Build Portability
    Production
    Vs
    Development
    Developer
    Vs
    QA
    Manager
    Vs
    Individual Contributer
    Windows
    Vs
    Linux
  • 78. Welcome Maven Profiles
  • 79. Lets Declare a Maven Profile
  • 80.
  • 81.
  • 82.
  • 83. More Details
    http://maven.apache.org/guides/introduction/introduction-to-profiles.html
  • 84. Maven Settings.xml
  • 85. Scenario
    CVS
    Archiva Server
    (Repository Server)
    pom.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>
    <settings>
    ...
    <servers>
    <server>
    <id>deployment.webdav</id>
    <username>{archiva-deployment-user}</username>
    <password>{archiva-deployment-pwd}</password>
    </server>
    ...
    </servers>
    ...
    </settings>
    Where to specify Username and
    Password?
    settings.xml
    pom.xml
    pom.xml
    pom.xml
  • 86. More Details
    • http://maven.apache.org/settings.html
    • 87. http://archiva.apache.org/docs/1.0.1/userguide/deploy.html
  • Sonar
  • 88. What is Sonar?
    Code Quality Analysis tool
  • 89. Install Sonar
    Download Sonar - http://www.sonarsource.org/downloads/
    Change DB Settings if needed
    $>sonar start
    http://xyz:9000
  • 90. Using Sonar with Maven
    Provide Sonar DB Settings in maven’s Settings.xml
    $>mvn clean package sonar:sonar
    Visit http://xyz:9000/
  • 91.
  • 92.
  • 93. Sonar Report for EmployeeService
  • 94. 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
  • 95. Lines of Code/Classes/Method
  • 96. Lines of Code/Classes/Method
    General Demographics about
    Total lines
    Total lines of code
    Total Statements
    Total Packages
    Total Classes
    Total Methods
  • 97. Rules Compliance Index & Violations
  • 98. Rules Compliance Index & Violations
    Compare to PMD, Find Bugs, Code analysis tool
    Violations Categorized into
    Blocker
    Critical
    Major
    Minor
    This all is customizable
  • 99. Comments and Duplicate Code
  • 100. 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?
  • 101. Package Tangle Index
  • 102. Package Tangle Index
    Architectural health
    Detects Cyclic Dependency between Packages
    Shows if Architecture is layered architecture
  • 103. Method/Class Complexity
  • 104. 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
  • 105. LCOM4 and RFC
  • 106. 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
  • 107. 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/
  • 108. RFC – Response for Class
    Total number of methods/constructor invoked as a result of calling the method of a class
  • 109. Code Coverage and Test Results
  • 110. 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
  • 111. Time Machine
  • 112. Time Machine
    Compare any of the metrics over a period of time
    Instant Dashboard of
    What’s improving
    What’s degrading
  • 113. Design
  • 114. Design
  • 115. Design
    Tells about Cyclic Dependencies in Packages
    Tells about the state of Layered Architecture
  • 116. Violations DrillDown
  • 117. Hotspots
  • 118. Hotspots
    One Place to see all risks area
    Drill Down to the problem areas
  • 119. And Many More
    Visit this link for more details http://docs.codehaus.org/display/SONAR/Sonar+Plugin+Library
  • 120. Sample Sonar Reports
    http://nemo.sonarsource.org/
  • 121. References
    Must Own Book for Maven
    http://www.sonarsource.org/
    Buy a Copy Today