Integrating Check-style and code coverage plugin in AEM codebase

13 / Oct / 2023 by Medha Kohli 0 comments

Introduction

As developers, we strive to maintain high code quality and ensure that our software is both functional and maintainable. Two essential tools for achieving this are Checkstyle and code coverage analysis. In this blog post, we’ll explore how to integrate these plugins into your Adobe Experience Manager (AEM) codebase to boost your code quality and ensure comprehensive testing coverage.

What is Checkstyle?

Checkstyle is a static code analysis tool that enforces coding conventions and best practices in your Java code. It checks your source code against predefined rules to ensure consistency and adherence to coding standards.

Why Code Coverage Matters

Code coverage analysis measures how much of your codebase is exercised by your tests. It helps identify untested or poorly tested code, reducing the risk of undetected bugs and ensuring your application behaves as expected.

Integrating Checkstyle in AEM

Integrating Checkstyle into your AEM project is straightforward and can significantly improve code quality. Here’s how to do it:

Step 1: Setting Up Your Project

First, we need to set up our AEM project. You can use the AEM Project Archetype to generate a new project. Run the following command in your terminal:

mvn -B archetype:generate \
 -D archetypeGroupId=com.adobe.aem \
 -D archetypeArtifactId=aem-project-archetype \
 -D archetypeVersion=25 \
 -D appTitle="My Site" \
 -D appId="mysite" \
 -D groupId="com.mysite" \
 -D aemVersion=6.5.5

Step 2: Define Your Checkstyle Configuration

  • Create a Checkstyle configuration file (e.g., custom-checkstyle.xml) or use an existing one.
  • Define your coding rules and standards in the configuration file. To achieve this you can create a new folder under the project you created and name it as code-analysis. create a new pom.xml file inside this folder and add below XML code to it.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mysite</groupId>
<artifactId>static-analysis</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

Folder structure for reference :

folder-structure

Step 3: Add your custom/default check style

Note: In case you were confused which check style configuration to choose

  • Google Java Style: Google’s Java Style Guide is well-regarded in the industry, and they provide a Checkstyle configuration that enforces their coding standards. It’s known for its clarity and consistency. You can find it here
  • Sun/Oracle Checkstyle Configuration: This configuration is based on the Java Coding Style Guide by Sun Microsystems (now Oracle). It enforces many widely accepted Java coding conventions. You can find it here
  • You can customise the configuration as per your projects requirements.

Step 4: Add the code-analysis folder in the allowed modules in project’s root pom.xml

pom-allow-module

Step 5: Add the Maven Checkstyle Plugin in POM

  • Open your project’s root pom.xml file.
  • Add the Maven Checkstyle Plugin configuration to your <build><plugins> section.
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.1.2</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>checkstyle</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <skip>false</skip>
        <failsOnError>true</failsOnError>
        <consoleOutput>true</consoleOutput>
        <linkXRef>true</linkXRef>
        <configLocation>../code-analysis/resources/custom-checkstyle.xml</configLocation>
        <includeResources>false</includeResources>
        <includeTestResources>false</includeTestResources>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>com.mysite</groupId>
            <artifactId>code-analysis</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>8.41</version>
        </dependency>
    </dependencies>
</plugin>

Note:

  1. If you dont want your build to fail on errors, set the <failsOnError> flag to false.
  2. You can either add below code in pom or mention this command at run time – mvn clean install checkstyle:checkstyle to run your checkstyle

Step 6: You can now run mvn clean install and checkstyle will report violations in your code according to the rules you’ve defined.

You can check the error in the report generated at ../mysite/core/target/checkstyle-result.xml

blog-error-on-build

Integrating Code Coverage Analysis

To ensure comprehensive code coverage, you can integrate a code coverage plugin like JaCoCo:

What is JaCoCo?

JaCoCo, short for Java Code Coverage, is an open-source code coverage tool for Java applications. Code coverage tools like JaCoCo help developers measure how much of their source code is executed by their tests.

Adding the JaCoCo Plugin

  • Open your /core/pom.xml file.
  • Add the below JaCoCo Maven Plugin configuration to your <build><plugins> section.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>CLASS</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.70</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.70</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>

Note: You can set code coverage according to what your project’s requirements are. In this context, I have chosen to set a code coverage target of 70%, which aligns with our project’s quality and testing goals.

You can view the code coverage report at ../core/target/site/jacoco/index.html

Screenshot-jacoco

References

  1. https://github.com/adobe/aem-project-archetype/blob/master/VERSIONS.md
  2. https://checkstyle.sourceforge.io/version/8.40/checks.html
  3. https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/sun_checks.xml
  4. https://www.eclemma.org/jacoco/trunk/doc/check-mojo.html#rules
FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *