Skip to content

Commit 93b8658

Browse files
committed
Collect all existing sample projects from the various branches
1 parent 219f1c2 commit 93b8658

File tree

51 files changed

+2477
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2477
-4
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
This repository contains separate folders for specific use cases:
44

5-
* Java Custom Rules
6-
* PLSQL Custom Rules
7-
* Java Custom Rules without maven
8-
* Simple Maven Project
5+
* Creating Custom Rules
6+
* For Java: <custom-rules/maven-java/>
7+
* For PLSQL: <custom-rules/maven-plsql/>
8+
* For Java, but without maven: <custom-rules/plain-java/>
9+
* Using PMD
10+
* With Maven: <maven/simple-project/>

custom-rules/maven-java/.ci/build.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# exit immediately if a command fails
4+
set -e
5+
6+
java -version
7+
8+
echo
9+
echo "======================================================="
10+
echo "Building..."
11+
echo "======================================================="
12+
echo
13+
./mvnw clean verify
14+
15+
echo
16+
echo "======================================================="
17+
echo "Running PMD..."
18+
echo "======================================================="
19+
echo
20+
cd pmd-java-dist/target
21+
unzip pmd-java-bin-1.0.0-SNAPSHOT.zip
22+
pmd-java-bin-1.0.0-SNAPSHOT/bin/run.sh pmd -no-cache \
23+
-f text \
24+
-d ../../ \
25+
-R custom-java-ruleset.xml \
26+
-failOnViolation false \
27+
-reportfile pmdreport.txt
28+
29+
grep "examples/java/rules/MyRule.java" pmdreport.txt || (echo "Missing expected rule violation"; exit 1)

custom-rules/maven-java/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.2/apache-maven-3.9.2-bin.zip
18+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar

custom-rules/maven-java/README.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Java Custom Rules
2+
3+
Sample project which shows how to create and use custom rules for Java.
4+
5+
You can build the project using maven:
6+
7+
```
8+
$ ./mvnw clean verify
9+
```
10+
11+
## pmd-java-custom
12+
13+
This is a sample project, which contains two sample rules:
14+
15+
* MyRule - which is a Java-based rule for detecting variables with a specific name.
16+
* VariableNaming - which is a XPath-based rule for checking variable names against a regular expression.
17+
18+
Building the project also runs the unit tests for the rules.
19+
20+
It also contains a custom ruleset, which includes some PMD built-in rules as well as the custom rules.
21+
See `custom-java-ruleset.xml`.
22+
23+
The result is a jar file, which contains the rules: `target/pmd-java-custom-1.0.0-SNAPSHOT.jar`.
24+
25+
## pmd-java-dist
26+
27+
This builds a customized PMD binary, which includes all necessary dependencies for Java only
28+
including the custom rules.
29+
30+
The result is a zip file: `target/pmd-java-bin-1.0.0-SNAPSHOT.zip`.
31+
32+
## Using with PMD CLI
33+
34+
### Option A
35+
36+
1. Install PMD using the created `pmd-java-bin-1.0.0-SNAPSHOT.zip` file like a normal PMD binary distribution.
37+
2. Run PMD: `./run.sh pmd -f text -d src -R custom-java-ruleset.xml`
38+
39+
### Option B
40+
41+
1. Install PMD as usual.
42+
2. Copy the jar file `pmd-java-custom-1.0.0-SNAPSHOT.jar` to the `lib` directory, where you have
43+
installed PMD.
44+
3. Run PMD: `./run.sh pmd -f text -d src -R custom-java-ruleset.xml`
45+
46+
## Using with the maven-pmd-plugin
47+
48+
1. Install the sample project into your local maven repo:
49+
50+
./mvnw clean install
51+
52+
Or configure the `distributionManagement` section and run `./mvnw clean deploy`, to deploy into
53+
your repository.
54+
55+
2. Modify the plugin section, so that your custom-rule-example is added as an additional dependency for
56+
the maven-pmd-plugin:
57+
58+
```xml
59+
...
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-pmd-plugin</artifactId>
63+
<version>3.11.0</version>
64+
<executions>
65+
<execution>
66+
<phase>verify</phase>
67+
<goals>
68+
<goal>pmd</goal>
69+
<goal>cpd</goal>
70+
</goals>
71+
</execution>
72+
</executions>
73+
<configuration>
74+
<minimumTokens>100</minimumTokens>
75+
<targetJdk>11</targetJdk>
76+
<rulesets>
77+
<ruleset>custom-java-ruleset.xml</ruleset>
78+
</rulesets>
79+
</configuration>
80+
<dependencies>
81+
<dependency>
82+
<groupId>net.sourceforge.pmd.examples</groupId>
83+
<artifactId>pmd-java-custom</artifactId>
84+
<version>1.0.0-SNAPSHOT</version>
85+
</dependency>
86+
</dependencies>
87+
</plugin>
88+
```
89+
90+
Now the m-pmd-p executes your custom rule.

0 commit comments

Comments
 (0)