Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.Cognifide:AEM-Rules-for-SonarQube:1.3'
}
dependencies {
implementation("com.github.Cognifide:AEM-Rules-for-SonarQube:1.3")
}
<dependency>
<groupId>com.github.Cognifide</groupId>
<artifactId>AEM-Rules-for-SonarQube</artifactId>
<version>1.3</version>
</dependency>
libraryDependencies += "com.github.Cognifide" % "AEM-Rules-for-SonarQube" % "1.3"
:dependencies [[com.github.Cognifide/AEM-Rules-for-SonarQube "1.3"]]
<img src="assets/vml-logo.png" width=50% height=50%>
<img src="https://rules.sonarsource.com/images/logos/SonarLint-black.svg" height="28" alt="Available in SonarLint">
<img src="https://rules.sonarsource.com/images/logos/SonarCloud-black.svg" height="28" alt="Available in SonarCloud">
<img src="https://rules.sonarsource.com/images/logos/SonarQube-black.svg" height="28" alt="Available in SonarQube">
As we all know, SonarQube is a great tool that helps us increase quality of our codebase. However, it does apply mainly to general Java issues. As we know, we can hurt ourselves much more doing AEM. Adobe Experience Manager is a comprehensive content management platform solution for building websites, mobile apps and forms. This tool is intended to find common bugs and bad smells specific for AEM development. Documentation of each rule is available from SonarQube interface after plugin installation.
Each release has its own prerequisites section, for more information please check releases page.
Check the plugin locally with the prepared docker-compose.yml
. Just run these commands from the repo root to build the latest package and fire up a container.
mvn clean package
cd local-docker-image
docker-compose up -d
Go to your SonarQube instance administration console and open Update Center. Find AEM Rules for SonarQube plugin and click install!
aemrules-x.y.jar
or build AEM Rules for SonarQube plugin.sonarqube/extensions/plugins
directory.Use of the plugin does not differ much from regular SonarQube analysis. However, as rules are often tied to a certain AEM version and its components (Felix, Sling), we've introduced the aemVersion
analysis property.
Each rule defines supported AEM version or version range. Most of the rules are universal.
SonarLint supports only AEM-JAVA rules. Currently SonarLint does not support custom languages. Please refer to this article.
It was tried to add HTL
rules to web
language, but sonar-html-plugin is final and can not be extended.
When running analysis, pass sonarRunner.aemVersion
property with your AEM version. The format is as follows:
sonarRunner.aemVersion=<MAJOR_VERSION>.<MINOR_VERSION>
To avoid quality profiles collisions, the additional execution param has been added.
-Dsonar.html.file.suffixes=.notexistingsuffix
Running with Maven
mvn clean verify sonar:sonar \
-Dsonar.projectKey={sonar_project_key} \
-Dsonar.projectName='{sonar_project_name}' \
-Dsonar.host.url=http://localhost:9000 \
-Dsonar.token={sonar_project_token} \
-DsonarRunner.aemVersion=6.5 \
-Dsonar.html.file.suffixes=.notexistingsuffix
Below you will find descriptions of all rules available in AEM Rules for SonarQube plugin.
AEM-1 Use predefined constant in annotation instead of hardcoded value.
AEM-2 Use predefined constant instead of hardcoded value.
AEM-5 getContentResource()
is not null checked
getContentResource()
. It is possible to get a null if a jcr:content node does not exist in the repository.AEM-8 Prefer cleaner @SlingServlet
annotation.
@SlingServlet
annotation over @Properties
approach. Do not mix up both approaches.AEM-15 Usage of synchronized
keyword should be avoided if possible.
synchronized
keyword should be avoided if possible. Check if using synchronized
can be replaced with more sophisticated solution.AEM-17 No mutator methods invoked on ModifiableValueMap
ModifiableValueMap
should be replaced by ValueMap
if no mutator methods are invoked.AEM-19 Implicit search strategy used in Sling Query
SearchStrategy
can have negative performance impact if mismatched.
Therefore developer should always make informed decision and define strategy explicitly.HTL-1 Wrong placement of the HTL Attribute.
HTL-2 HTL Templates should be placed in separate files.
HTL-3 Use Explicit Names in Loops
data-sly-list
and data-sly-repeat
blocks.
Try to avoid them and use explicit names clarifying the role of the objects instead.HTL-4 Name and re-use Repeating Conditions
HTL-5 Usage of HTML comments should be avoided if possible
HTL-6 HTL automatically recognises the context for HTML output
HTL-7 Style and script tags display context definition is mandatory
HTL-8 Event attribute attributes must have display context defined
HTL-9 Inline styles must have display context defined
HTL-10 Use sly tags over redundant markup.
HTL-11 Use existing HTML elements instead of adding extra sly tags.
HTL-12 Use the most restrictive HTL context possible.
HTL-13 Avoid using 'unsafe' display context.
HTL-14 HTL expressions in HTML comments should have defined context.
HTL-15 Use Camel Case in identifiers:
AEM-3 Non-thread safe object used as a field of Servlet / Filter etc.
Servlet
or Filter
. Rule checks for the occurrence of any instance or static fields of following types:
org.apache.sling.api.resource.ResourceResolver
javax.jcr.Session
com.day.cq.wcm.api.PageManager
com.day.cq.wcm.api.components.ComponentManager
com.day.cq.wcm.api.designer.Designer
com.day.cq.dam.api.AssetManager
com.day.cq.tagging.TagManager
com.day.cq.security.UserManager
org.apache.jackrabbit.api.security.user.Authorizable
org.apache.jackrabbit.api.security.user.User
org.apache.jackrabbit.api.security.user.UserManager
AEM-6 ResourceResolver should be closed in finally block.
close
method. It is very important to call the close
method once the resource resolver is not used any more to ensure any system resources are properly clean up.AEM-7 Session should be logged out in finally block.
javax.jcr.Session
should be logged out after it is no longer needed. The logout
method releases all resources associated with Session
.AEM-11 Do not use deprecated administrative access methods
ResourceResolverFactory.getAdministrativeResourceResolver
and SlingRepository.loginAdministrative
has been deprecated. Use ResourceResolverFactory.getServiceResourceResolver
or SlingRepository.loginService
respectively.DefaultInjectionStrategy
@Optional
annotation is redundant, when defaultInjectionStrategy
is OPTIONAL
.Release notes for each version can be found in releases section.
Copyright 2015-2024 VML
Licensed under the Apache License, Version 2.0
Technical support can be made available if needed. Please contact us for more details.
We can: