Skip to content

Commit

Permalink
Merge pull request #3 from adangel/pmd7
Browse files Browse the repository at this point in the history
Update to PMD 7.0.0-rc3
  • Loading branch information
adangel committed Sep 4, 2023
2 parents 2cfd612 + 345cf61 commit 3ddb25d
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 36 deletions.
6 changes: 3 additions & 3 deletions custom-rules/maven-java/.ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ echo "Running PMD..."
echo "======================================================="
echo
cd pmd-java-dist/target
unzip -q pmd-java-bin-1.0.0-SNAPSHOT.zip
pmd-java-bin-1.0.0-SNAPSHOT/bin/run.sh pmd --no-cache \
unzip -q pmd-java-dist-1.0.0-SNAPSHOT-bin.zip
pmd-java-bin-1.0.0-SNAPSHOT/bin/pmd check --no-cache \
-f text \
-d ../../ \
-R custom-java-ruleset.xml \
--fail-on-violation false \
--no-fail-on-violation \
--report-file pmdreport.txt

grep "examples/java/rules/MyRule.java" pmdreport.txt || (echo -e "\n\n\x1b[31mMissing expected rule violation\e[0m"; exit 1)
Expand Down
29 changes: 26 additions & 3 deletions custom-rules/maven-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ The result is a zip file: `target/pmd-java-bin-1.0.0-SNAPSHOT.zip`.
### Option A

1. Install PMD using the created `pmd-java-bin-1.0.0-SNAPSHOT.zip` file like a normal PMD binary distribution.
2. Run PMD: `./run.sh pmd -f text -d src -R custom-java-ruleset.xml`
2. Run PMD: `bin/pmd check -f text -d src -R custom-java-ruleset.xml`

### Option B

1. Install PMD as usual.
2. Copy the jar file `pmd-java-custom-1.0.0-SNAPSHOT.jar` to the `lib` directory, where you have
installed PMD.
3. Run PMD: `./run.sh pmd -f text -d src -R custom-java-ruleset.xml`
3. Run PMD: `bin/pmd check -f text -d src -R custom-java-ruleset.xml`

## Using with the maven-pmd-plugin

Expand All @@ -56,11 +56,14 @@ The result is a zip file: `target/pmd-java-bin-1.0.0-SNAPSHOT.zip`.
the maven-pmd-plugin:

```xml
<properties>
<pmd.version>7.0.0-rc3</pmd.version>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.11.0</version>
<version>3.21.1-pmd-7-SNAPSHOT</version>
<executions>
<execution>
<phase>verify</phase>
Expand All @@ -83,6 +86,26 @@ The result is a zip file: `target/pmd-java-bin-1.0.0-SNAPSHOT.zip`.
<artifactId>pmd-java-custom</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
<version>${pmd.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>
<version>${pmd.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-javascript</artifactId>
<version>${pmd.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-jsp</artifactId>
<version>${pmd.version}</version>
</dependency>
</dependencies>
</plugin>
```
Expand Down
4 changes: 2 additions & 2 deletions custom-rules/maven-java/pmd-java-custom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.sourceforge.pmd.examples.java.rules;

import org.checkerframework.checker.nullness.qual.NonNull;

import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;

Expand All @@ -14,13 +17,17 @@ public class MyRule extends AbstractJavaRule {

public MyRule() {
definePropertyDescriptor(BAD_NAME);
addRuleChainVisit(ASTVariableDeclaratorId.class);
}

@Override
protected @NonNull RuleTargetSelector buildTargetSelector() {
return RuleTargetSelector.forTypes(ASTVariableDeclaratorId.class);
}

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
String badName = getProperty(BAD_NAME);
if (node.hasImageEqualTo(badName)) {
if (badName.equals(node.getName())) {
asCtx(data).addViolation(node, node.getName());
}
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Variables should be all lowercase.
<property name="xpath">
<value>
<![CDATA[
//VariableDeclaratorId[not(pmd:matches(@Name, $pattern))]
//VariableDeclaratorId[not(matches(@Name, $pattern))]
]]>
</value>
</property>
Expand Down
15 changes: 12 additions & 3 deletions custom-rules/maven-java/pmd-java-dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<includes>scripts/**,LICENSE</includes>
<includes>scripts/**,LICENSE,conf/**</includes>
</artifactItem>
</artifactItems>
</configuration>
Expand All @@ -56,12 +56,10 @@
</dependency>
</dependencies>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<attach>false</attach>
<archiverConfig>
<defaultDirectoryMode>493</defaultDirectoryMode> <!-- 0755 -->
</archiverConfig>
<finalName>${pmd.dist.bin.baseDirectory}</finalName>
<descriptorRefs>
<descriptorRef>pmd-bin</descriptorRef>
</descriptorRefs>
Expand Down Expand Up @@ -97,6 +95,17 @@
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-ui</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-cli</artifactId>
</dependency>
<!-- include bash/zsh completions -->
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-cli</artifactId>
<type>sh</type>
<classifier>completion</classifier>
</dependency>
</dependencies>

</project>
66 changes: 61 additions & 5 deletions custom-rules/maven-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>8</java.version>
<maven.compiler.release>${java.version}</maven.compiler.release>
<pmd.version>6.55.0</pmd.version>
<pmd.ui.version>6.49.0</pmd.ui.version>
<pmd.version>7.0.0-rc3</pmd.version>
<pmd.ui.version>7.0.0-rc1</pmd.ui.version>
</properties>

<dependencyManagement>
Expand All @@ -35,16 +35,45 @@
<artifactId>pmd-ui</artifactId>
<version>${pmd.ui.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-cli</artifactId>
<version>${pmd.version}</version>
<scope>runtime</scope>
<exclusions>
<!-- exclude pmd-languages-deps because we only want to include pmd-java in our custom distribution -->
<exclusion>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-languages-deps</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- include bash/zsh completions -->
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-cli</artifactId>
<version>${pmd.version}</version>
<type>sh</type>
<classifier>completion</classifier>
<scope>runtime</scope>
<exclusions>
<!-- exclude pmd-languages-deps because we only want to include pmd-java in our custom distribution -->
<exclusion>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-languages-deps</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-test</artifactId>
<version>${pmd.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -68,8 +97,35 @@
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</version>
</plugin>
<plugin>
<groupId>org.cyclonedx</groupId>
<artifactId>cyclonedx-maven-plugin</artifactId>
<version>2.7.6</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.cyclonedx</groupId>
<artifactId>cyclonedx-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>makeAggregateBom</goal>
</goals>
</execution>
</executions>
<!-- https://github.com/CycloneDX/cyclonedx-maven-plugin/issues/326 -->
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<url>https://github.com/pmd/pmd-examples</url>
Expand Down
6 changes: 3 additions & 3 deletions custom-rules/maven-plsql/.ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ echo "======================================================="
echo
cd pmd-plsql-dist/target
unzip -q pmd-plsql-bin-1.0.0-SNAPSHOT.zip
pmd-plsql-bin-1.0.0-SNAPSHOT/bin/run.sh pmd --no-cache \
--use-version plsql- \
pmd-plsql-bin-1.0.0-SNAPSHOT/bin/pmd check --no-cache \
--use-version plsql-21c \
-f text \
-d ../../pmd-plsql-custom/src/test/plsql \
-R custom-plsql-ruleset.xml \
--fail-on-violation false \
--no-fail-on-violation \
--report-file pmdreport.txt

grep "pmd-plsql-custom/src/test/plsql/short_variables.pls" pmdreport.txt || (echo -e "\n\n\x1b[31mMissing expected rule violation\e[0m"; exit 1)
Expand Down
5 changes: 5 additions & 0 deletions custom-rules/maven-plsql/pmd-plsql-custom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-plsql</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

package net.sourceforge.pmd.examples.plsql.rules;

import org.checkerframework.checker.nullness.qual.NonNull;

import net.sourceforge.pmd.lang.plsql.ast.ASTID;
import net.sourceforge.pmd.lang.plsql.ast.ASTVariableOrConstantDeclaratorId;
import net.sourceforge.pmd.lang.plsql.rule.AbstractPLSQLRule;
import net.sourceforge.pmd.lang.rule.RuleTargetSelector;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import net.sourceforge.pmd.properties.constraints.NumericConstraints;
Expand All @@ -21,14 +24,18 @@ public class ShortVariableRule extends AbstractPLSQLRule {

public ShortVariableRule() {
definePropertyDescriptor(MINIMUM_LENGTH);
addRuleChainVisit(ASTVariableOrConstantDeclaratorId.class);
}

@Override
protected @NonNull RuleTargetSelector buildTargetSelector() {
return RuleTargetSelector.forTypes(ASTVariableOrConstantDeclaratorId.class);
}

@Override
public Object visit(ASTVariableOrConstantDeclaratorId node, Object data) {
Integer min = getProperty(MINIMUM_LENGTH);

ASTID id = node.getFirstChildOfType(ASTID.class);
ASTID id = node.firstChild(ASTID.class);
if (id != null) {
if (node.getImage().length() < min) {
asCtx(data).addViolation(node, node.getImage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Variables should be all lowercase.
<property name="xpath">
<value>
<![CDATA[
//VariableOrConstantDeclaratorId[ID[not(pmd:matches(@Image, $pattern))]]
//VariableOrConstantDeclaratorId[ID[not(matches(@Image, $pattern))]]
]]>
</value>
</property>
Expand Down
13 changes: 12 additions & 1 deletion custom-rules/maven-plsql/pmd-plsql-dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<includes>scripts/**,LICENSE</includes>
<includes>scripts/**,LICENSE,conf/**</includes>
</artifactItem>
</artifactItems>
</configuration>
Expand Down Expand Up @@ -97,6 +97,17 @@
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-ui</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-cli</artifactId>
</dependency>
<!-- include bash/zsh completions -->
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-cli</artifactId>
<type>sh</type>
<classifier>completion</classifier>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit 3ddb25d

Please sign in to comment.