Skip to content

Commit 995edef

Browse files
committed
(doc) Add example "Using a different ruleset for tests"
This is related to MPMD-236 and MPMD-242
1 parent 45a5e54 commit 995edef

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
------
2+
Using a different ruleset for tests
3+
------
4+
Andreas Dangel
5+
------
6+
2021-07-09
7+
------
8+
9+
~~ Licensed to the Apache Software Foundation (ASF) under one
10+
~~ or more contributor license agreements. See the NOTICE file
11+
~~ distributed with this work for additional information
12+
~~ regarding copyright ownership. The ASF licenses this file
13+
~~ to you under the Apache License, Version 2.0 (the
14+
~~ "License"); you may not use this file except in compliance
15+
~~ with the License. You may obtain a copy of the License at
16+
~~
17+
~~ http://www.apache.org/licenses/LICENSE-2.0
18+
~~
19+
~~ Unless required by applicable law or agreed to in writing,
20+
~~ software distributed under the License is distributed on an
21+
~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22+
~~ KIND, either express or implied. See the License for the
23+
~~ specific language governing permissions and limitations
24+
~~ under the License.
25+
26+
~~ NOTE: For help with the syntax of this file, see:
27+
~~ http://maven.apache.org/doxia/references/apt-format.html
28+
29+
Using a different ruleset for tests
30+
31+
PMD shall be configured to scan test source code with a less strict
32+
ruleset than production code. This can be achieved by configuring
33+
multiple plugin
34+
{{{https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_executions_Tag}executions}}
35+
with different configurations.
36+
37+
<<Note:>> There are different opinions whether one should lower
38+
that quality just because it's "just test code". While test code doesn't run
39+
in production, it's used to test production code, so you might consider
40+
the test code then as your weakest link if you use less strict checks.
41+
42+
Here's the complete plugin configuration for this scenario:
43+
44+
+-----+
45+
<project>
46+
...
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-pmd-plugin</artifactId>
52+
<version>${project.version}</version>
53+
<configuration>
54+
<includeTests>false</includeTests>
55+
<rulesets>
56+
<ruleset>config/pmd/pmdMain.xml</ruleset>
57+
</rulesets>
58+
<linkXRef>false</linkXRef>
59+
</configuration>
60+
<executions>
61+
<execution>
62+
<id>pmd-main</id>
63+
<phase>verify</phase>
64+
<goals>
65+
<goal>check</goal>
66+
</goals>
67+
</execution>
68+
<execution>
69+
<id>pmd-test</id>
70+
<phase>verify</phase>
71+
<goals>
72+
<goal>pmd</goal>
73+
<goal>check</goal>
74+
</goals>
75+
<configuration>
76+
<targetDirectory>\${project.build.directory}/pmdTest/</targetDirectory>
77+
<includeTests>true</includeTests>
78+
<excludeRoots>
79+
<excludeRoot>${basedir}/src/main/java</excludeRoot>
80+
</excludeRoots>
81+
<rulesets>
82+
<ruleset>config/pmd/pmdTest.xml</ruleset>
83+
</rulesets>
84+
</configuration>
85+
</execution>
86+
<execution>
87+
<id>cpd</id>
88+
<phase>verify</phase>
89+
<goals>
90+
<goal>cpd-check</goal>
91+
</goals>
92+
</execution>
93+
</executions>
94+
</plugin>
95+
...
96+
</plugins>
97+
</build>
98+
</project>
99+
+-----+
100+
101+
It uses the ruleset <<<config/pmd/pmdMain.xml>>> for the main code without tests. This is configured
102+
directly at the plugin level. The ruleset <<<config/pmd/pmdTest.xml>>> is used for the test code only.
103+
This is configured in the execution with id <<<pmd-test>>>.
104+
105+
This solution has one downside though: PMD is run three times: Twice for the main code, and once for
106+
the test code. The reason is that "pmd:check" triggers automatically "pmd:pmd", but it uses only the
107+
standard configuration (e.g. it ignores the lifecycle/execution id).
108+
109+
The execution <<<pmd-test>>> calls once "pmd:pmd" for the test code (which creates
110+
<<<target/pmdTest/pmd.xml>>>), then calls "pmd:check" - which itself calls "pmd:pmd" and uses
111+
the default configuration - and finally runs "pmd:check" actually, which uses the execution
112+
configuration and uses <<<target/pmdTest/pmd.xml>>> to decide whether to fail the build.

src/site/apt/index.apt.vm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,6 @@ ${project.name}
100100

101101
* {{{./examples/cpdCsharp.html}Finding duplicated code in C#}}
102102

103+
* {{{./examples/differentRulesetForTests.html}Different ruleset for tests}}
104+
103105
[]

src/site/site.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ under the License.
4343
<item name="Analyzing Java Server Pages" href="examples/jspReport.html"/>
4444
<item name="Violations Exclusions" href="examples/violation-exclusions.html"/>
4545
<item name="Duplicated code in C#" href="examples/cpdCsharp.html"/>
46+
<item name="Different ruleset for tests" href="examples/differentRulesetForTests.html"/>
4647
</menu>
4748
</body>
4849
</project>

0 commit comments

Comments
 (0)