sudhakar teki
sudhakar teki

Reputation: 21

How to integrate SonarQube with SAP Hybris

SAP Hybris Suite comes with integrated PMD Plugin and runs custom PMD Code rulesets.

The PMD Plugin is called through Hybris ANT build script.

But i would like to know if there is a way to integrate Hybris Suite with SonarQube

Upvotes: 2

Views: 7510

Answers (3)

agabrys
agabrys

Reputation: 9126

The sonar target is deprecated. Instead you should use the sonarcheck target. This target by default scans all extensions added to the localextensions.xml file, but you may overwrite this list. All parameters may be configured by using the local.properties file or by passing them by the system properties (parameters prefixed with -D).

Let's say you have 4 extensions in the bin/modules/custom directory. They are named ext1, ext2, ext3 and ext4. Bellow are steps which should be executed to perform SonarScanner analysis with a test code coverage.

Build Software

SonarScanner requires the project to be built (binary files are required).

  1. configure Ant
    cd ${HYBRIS_HOME}/hybris/bin/platform
    . ./setantenv.sh
    
  2. create localextensions.xml
    ant extensionsxml \
          -Dplatform.extensionsgen.filename=${HYBRIS_HOME}/hybris/config/localextensions.xml \
          -Dplatform.extensions=ext1,ext2,ext3,ext4
    
  3. build software
    ant all
    

Execute Tests (+JaCoCo)

The SAP Hybris suite provides two tasks to execute tests:

  • alltests - tests for classes in the main classpath
  • allwebtests - tests for classes in the web context

Both must be executed with additional parameters to generate test code coverage report.

  1. download JaCoCo agent
    cd /tmp
    wget https://repo1.maven.org/maven2/org/jacoco/org.jacoco.agent/0.8.6/org.jacoco.agent-0.8.6-runtime.jar -O jacocoAgent.jar
    cd ${HYBRIS_HOME}/hybris/bin/platform
    
  2. execute alltests
    ant alltests \
          -Dtestclasses.extensions=ext1,ext2,ext3,ext4 \
          -Dtestclasses.reportdir=/tmp/alltests \
          -Dstandalone.javaoptions="-Djava.locale.providers=COMPAT,CLDR -javaagent:/tmp/jacocoAgent.jar=destfile=/tmp/jacocoAlltests.exec"
    
    The java.locale.providers parameter is required to not break tests which uses non ASCII characters in the impex files. It is only needed when the JaCoCo agent is added.
  3. Execute allwebtests
    ant allwebtests \
          -Dtestclasses.extensions=ext1,ext2,ext3,ext4 \
          -Dtestclasses.reportdir=/tmp/allwebtests \
          -Dstandalone.javaoptions="-Djava.locale.providers=COMPAT,CLDR -javaagent:/tmp/jacocoAgent.jar=destfile=/tmp/jacocoAllwebtests.exec"
    

When all tests are done we have to convert the JaCoCo exec files to the XML format.

  1. download JaCoCo cli
    cd /tmp
    wget https://repo1.maven.org/maven2/org/jacoco/org.jacoco.cli/0.8.6/org.jacoco.cli-0.8.6-nodeps.jar -O jacocoCli.jar
    cd ${HYBRIS_HOME}/hybris/bin/platform
    
  2. create XML report
    java -jar /tmp/jacocoCli.jar report \
           /tmp/jacocoAlltests.exec \
           /tmp/jacocoAllwebtests.exec \
           --classfiles ${HYBRIS_HOME}/hybris/bin/modules/custom/ext1 \
           --classfiles ${HYBRIS_HOME}/hybris/bin/modules/custom/ext2 \
           --classfiles ${HYBRIS_HOME}/hybris/bin/modules/custom/ext3 \
           --classfiles ${HYBRIS_HOME}/hybris/bin/modules/custom/ext4 \
           --xml /tmp/jacocoTestReport.xml
    

Execute SonarScanner

There are a few parameters which should be set:

  • sonar.host.url - the SonarQube/SonarCloud server URL
  • sonar.login - the user's token used to authorize to the server
  • sonar.projectKey - the project identifier
  • sonar.projectName - the project display name
  • sonar.projectVersion - the project version
  • sonar.java.source - the Java version required to run the software (required only for versions older than 2105)
  • sonar.extensions - the extensions which should be analyzed (required only for versions older than 2105)
  • sonar.junit.reportPaths - the paths to the test results
  • sonar.test.exclusions - the tests which should be excluded by SonarScanner
  • sonar.coverage.jacoco.xmlReportPaths - the path to the JaCoCo XML report
ant sonarcheck
      -Dsonar.host.url=https://example.org/ \
      -Dsonar.login=1234567890abcdefghijk \
      -Dsonar.projectKey=my.company:custom-extensions \
      -Dsonar.projectName="Custom Extensions" \
      -Dsonar.projectVersion=1.0.0-SNAPSHOT \
      -Dsonar.java.source=11 \
      -Dsonar.extensions=ext1,ext2,ext3,ext4 \
      -Dsonar.junit.reportPaths=/tmp/alltests,/tmp/allwebtests \
      -Dsonar.test.exclusions= \
      -Dsonar.coverage.jacoco.xmlReportPaths=/tmp/jacocoTestReport.xml

Upvotes: 0

Nicolas
Nicolas

Reputation: 1186

Depending on your hybris version, there's an already existing sonar Ant task that will send the metrics to Sonar.

First go to your platform home:

cd $HYBRIS_HOME/hybris/bin/platform

Then, to initialize Ant correctly:

. ./setantenv.sh

Execute tests:

ant unittests

The send the results to Sonar:

ant sonar

Sonar Runner can be configured inside the platform in the config/local.properties file. For example:

sonar.java.source=8
sonar.projectName=Example
sonar.projectKey=example
sonar.exclusions=file:**/gensrc/**, **/ws/axis/*
sonar.login=secret
sonar.password=evenmoresecret
sonar.excludedExtensions=core,testweb,scripting,paymentstandard,mediaweb,maintenanceweb,deliveryzone,commons,processing,impex,validation,catalog,europe1,platformservices,workflow,hac,comments,advancedsavedquery,springintegrationlibs,ldap,hmc,virtualjdbc,cockpit,admincockpit,reportcockpit,platformhmc,productcockpit,customerreview,sapcoretest,sapcoreodata,sapcore,sapcorejco,sapcorejcorec,sapcorebol,advancedexport,backoffice,datahubbackoffice,mcc,wishlist,mediaconversion,solrfacetsearch,solrfacetsearchhmc,voucher,promotions,basecommerce,ticketsystem,cms2,cms2lib,btg,cmscockpit,btgcockpit,b2bcommerce,payment,commerceservices,b2bapprovalprocess,commercewebservicescommons,cscockpit,acceleratorservices,b2bacceleratorservices,acceleratorcms,commercefacades,acceleratorfacades,acceleratorstorefrontcommons,b2bacceleratorfacades,addonsupport,sapcoreaddon,captchaaddon,liveeditaddon,commercesearch,commercesearchhmc,commercesearchbackoffice,datahubadapter,sapcoreconfiguration,sapmodel,sapproductavailability,sapproductavailabilityhmc,b2bsapproductavailability,sappricingbol,sappricinghmc,sappricing,sappricingproductcarouseladdon,b2bsappricing,sapcustomerb2c,sapcustomerb2b,sapmodelhmc,saporderexchange,saporderexchangeb2b,sapcoreconfigurationhmc,sapproducthmc,sapcoreconfigurationbackoffice,multipaymentmode,amazonpay,codpayment,bspay,paypalservice

This is also the place you might want to configure the Sonar instance results need to be sent to.

Upvotes: 4

G. Ann - SonarSource Team
G. Ann - SonarSource Team

Reputation: 22804

Assuming this is Java code we're talking about you can easily "analyze" it with SonarQube. As far as "integrating" it goes, if you mean that you want to run the "custom PMD Code rulesets" via SonarQube, that's should be just a matter of installing the SonarQube PMD plugin and setting up the Quality Profile.

Upvotes: 1

Related Questions