Skip to content

Commit

Permalink
Add gradle example
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Mar 8, 2024
1 parent 2410ea5 commit 33ec8e6
Show file tree
Hide file tree
Showing 16 changed files with 533 additions and 5 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,25 @@ jobs:
run: |
cd maven/simple-project
.ci/build.sh
gradle_simple_project:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Run .ci/build.sh
run: |
cd gradle/simple-project
.ci/build.sh
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
This repository contains separate folders for specific use cases:

* Creating Custom Rules
* For Java: <custom-rules/maven-java/>
* For PLSQL: <custom-rules/maven-plsql/>
* For Java, but without maven: <custom-rules/plain-java/>
* For Java: [custom-rules/maven-java/](custom-rules/maven-java/)
* For PLSQL: [custom-rules/maven-plsql/](custom-rules/maven-plsql/)
* For Java, but without maven: [custom-rules/plain-java/](custom-rules/plain-java/)
* Using PMD
* With Maven: <maven/simple-project/>
* With Maven: [maven/simple-project/](maven/simple-project/)
* With Gradle: [gradle/simple-project/](gradle/simple-project/)
12 changes: 12 additions & 0 deletions gradle/simple-project/.ci/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# exit immediately if a command fails
set -e

./gradlew check 2>&1 | tee build.log

echo
echo "Verifying build.log..."
grep "2 PMD rule violations were found." build.log || (echo -e "\n\n\x1b[31mMissing expected rule violation\e[0m"; exit 1)

echo -e "\n\n\x1b[32mTest successful\e[0m"
9 changes: 9 additions & 0 deletions gradle/simple-project/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

5 changes: 5 additions & 0 deletions gradle/simple-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
17 changes: 17 additions & 0 deletions gradle/simple-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# simple-project

Demonstrates the usage of PMD with Gradle.

Run with

./gradlew check

This should find the following violations in App.java:

.../pmd-examples/gradle/simple-project/app/src/main/java/org/example/App.java:8: UnusedPrivateField: Avoid unused private fields such as 'unusedField'.
.../pmd-examples/gradle/simple-project/app/src/main/java/org/example/App.java:16: SystemPrintln: Usage of System.out/err

## References

* <https://docs.pmd-code.org/latest/pmd_userdocs_tools_gradle.html>
* <https://docs.gradle.org/current/userguide/pmd_plugin.html>
55 changes: 55 additions & 0 deletions gradle/simple-project/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* 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.
*/

plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
id 'pmd'
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
}

dependencies {
// Use JUnit Jupiter for testing.
testImplementation libs.junit.jupiter

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// This dependency is used by the application.
implementation libs.guava
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

application {
// Define the main class for the application.
mainClass = 'org.example.App'
}

tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}

pmd {
toolVersion = "7.0.0-SNAPSHOT"
consoleOutput = true

//ruleSets = ["category/java/errorprone.xml"] // default
ruleSets = ["category/java/errorprone.xml", "category/java/bestpractices.xml"]
}
18 changes: 18 additions & 0 deletions gradle/simple-project/app/src/main/java/org/example/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package org.example;

public class App {
// UnusedPrivateField
private String unusedField = "unused";

public String getGreeting() {
return "Hello World!";
}

public static void main(String[] args) {
// SystemPrintln
System.out.println(new App().getGreeting());
}
}
14 changes: 14 additions & 0 deletions gradle/simple-project/app/src/test/java/org/example/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package org.example;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class AppTest {
@Test void appHasAGreeting() {
App classUnderTest = new App();
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
}
}
10 changes: 10 additions & 0 deletions gradle/simple-project/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format

[versions]
guava = "32.1.3-jre"
junit-jupiter = "5.10.0"

[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 33ec8e6

Please sign in to comment.