{"id":59143,"date":"2023-10-13T16:30:15","date_gmt":"2023-10-13T11:00:15","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=59143"},"modified":"2024-06-10T15:35:46","modified_gmt":"2024-06-10T10:05:46","slug":"integrating-check-style-and-code-coverage-plugin-in-aem-codebase","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-check-style-and-code-coverage-plugin-in-aem-codebase\/","title":{"rendered":"Integrating Check-style and code coverage plugin in AEM codebase"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">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&#8217;ll explore how to integrate these plugins into your Adobe Experience Manager (AEM) codebase to boost your code quality and ensure comprehensive testing coverage.<\/span><\/p>\n<h2><strong>What is Checkstyle?<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<h2><strong>Why Code Coverage Matters<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<h2><strong>Integrating Checkstyle in AEM<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Integrating Checkstyle into your AEM project is straightforward and can significantly improve code quality. Here&#8217;s how to do it:<\/span><\/p>\n<h4><span style=\"font-weight: 400;\"><strong>Step 1<\/strong>: Setting Up Your Project<\/span><\/h4>\n<p><span style=\"font-weight: 400;\">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:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">mvn -B archetype:generate \\<\/span><span style=\"font-weight: 400;\">\r\n<\/span><span style=\"font-weight: 400;\"> -D archetypeGroupId=com.adobe.aem \\<\/span><span style=\"font-weight: 400;\">\r\n<\/span><span style=\"font-weight: 400;\"> -D archetypeArtifactId=aem-project-archetype \\<\/span><span style=\"font-weight: 400;\">\r\n<\/span><span style=\"font-weight: 400;\"> -D archetypeVersion=25 \\<\/span><span style=\"font-weight: 400;\">\r\n<\/span><span style=\"font-weight: 400;\"> -D appTitle=\"My Site\" \\<\/span><span style=\"font-weight: 400;\">\r\n<\/span><span style=\"font-weight: 400;\"> -D appId=\"mysite\" \\<\/span><span style=\"font-weight: 400;\">\r\n<\/span><span style=\"font-weight: 400;\"> -D groupId=\"com.mysite\" \\<\/span><span style=\"font-weight: 400;\">\r\n<\/span><span style=\"font-weight: 400;\"> -D aemVersion=6.5.5<\/span><\/pre>\n<h4><span style=\"font-weight: 400;\"><strong>Step 2<\/strong>: Define Your Checkstyle Configuration<\/span><\/h4>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Create a Checkstyle configuration file (e.g., custom-checkstyle.xml) or use an existing one.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">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 <\/span><b>code-analysis. <\/b><span style=\"font-weight: 400;\">create a new pom.xml file inside this folder and add below XML code to it.<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;<\/span>\r\n<b>&lt;<\/b><b>project <\/b><span style=\"font-weight: 400;\">xmlns<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">\"<\/span><a href=\"http:\/\/maven.apache.org\/POM\/4.0.0\"><span style=\"font-weight: 400;\">http:\/\/maven.apache.org\/POM\/4.0.0<\/span><\/a><span style=\"font-weight: 400;\">\"<\/span>\r\n<span style=\"font-weight: 400;\">xmlns:xsi<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"<\/span>\r\n<span style=\"font-weight: 400;\">xsi:schemaLocation<\/span><span style=\"font-weight: 400;\">=<\/span><span style=\"font-weight: 400;\">\"http:\/\/maven.apache.org\/POM\/4.0.0<\/span>\r\n<a href=\"http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"><span style=\"font-weight: 400;\">http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd<\/span><\/a><span style=\"font-weight: 400;\">\"<\/span><b>&gt;<\/b>\r\n<b>&lt;<\/b><b>modelVersion<\/b><b>&gt;<\/b><span style=\"font-weight: 400;\">4.0.0<\/span><b>&lt;\/<\/b><b>modelVersion<\/b><b>&gt;<\/b>\r\n<b>&lt;<\/b><b>groupId<\/b><b>&gt;<\/b><span style=\"font-weight: 400;\">com.mysite<\/span><b>&lt;\/<\/b><b>groupId<\/b><b>&gt;<\/b>\r\n<b>&lt;<\/b><b>artifactId<\/b><b>&gt;<\/b><span style=\"font-weight: 400;\">static-analysis<\/span><b>&lt;\/<\/b><b>artifactId<\/b><b>&gt;<\/b>\r\n<b>&lt;<\/b><b>version<\/b><b>&gt;<\/b><span style=\"font-weight: 400;\">1.0.0-SNAPSHOT<\/span><b>&lt;\/<\/b><b>version<\/b><b>&gt;<\/b>\r\n<b>&lt;<\/b><b>properties<\/b><b>&gt;<\/b>\r\n<b>&lt;<\/b><b>project.build.sourceEncoding<\/b><b>&gt;<\/b><span style=\"font-weight: 400;\">UTF-8<\/span><b>&lt;\/<\/b><b>project.build.sourceEncoding<\/b><b>&gt;<\/b>\r\n<b>&lt;\/<\/b><b>properties<\/b><b>&gt;<\/b>\r\n<b>&lt;\/<\/b><b>project<\/b><b>&gt;<\/b><\/pre>\n<p><b>Folder structure for reference : <\/b><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58827 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/blog-folder-structure.jpg\" alt=\"folder-structure\" width=\"244\" height=\"136\" \/><\/p>\n<h4><span style=\"font-weight: 400;\"><strong>Step 3<\/strong>: Add your custom\/default check style<\/span><\/h4>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">To integrate default sun checks, copy and paste the file in custom-checkstyle.xml &#8211;<\/span><a href=\"https:\/\/github.com\/checkstyle\/checkstyle\/blob\/checkstyle-8.41\/src\/main\/resources\/sun_checks.xml\"> <span style=\"font-weight: 400;\">https:\/\/github.com\/checkstyle\/checkstyle\/blob\/checkstyle-8.41\/src\/main\/resources\/sun_checks.xml<\/span><\/a><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">To integrate google checks, refer &#8211;<\/span><a href=\"https:\/\/github.com\/checkstyle\/checkstyle\/blob\/checkstyle-8.41\/src\/main\/resources\/google_checks.xml\"> <span style=\"font-weight: 400;\">https:\/\/github.com\/checkstyle\/checkstyle\/blob\/checkstyle-8.41\/src\/main\/resources\/google_checks.xml<\/span><\/a><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\"><strong>Note: In case you were confused which check style configuration to choose<\/strong> <\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><strong>Google Java Style:<\/strong> Google&#8217;s Java Style Guide is well-regarded in the industry, and they provide a Checkstyle configuration that enforces their coding standards. It&#8217;s known for its clarity and consistency. You can find it <\/span><a href=\"https:\/\/github.com\/checkstyle\/checkstyle\/blob\/master\/src\/main\/resources\/google_checks.xml\"><span style=\"font-weight: 400;\">here<\/span><\/a><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><strong>Sun\/Oracle Checkstyle Configuration<\/strong>: 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 <\/span><a href=\"https:\/\/github.com\/checkstyle\/checkstyle\/blob\/master\/src\/main\/resources\/sun_checks.xml\"><span style=\"font-weight: 400;\">here<\/span><\/a><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">You can customise the configuration as per your projects requirements.<\/span><\/li>\n<\/ul>\n<h4><span style=\"font-weight: 400;\"><strong>Step 4<\/strong>: Add the code-analysis folder in the allowed modules in project&#8217;s root pom.xml<\/span><\/h4>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58829 size-medium\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/pom-allow-module-300x208.jpg\" alt=\"pom-allow-module\" width=\"300\" height=\"208\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/pom-allow-module-300x208.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/09\/pom-allow-module.jpg 366w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<h4><span style=\"font-weight: 400;\"><strong>Step 5:<\/strong> Add the Maven Checkstyle Plugin in POM<\/span><\/h4>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Open your project&#8217;s root pom.xml file.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Add the Maven Checkstyle Plugin configuration to your &lt;build&gt;&lt;plugins&gt; section.<\/span><\/li>\n<\/ul>\n<pre>&lt;plugin&gt;\r\n    &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;maven-checkstyle-plugin&lt;\/artifactId&gt;\r\n    &lt;version&gt;3.1.2&lt;\/version&gt;\r\n    &lt;executions&gt;\r\n        &lt;execution&gt;\r\n            &lt;phase&gt;package&lt;\/phase&gt;\r\n            &lt;goals&gt;\r\n                &lt;goal&gt;checkstyle&lt;\/goal&gt;\r\n            &lt;\/goals&gt;\r\n        &lt;\/execution&gt;\r\n    &lt;\/executions&gt;\r\n    &lt;configuration&gt;\r\n        &lt;skip&gt;false&lt;\/skip&gt;\r\n        &lt;failsOnError&gt;true&lt;\/failsOnError&gt;\r\n        &lt;consoleOutput&gt;true&lt;\/consoleOutput&gt;\r\n        &lt;linkXRef&gt;true&lt;\/linkXRef&gt;\r\n        &lt;configLocation&gt;..\/code-analysis\/resources\/custom-checkstyle.xml&lt;\/configLocation&gt;\r\n        &lt;includeResources&gt;false&lt;\/includeResources&gt;\r\n        &lt;includeTestResources&gt;false&lt;\/includeTestResources&gt;\r\n    &lt;\/configuration&gt;\r\n    &lt;dependencies&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;com.mysite&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;code-analysis&lt;\/artifactId&gt;\r\n            &lt;version&gt;1.0.0-SNAPSHOT&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;com.puppycrawl.tools&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;checkstyle&lt;\/artifactId&gt;\r\n            &lt;version&gt;8.41&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n    &lt;\/dependencies&gt;\r\n&lt;\/plugin&gt;<\/pre>\n<p><span style=\"font-weight: 400;\">Note: <\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">If you dont want your build to fail on errors, set the &lt;failsOnError&gt; flag to false.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">You can either add below code in pom or mention this command at run time &#8211; <\/span><b>mvn clean install checkstyle:checkstyle<\/b><span style=\"font-weight: 400;\"> to run your checkstyle<\/span><\/li>\n<\/ol>\n<h4><span style=\"font-weight: 400;\"><strong>Step 6:<\/strong> You can now run <strong>mvn clean install<\/strong> and checkstyle will report violations in your code according to the rules you&#8217;ve defined. <\/span><\/h4>\n<p><span style=\"font-weight: 400;\">You can check the error in the report generated at ..\/mysite\/core\/target\/checkstyle-result.xml<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-58830 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/blog-error-on-build.jpg\" alt=\"blog-error-on-build\" width=\"954\" height=\"386\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/09\/blog-error-on-build.jpg 954w, \/blog\/wp-ttn-blog\/uploads\/2023\/09\/blog-error-on-build-300x121.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/09\/blog-error-on-build-768x311.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/09\/blog-error-on-build-624x252.jpg 624w\" sizes=\"(max-width: 954px) 100vw, 954px\" \/><\/p>\n<h3><strong>Integrating Code Coverage Analysis<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">To ensure comprehensive code coverage, you can integrate a code coverage plugin like JaCoCo:<\/span><\/p>\n<h4><strong>What is JaCoCo?<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">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. <\/span><\/p>\n<h4><strong>Adding the JaCoCo Plugin<\/strong><\/h4>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Open your \/core\/pom.xml file.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Add the below JaCoCo Maven Plugin configuration to your &lt;build&gt;&lt;plugins&gt; section.<\/span><\/li>\n<\/ul>\n<pre>&lt;plugin&gt;\r\n&lt;groupId&gt;org.jacoco&lt;\/groupId&gt;\r\n&lt;artifactId&gt;jacoco-maven-plugin&lt;\/artifactId&gt;\r\n&lt;version&gt;0.8.6&lt;\/version&gt;\r\n&lt;executions&gt;\r\n&lt;execution&gt;\r\n&lt;id&gt;default-prepare-agent&lt;\/id&gt;\r\n&lt;goals&gt;\r\n&lt;goal&gt;prepare-agent&lt;\/goal&gt;\r\n&lt;\/goals&gt;\r\n&lt;\/execution&gt;\r\n&lt;execution&gt;\r\n&lt;id&gt;default-report&lt;\/id&gt;\r\n&lt;phase&gt;prepare-package&lt;\/phase&gt;\r\n&lt;goals&gt;\r\n&lt;goal&gt;report&lt;\/goal&gt;\r\n&lt;\/goals&gt;\r\n&lt;\/execution&gt;\r\n&lt;execution&gt;\r\n&lt;id&gt;default-check&lt;\/id&gt;\r\n&lt;goals&gt;\r\n&lt;goal&gt;check&lt;\/goal&gt;\r\n&lt;\/goals&gt;\r\n&lt;configuration&gt;\r\n&lt;rules&gt;\r\n&lt;rule&gt;\r\n&lt;element&gt;CLASS&lt;\/element&gt;\r\n&lt;limits&gt;\r\n&lt;limit&gt;\r\n&lt;counter&gt;LINE&lt;\/counter&gt;\r\n&lt;value&gt;COVEREDRATIO&lt;\/value&gt;\r\n&lt;minimum&gt;0.70&lt;\/minimum&gt;\r\n&lt;\/limit&gt;\r\n&lt;limit&gt;\r\n&lt;counter&gt;BRANCH&lt;\/counter&gt;\r\n&lt;value&gt;COVEREDRATIO&lt;\/value&gt;\r\n&lt;minimum&gt;0.70&lt;\/minimum&gt;\r\n&lt;\/limit&gt;\r\n&lt;\/limits&gt;\r\n&lt;\/rule&gt;\r\n&lt;\/rules&gt;\r\n&lt;\/configuration&gt;\r\n&lt;\/execution&gt;\r\n&lt;\/executions&gt;\r\n&lt;\/plugin&gt;<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Note<\/strong>: <em>You can set code coverage according to what your project&#8217;s requirements are. In this context, I have chosen to set a code coverage target of 70%, which aligns with our project&#8217;s quality and testing goals.<\/em><\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can view the code coverage report at ..\/core\/target\/site\/jacoco\/index.html<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-59142 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/10\/Screenshot-jacoco-1024x442.jpg\" alt=\"Screenshot-jacoco\" width=\"625\" height=\"270\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/10\/Screenshot-jacoco-1024x442.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2023\/10\/Screenshot-jacoco-300x129.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/10\/Screenshot-jacoco-768x331.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/10\/Screenshot-jacoco-624x269.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2023\/10\/Screenshot-jacoco.jpg 1353w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">References <\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/github.com\/adobe\/aem-project-archetype\/blob\/master\/VERSIONS.md\"><span style=\"font-weight: 400;\">https:\/\/github.com\/adobe\/aem-project-archetype\/blob\/master\/VERSIONS.md<\/span><\/a><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/checkstyle.sourceforge.io\/version\/8.40\/checks.html\"><span style=\"font-weight: 400;\">https:\/\/checkstyle.sourceforge.io\/version\/8.40\/checks.html<\/span><\/a><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/github.com\/checkstyle\/checkstyle\/blob\/master\/src\/main\/resources\/sun_checks.xml\"><span style=\"font-weight: 400;\">https:\/\/github.com\/checkstyle\/checkstyle\/blob\/master\/src\/main\/resources\/sun_checks.xml<\/span><\/a><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/www.eclemma.org\/jacoco\/trunk\/doc\/check-mojo.html#rules\"><span style=\"font-weight: 400;\">https:\/\/www.eclemma.org\/jacoco\/trunk\/doc\/check-mojo.html#rules<\/span><\/a><\/li>\n<\/ol>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>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&#8217;ll explore how to integrate these plugins into your Adobe Experience Manager (AEM) codebase to boost your code quality [&hellip;]<\/p>\n","protected":false},"author":1294,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":254},"categories":[5868,446,4488],"tags":[4847,5514,1593,5515,4844],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59143"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1294"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=59143"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59143\/revisions"}],"predecessor-version":[{"id":59358,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59143\/revisions\/59358"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=59143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=59143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=59143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}