- Java 1.7.0_40
- Gradle 1.10
I've never used Gradle's PMD plugin and I'm running into trouble trying to add rule sets to my build.gradle. The Pmd documentation is not clear about what the valid values of ruleSets are. Their example is ruleSets = ["basic", "braces"] and they link to the "official list". There's not much to go on, unfortunately.
I was guessing the section title maps to the valid string somehow? Like,
- "Basic (java)" -> "basic"
- "Braces (java)" -> "braces"
But what about things like "Empty Code (java)"?
Here's a working build.gradle example:
apply plugin: 'java'
apply plugin: 'pmd'
pmd {
ruleSets = [
// The first two better work since it's right in the Javadoc...
"basic",
"braces",
// This one does not work and other variations like
// "empty code", "emptycode", "empty-code", "empty_code" do not work.
"emptyCode"
]
}
repositories {
mavenCentral()
}
Gradle spits out the following error:
$ gradle check
:pmdMain FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':pmdMain'.
> Can't find resource emptyCode. Make sure the resource is a valid file or URL
or is on the CLASSPATH. Here's the current classpath:
/Users/kuporific/gradle/gradle-1.10/lib/gradle-launcher-1.10.jar
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.
BUILD FAILED
Total time: 9.907 secs
Running with --stacktrace or --debug as suggested doesn't seem to yield anything useful...
Note: create a dummy file like src/main/java/Dummy.java. Otherwise, the build will succeed.
How are ruleSets supposed to be declared?
Edit:
It ended up being easier declaring an xml rule set because it offers fine-grained control over the rules. It is included in build.gradle like so:
apply plugin: 'pmd'
pmd {
ruleSetFiles = files('path/to/ruleSet.xml')
}
And the rule set file looks something like this:
Note: This exaple is written for Gradle 1.10. Newer versions of Gradle (circa 2.0) use a newer version of PMD; therefore, many of the rulesets paths changed. So rulesets/logging-java.xml is now found in rulesets/java/logging-java.xml, for example.
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="Android Application Rules"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd" >
<rule ref="rulesets/logging-java.xml" />
<rule ref="rulesets/basic.xml" />
<rule ref="rulesets/braces.xml" />
<rule ref="rulesets/codesize.xml" >
<exclude name="TooManyMethods" />
</rule>
<rule ref="rulesets/controversial.xml">
<exclude name="UseConcurrentHashMap" />
<exclude name="AvoidLiteralsInIfCondition" />
<exclude name="DataflowAnomalyAnalysis" />
<exclude name="CallSuperInConstructor" />
<exclude name="AtLeastOneConstructor" />
<exclude name="NullAssignment" />
</rule>
<!-- etc... -->
</ruleset>