beyond the basics of sonarqube : improve your java(script) code even further

94
Beyond the Basics of SonarQube: Improve Your Java(Script) Code Even Further Johan Janssen (Info Support)

Upload: acacia

Post on 08-Jan-2016

39 views

Category:

Documents


1 download

DESCRIPTION

Beyond the Basics of SonarQube : Improve Your Java(Script) Code Even Further. Johan Janssen (Info Support). Agenda. SonarQube basic features Analyzing data Code review Hunting bad design and architecture Testing Other languages Plugins Using SonarQube on existing projects - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Beyond the Basics of SonarQube: Improve

Your Java(Script) Code Even Further

Johan Janssen (Info Support)

Page 2: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Agenda SonarQube basic features Analyzing data Code review Hunting bad design and architecture Testing Other languages Plugins Using SonarQube on existing projects Tips / summary Questions

Page 3: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Quality for thousands of years

Page 4: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Quality after years of improvements

Page 5: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Quality for 39 days

Page 6: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Bugs Microsoft Applications: "about 10 - 20

defects per 1000 lines of code during in-house testing, and 0.5 defect per 1000 lines of code in released product (Dave Moore 1992).“

Page 7: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Bugs NASA's Spirit rover became unresponsive on

January 21, 2004, a few weeks after landing on Mars. (Wikipedia)

In January 2009, Google's search engine erroneously notified users that every web site world wide was potentially malicious, including its own. (Wikipedia)

Page 8: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Find them as fast as possible

Page 9: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Why should I use SonarQube? I’m told to use it. We need to achieve certain results (SIG…) I want to improve my coding standards

Page 10: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

SonarQube Platform to manage code quality Open source, possible to pay for support

and some plugins Since 2006; now they have 200 customers

and SonarQube is used in 15.000 organizations

Active community: support, plugins, books

Page 11: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

SonarQube2013: 5 releases of SonarQube platform 130 releases of ecosystem products 75,000 downloads of SonarQube 13,000+ messages on mailing lists

Page 12: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

SonarQube architecture

Page 13: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

SonarQube basic features

Page 14: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further
Page 15: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Dashboard with ‘time changes’

Page 16: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Configure dashboards

Page 17: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Configure dashboards

Page 18: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further
Page 19: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further
Page 20: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Quality Gates

Page 21: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Notifications

Page 22: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Analysing data

Page 23: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Compare

Page 24: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Measures

Page 25: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Measure Filter as Bubble Chart

Page 26: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Filter Motion Chart

Page 27: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

What is good code?

Page 28: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Code review

Page 29: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Roles

Page 30: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Add code review

Page 31: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Why code reviews?

“We all need people who will give us feedback. That's how we improve.”

- Bill Gates

Page 32: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Hunting bad design and architecture

Page 33: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Hunting bad design and architecture

Page 34: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Architectural constraints

Page 35: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Architectural constraints

Page 36: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Package tangle index

Page 37: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Package tangle index

Page 38: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Testing

Page 39: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Now manual execution of unit tests

mvn clean installmvn sonar:sonar

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install

-Dmaven.test.failure.ignore=truemvn sonar:sonar

Page 40: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Coverage per test

Page 41: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Coverage per test Configure the POM (see next slide) Activate profile to get coverage per test

informationmvn org.jacoco:jacoco-maven-

plugin:prepare-agent clean install -Pcoverage-per-test

Analyze the projectmvn sonar:sonar

Page 42: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further
Page 43: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Coverage per test workspace

Page 44: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Integration testing

Page 45: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Integration testing Add integration test coverage widget to

SonarQube Download Jacoco agent Configuration for Tomcat’s catalina.bat:set JACOCO=-javaagent:$path$\lib\jacocoagent.jar,destfile=$resultpath$\jacoco.exec,append=false,includes=com.dockerpi.*set JAVA_OPTS=%JAVA_OPTS% %JACOCO%

Page 46: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Integration testing Start Tomcat Execute tests (manual, integration,

performance…) Stop Tomcat and execute SonarQube

analysismvn clean install sonar:sonar

-Dsonar.dynamicAnalysis=reuseReports

-Dsonar.jacoco.itReportPath=$resultpath$\jacoco.exec

Page 47: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Unit and integration testing combined

Page 48: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

PMD rules for unit testing

Page 49: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

PMD rules for unit testing

Page 50: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

PMD rules for unit testing

Page 51: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further
Page 52: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Other languages

Page 53: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

JavaScript

Page 54: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

JavaScript

Page 55: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

CSS

Page 56: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

CSS

Page 57: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Web plugin

Page 58: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Plugins

Page 59: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Pitest plugin

Page 60: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Pitest plugin Enable ‘Survived mutant’ rule in

SonarQube Quality Profile Configure Pitest for instance with Maven

(see next slides)

Page 61: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Pitest plugin Execute tests and send results to

SonarQubemvn org.pitest:pitest-maven:mutationCoveragemvn sonar:sonar

-Dsonar.pitest.mode=reuseReport

Page 62: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Pitest plugin Maven configuration inside the build/plugins section<plugin>  <groupId>org.pitest</groupId>  <artifactId>pitest-maven</artifactId>  <version>LATEST</version>  <configuration>    <inScopeClasses>      <param>com.example*</param>    </inScopeClasses>    <targetClasses>      <param>com.example*</param>    </targetClasses>    <outputFormats>      <outputFormat>XML</outputFormat>     </outputFormats>  </configuration></plugin>

Page 63: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Pitest plugin

Page 64: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Build breaker plugin[INFO] [15:15:57.671] Executing post-job class org.sonar.plugins.buildbreaker.AlertBreaker[ERROR] [15:15:57.673] [BUILD BREAKER] Coverage < 80[ERROR] [15:15:57.673] [BUILD BREAKER] Critical issues > 0[ERROR] [15:15:57.673] [BUILD BREAKER] Major issues > 0[ERROR] Alert thresholds have been hit (3 times).[INFO] ------------------------------------------------------------------------[INFO] BUILD FAILURE[INFO] ------------------------------------------------------------------------[INFO] Total time: 8.169s[INFO] Finished at: Sat Aug 30 15:15:57 CEST 2014[INFO] Final Memory: 21M/234M[INFO] ------------------------------------------------------------------------[ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.4:sonar (default-cli) on project DockerPiExample: Alert thresholds have been hit (3 times)

Page 65: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

(Eclipse) IDE integration Supports incremental analyses (default) Install plugin through Eclipse Marketplace Configure SonarQube server:

o Window -> Preferences -> SonarQube -> Servers

o Right click on your project in the Project Explorer

o Configure -> Associate with SonarQube

Page 66: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Eclipse SonarQube Issues

Page 67: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Eclipse SonarQube Issues Editor

Page 68: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Eclipse SonarQube Web Browser

Page 69: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

SCM Activity plugin

Page 70: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Build Stability plugin

Page 71: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Build Stability plugin

Page 72: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Tab metrics plugin

Page 73: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Mojo Bridge Plugin (Development)

Page 74: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Mojo Bridge Plugin (Development)

Page 75: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Leftovers

Page 76: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Libraries

Page 77: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Branches mvn sonar:sonar -Dsonar.branch=4.4

Page 78: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Exclusions Ignore files Ignore issues Ignore duplications Ignore code coverage Example:mvn sonar:sonar -Dsonar.exclusions=

src/main/webapp/lib/*.js

Page 79: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Duplications across projects

Page 80: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Duplications across projects

Page 81: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Complexity

Page 82: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Technical debt pyramid

Page 83: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

LCOM4 / RFC

Page 84: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Using SonarQube on existing projects

Page 85: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Using SonarQube on existing projects

Page 86: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Using SonarQube on existing projects

Identity hotspots Use action plans Maybe use a less strict quality profile Add tasks/stories in the sprint to improve

quality Monitor quality of new code.

o Cutoff plugin

Page 87: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Hotspots

Page 88: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Planning issues

Page 89: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Issues

Page 90: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Tips for using SonarQube in a project

Create stories/tasks to improve the quality Perform manual code reviews Agree on a standard for items SonarQube

cannot check Fix items or mark them as false positive Use separate logins Settings/reviews are project specific

Page 91: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Tips for using SonarQube in a project

Options to use SonarQubeo Central server (commit AND nightly)o IDE plugin (for instance Eclipse plugin)o Local in your development environment

Check code quality before ‘To verify’ stepo First merge your code so it is up to date

Verifier should also verify the quality

Page 92: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further
Page 93: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Accept failure, but improve continuously

“Once we accept our limits, we go beyond them.”

- Albert Einstein

Page 94: Beyond the Basics of  SonarQube : Improve Your Java(Script) Code Even  Further

Questions Ask me! SonarQube dev /user mailinglist Sonarqube.org Nemo example dashboard SonarQube books

[email protected]