-
Rule Set: Description: Sample run command:
I want to achieve something like being able to specify:
And get the value of the -customization parameter in one of the rules. How would you guys recommend me to do it? I need to avoid recompilation of PMD for each configuration change, so I need to access the content of the files at run time. Would appreciate the help! Thanks a lot. Running PMD through: [CLI] |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
@Waqas716 well, you would have to add the parameter to However, given that rules can be passed properties through the ruleset XML, this is most likely completely unnecessary. What is it exactly that you are trying to achieve? |
Beta Was this translation helpful? Give feedback.
-
@jsotuyod Thanks for the help, Exactly, I am hoping to find something easier than what you suggested with PmdParameters and PmdConfiguration. What I am trying to achieve is to give users ability to override some threshold values for some rules according to what makes sense for their project without having the need to customise/compile their own pmd. For example here in GOD Class Rule if user wants to change the value of I am creating some rules which use similar constants values but I want to give user ability to override those value by providing a customisation file( json or could be XML if that's easier to work with as you suggested ruleset XML). I could force them to put on a specific path like ~/pmd-customisation/file.json which is ugly. |
Beta Was this translation helpful? Give feedback.
-
@Waqas716 then you don't need anything at all. The existing properties support is more than enough for your usecase. Instead of using a constant, you define a property with a proper type and default value (for instance Given that, the end user can write a custom ruleset XML including your new rule, passing proper values for the properties, for instance: <?xml version="1.0"?>
<ruleset name="Custom Properties Java ruleset example"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
Custom ruleset for Java projects
</description>
<rule ref="YourRuleSet.xml/YourRuleName">
<properties>
<property name="yourPropertyName" value="23" />
</properties>
</rule>
</ruleset> Notice that if the user doesn't define the property (or uses your ruleset directly, without a custom xml) the default value will be used. This is an opt-in for configuration and flexibility, if the defaults work for the user, they don't need to do anything. When writing unit tests, if you are using pmd-test, you can setup the values to assign to properties in each scenario like this |
Beta Was this translation helpful? Give feedback.
-
Thank you for the great answer. That's what I need, perfect! |
Beta Was this translation helpful? Give feedback.
@Waqas716 then you don't need anything at all. The existing properties support is more than enough for your usecase.
Instead of using a constant, you define a property with a proper type and default value (for instance
AvoidDeeplyNestedIfStmtsRule
defines an integer property for how many are too many nested ifs). There are property types for pretty much all data types you can think of in thenet.sourceforge.pmd.lang.rule.properties
package.Given that, the end user can write a custom ruleset XML including your new rule, passing proper values for the properties, for instance: