jborgers / sonar-pmd

☕️ PMD Plugin for SonarQube
https://redirect.sonarsource.com/plugins/pmd.html
GNU Lesser General Public License v3.0
258 stars 156 forks source link

Add custom pmd rule to sonar 5.4 #27

Closed emnvn closed 2 years ago

emnvn commented 8 years ago

Hello,

I m using Sonar 3.4 with PMD. I have written some pmd rule in java, then I add these rules to sonar 3.4, the result that it work well.

I have plan to use sonar 5.4, when I try to add my pmd rule to sonar 5.4, It can not load these rule, I can't see any error report too.

How can I use my custom pmd rule in sonar 5.4 ?

Thanks for helping!

timwebster9 commented 8 years ago

Hi,

It's not clear to me whether the platform still supports PMD extensions. Some Googling turned up this (although it looks like it might not have been resolved):

https://sourceforge.net/p/pmd/discussion/188193/thread/f52eec4e/

Have you tried this?

emnvn commented 8 years ago

Thanks @timwebster9, I have tried it but It can't resolve my problem. I don't know whether it is supported or not. If Sonar don't support custom rules why it still support PMD plugin ?

dgeissl commented 7 years ago

I have had the same problem with SonarQube 4.5.6 and sonar-pmd-plugin 2.4.1/2.6. Didn't see that in the first place as we have only a small amount of custom rules and they still show up in the quality profile (as we used to embed them in a sonar-qube-plugin), but none of them is actually working.

I've tried to go back to sonar-pmd-plugin 2.3 as back then the /extensions/rules/pmd folder seems to have been scanned for extension xmls (altough there seems to be no schema around how to write them...) and with the help of http://sonarqube-archive.15.x6.nabble.com/PMD-custom-rule-problem-td3196265.html i got SonarQube to pick the rules up (with 2.3), but then pmd can't find the rules in the rules.jar, as it is not in the classpath.

So the next try was to play a bit with the plugin configuration. A Rather old example on how to do this can be found here: https://github.com/SonarQubeCommunity/sonar-pmd/tree/master/its/plugin/plugins/pmd-extension-plugin Now the key moment where that the plugin must use the repository key pmd (as it can be seen here https://github.com/SonarQubeCommunity/sonar-pmd/blob/master/its/plugin/plugins/pmd-extension-plugin/src/main/java/org/sonar/examples/pmd/PmdExtensionRepository.java) but that is only sufficient if you use sonar-pmd-plugin 2.3. From 2.4 onwards the server will not start anymore if you recreate a repository like this:

    public void define(Context context) {
        context.createRepository("pmd","java").setName("MyRepository")

you need to extend it:

     NewExtendedRepository repository = context.extendRepository(REPOSITORY_KEY, "java");

To be able to reuse the RulesDefinitionXmlLoader i wrote a thin wrapper around the NewExtendedRepository implementing RulesDefinition.NewExtendedRepository and delegating all rule related methods (excluding setName) to the ExtendedRepository.

That is so much tougher than it needs to be an i don't understand why pmd is not executing the rules if they are provided under a different repository name (like it used to be with findbugs-contrib). Any suggestions how to improve it?

jborgers commented 2 years ago

Is this still relevant?

jborgers commented 2 years ago

Old versions. Assuming not relevant anymore.