Adding a Java Custom Rule to check pom.xml project name

Hi all!

We are wondering if it is possible to create a custom Java rule to check that pom.xml name matches with a internal cmdb database.

There are several rules packaged in the SonarJava plugin under “org.sonar.java.checks.xml.maven” but when we try to create rules the same way in a custom java rules plugin the rules are never used.

This seems to be caused because the SonarJava plugin embeds its own XmlFileSensor that loads all those xml checks. In our custom plugin we cannot create a new XmlFileSensor because we get the “unsatisfied dependency error on sensor initialization” so our rules are never loaded for execution.

Is there a way to extend the java plugin with new maven xml checks?

Thanks!

Hi,

Extending SonarJava to write rules for Java code is interesting to reuse all the parsing logic, type inference, …
For simple XML files like a pom.xml, the benefit of extending SonarJava is low IMO.

So I suggest you write your own standalone plugin, with a Sensor, RulesDefinition, … and parse pom.xml files with the XML parser you like and raise issues as you need.

Thanks Julien!

Then we will write our own plugin… but I have been taking a look to SonarJava plugin source code and it seems so easy to add custom rules for XML… you should think about enabling the xml custom rules extensions :wink:

The problem of custom rules is that it requires from us to maintain an API (backward compatibility, documentation, …). This is a lot of work, and also slow down innovation. For Java files it really make sense since the benefit is huge (not everybody can/wants to develop a Java parser + all stuff to write rules).
Here we are “only” talking about an XML parser (or even regexp if your rules are simple), so that doesn’t worth it IMO.