Skip to content

Commit 33ec8e6

Browse files
committed
Add gradle example
1 parent 2410ea5 commit 33ec8e6

File tree

16 files changed

+533
-5
lines changed

16 files changed

+533
-5
lines changed

.github/workflows/build.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,25 @@ jobs:
9898
run: |
9999
cd maven/simple-project
100100
.ci/build.sh
101+
102+
gradle_simple_project:
103+
runs-on: ubuntu-latest
104+
105+
steps:
106+
- uses: actions/checkout@v4
107+
- uses: actions/setup-java@v4
108+
with:
109+
distribution: 'temurin'
110+
java-version: '17'
111+
- uses: actions/cache@v4
112+
with:
113+
path: |
114+
~/.gradle/caches
115+
~/.gradle/wrapper
116+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
117+
restore-keys: |
118+
${{ runner.os }}-gradle-
119+
- name: Run .ci/build.sh
120+
run: |
121+
cd gradle/simple-project
122+
.ci/build.sh

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
This repository contains separate folders for specific use cases:
44

55
* 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/>
6+
* For Java: [custom-rules/maven-java/](custom-rules/maven-java/)
7+
* For PLSQL: [custom-rules/maven-plsql/](custom-rules/maven-plsql/)
8+
* For Java, but without maven: [custom-rules/plain-java/](custom-rules/plain-java/)
99
* Using PMD
10-
* With Maven: <maven/simple-project/>
10+
* With Maven: [maven/simple-project/](maven/simple-project/)
11+
* With Gradle: [gradle/simple-project/](gradle/simple-project/)

gradle/simple-project/.ci/build.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# exit immediately if a command fails
4+
set -e
5+
6+
./gradlew check 2>&1 | tee build.log
7+
8+
echo
9+
echo "Verifying build.log..."
10+
grep "2 PMD rule violations were found." build.log || (echo -e "\n\n\x1b[31mMissing expected rule violation\e[0m"; exit 1)
11+
12+
echo -e "\n\n\x1b[32mTest successful\e[0m"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+

gradle/simple-project/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build

gradle/simple-project/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# simple-project
2+
3+
Demonstrates the usage of PMD with Gradle.
4+
5+
Run with
6+
7+
./gradlew check
8+
9+
This should find the following violations in App.java:
10+
11+
.../pmd-examples/gradle/simple-project/app/src/main/java/org/example/App.java:8: UnusedPrivateField: Avoid unused private fields such as 'unusedField'.
12+
.../pmd-examples/gradle/simple-project/app/src/main/java/org/example/App.java:16: SystemPrintln: Usage of System.out/err
13+
14+
## References
15+
16+
* <https://docs.pmd-code.org/latest/pmd_userdocs_tools_gradle.html>
17+
* <https://docs.gradle.org/current/userguide/pmd_plugin.html>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.6/userguide/building_java_projects.html in the Gradle documentation.
6+
*/
7+
8+
plugins {
9+
// Apply the application plugin to add support for building a CLI application in Java.
10+
id 'application'
11+
id 'pmd'
12+
}
13+
14+
repositories {
15+
// Use Maven Central for resolving dependencies.
16+
mavenCentral()
17+
maven {
18+
url "https://oss.sonatype.org/content/repositories/snapshots/"
19+
}
20+
}
21+
22+
dependencies {
23+
// Use JUnit Jupiter for testing.
24+
testImplementation libs.junit.jupiter
25+
26+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
27+
28+
// This dependency is used by the application.
29+
implementation libs.guava
30+
}
31+
32+
// Apply a specific Java toolchain to ease working on different environments.
33+
java {
34+
toolchain {
35+
languageVersion = JavaLanguageVersion.of(17)
36+
}
37+
}
38+
39+
application {
40+
// Define the main class for the application.
41+
mainClass = 'org.example.App'
42+
}
43+
44+
tasks.named('test') {
45+
// Use JUnit Platform for unit tests.
46+
useJUnitPlatform()
47+
}
48+
49+
pmd {
50+
toolVersion = "7.0.0-SNAPSHOT"
51+
consoleOutput = true
52+
53+
//ruleSets = ["category/java/errorprone.xml"] // default
54+
ruleSets = ["category/java/errorprone.xml", "category/java/bestpractices.xml"]
55+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* This Java source file was generated by the Gradle 'init' task.
3+
*/
4+
package org.example;
5+
6+
public class App {
7+
// UnusedPrivateField
8+
private String unusedField = "unused";
9+
10+
public String getGreeting() {
11+
return "Hello World!";
12+
}
13+
14+
public static void main(String[] args) {
15+
// SystemPrintln
16+
System.out.println(new App().getGreeting());
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* This Java source file was generated by the Gradle 'init' task.
3+
*/
4+
package org.example;
5+
6+
import org.junit.jupiter.api.Test;
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class AppTest {
10+
@Test void appHasAGreeting() {
11+
App classUnderTest = new App();
12+
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file was generated by the Gradle 'init' task.
2+
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
3+
4+
[versions]
5+
guava = "32.1.3-jre"
6+
junit-jupiter = "5.10.0"
7+
8+
[libraries]
9+
guava = { module = "com.google.guava:guava", version.ref = "guava" }
10+
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }

0 commit comments

Comments
 (0)