Eclipse-PMD Configure ruleset globally

3.6k views Asked by At

So i am using the Plugin Eclipse-PMD (http://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site/) in a shared version control enviroment.

We have multiple smaller projects in the entire project.

Out of the box it seems that this plugin requires individual configuration for every single project. The way it works it that it searched for a .pmd file in the project and read information from that.

But it's really inconvenient to do that for 10-20 subprojects.

There is a general setting under Preferences -> PMD. But this doesn't seem to apply globally, even if that global checkbox is checked.

What i basically want: I want to configure the plugin to respect a single ruleSet-file in one place.

There is another problem with configuring it subproject-specific: I cannot configure a relative path for the ruleSetFile in the .pmd-file. The problem with absolute path is that the file is checked into version control ... so with every commit everyone else would have to readjust. I found this commit: https://github.com/pmd/pmd/pull/36 but i cannot seem to make it work the way it's roughly described.

So, did anyone achieve what i am looking for?

Edit: Actually i cannot even specify any other file that is not ".ruleset" in the .pmd-file as <ruleSetFile> without specifying an absolute path??

The default value for ruleSetFile is ".ruleset". So i thought, analogous to that, i could create a file in the exact same dir, call it fooRules.xml, and specify it via <ruleSetFile>fooRules.xml</ruleSetFile> but it can only find it if put the entire path to fooRules.xml in there?!

2

There are 2 answers

11
eclipse-pmd On BEST ANSWER

Try eclipse-pmd (available in the Eclipse marketplace or via the update site http://www.acanda.ch/eclipse-pmd/release/latest). With eclipse-pmd you can configure your projects to use a single rule set file for several projects. It also stores its path relative to the workspace. You still have to configure each project individually though (for now, this will change in a future release).

To set up eclipse-pmd in the way you described you have to open the project properties of your first project, select the "PMD" property page and add the rule set. Select the rule set type "Workspace" and pick your rule set file. For every other project you have to open the project's PMD property page where you'll find the previously selected rule set file which simply needs to be checked to activate.

If you set it up this way there will be a .eclipse-pmd file in each project containing the settings. If you check this into your version control system then no one else in your team has to set up anything (apart from installing eclipse-pmd).

Disclaimer: I wrote eclipse-pmd. Mostly because I had the exact same problems as you with the other plugin.

1
Jörn Schellhaas On

I've been struggling a long time to get this working with PMD for Eclipse. While Eclipse-PMD has this feature built-in, I had some other issues with it (e.g. I think it is not meant to create reports).

The trick was adding the rules to the project as a link.

Create the rule file, e.g. pmd.xml, in the parent folder of the project. Add the file to the projects to be checked, but add it as a reference. Therefore, drag the file from the explorer to the bundle and select:

File Operation Dialog

In the project properties, in the PMD section, check Enable PMD and Apply and Close the settings.

Now close Eclipse. Edit the file with the name .pmd in the project folder by replacing the content with the following:

<?xml version="1.0" encoding="UTF-8"?>
<pmd>
<useProjectRuleSet>true</useProjectRuleSet>
<ruleSetFile>pmd.xml</ruleSetFile>
<includeDerivedFiles>false</includeDerivedFiles>
<violationsAsErrors>true</violationsAsErrors>
<fullBuildEnabled>true</fullBuildEnabled>
</pmd>

Restart Eclipse and right click the project. Select PMD/Check Code. Now, only the violations defined in pmd.xml should be reported.

Configuring PMD only using the GUI does not seem to work for me.