diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a309f2b..34c3c3c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -120,3 +120,23 @@ jobs: run: | cd gradle/simple-project .ci/build.sh + + ant_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: ant/simple-project/ant/tools + key: ${{ runner.os }}-ant-${{ hashFiles('build.xml', '.ci/build.sh') }} + restore-keys: | + ${{ runner.os }}-ant- + - name: Run .ci/build.sh + run: | + cd ant/simple-project + .ci/build.sh diff --git a/README.md b/README.md index 5a13175..448cf38 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,4 @@ This repository contains separate folders for specific use cases: * Using PMD * With Maven: [maven/simple-project/](maven/simple-project/) * With Gradle: [gradle/simple-project/](gradle/simple-project/) + * With Ant: [ant/simple-project/](ant/simple-project/) diff --git a/ant/simple-project/.ci/build.sh b/ant/simple-project/.ci/build.sh new file mode 100755 index 0000000..c26e608 --- /dev/null +++ b/ant/simple-project/.ci/build.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# exit immediately if a command fails +set -e + +ANT_VERSION=1.10.15 +PMD_VERSION=7.7.0 + +BASEDIR="$(pwd)" +mkdir -p tools + +echo +echo "=======================================================" +echo "Downloading ant ${ANT_VERSION}" +echo "=======================================================" +export ANT_HOME="${BASEDIR}/tools/apache-ant-${ANT_VERSION}" +if [ ! -d "${ANT_HOME}" ]; then + wget --no-verbose "https://dlcdn.apache.org/ant/binaries/apache-ant-${ANT_VERSION}-bin.zip" -O "${BASEDIR}/tools/apache-ant-${ANT_VERSION}-bin.zip" + unzip -q -d "${BASEDIR}/tools" "${BASEDIR}/tools/apache-ant-${ANT_VERSION}-bin.zip" + echo "Ant ${ANT_VERSION} installed at ${ANT_HOME}" +else + echo "Ant ${ANT_VERSION} already installed: ${ANT_HOME}" +fi + +echo +echo "=======================================================" +echo "Downloading PMD ${PMD_VERSION}" +echo "=======================================================" +export PMD_HOME="${BASEDIR}/tools/pmd-bin-${PMD_VERSION}" +if [ ! -d "${PMD_HOME}" ]; then + if [[ "${PMD_VERSION}" == *-SNAPSHOT ]]; then + wget --no-verbose https://sourceforge.net/projects/pmd/files/pmd/${PMD_VERSION}/pmd-dist-${PMD_VERSION}-bin.zip/download -O ${BASEDIR}/tools/pmd-dist-${PMD_VERSION}-bin.zip + else + wget --no-verbose https://github.com/pmd/pmd/releases/download/pmd_releases%2F${PMD_VERSION}/pmd-dist-${PMD_VERSION}-bin.zip -O ${BASEDIR}/tools/pmd-dist-${PMD_VERSION}-bin.zip + fi + unzip -q -d "${BASEDIR}/tools" "${BASEDIR}/tools/pmd-dist-${PMD_VERSION}-bin.zip" + echo "PMD ${PMD_VERSION} installed at: ${PMD_HOME}" +else + echo "PMD ${PMD_VERSION} already installed: ${PMD_HOME}" +fi + + +echo +echo "=======================================================" +echo "Building project..." +echo "=======================================================" +${ANT_HOME}/bin/ant -Dpmd.home="$PMD_HOME" clean pmd cpd + + +echo +echo "=======================================================" +echo "Verify build/pmd-report.xml..." +echo "=======================================================" +grep "${BASEDIR}/src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java" build/pmd-report.xml || (echo -e "\n\n\x1b[31mMissing expected rule violation\e[0m"; exit 1) + +echo -e "\n\n\x1b[32mTest successful\e[0m" diff --git a/ant/simple-project/.gitignore b/ant/simple-project/.gitignore new file mode 100644 index 0000000..d6264fe --- /dev/null +++ b/ant/simple-project/.gitignore @@ -0,0 +1,2 @@ +tools/ +build/ diff --git a/ant/simple-project/README.md b/ant/simple-project/README.md new file mode 100644 index 0000000..f9a79d2 --- /dev/null +++ b/ant/simple-project/README.md @@ -0,0 +1,18 @@ +# simple-project + +Demonstrates the usage of PMD with Apache Ant. + +Run with + + ant -Dpmd.home=path-to-pmd-bin-directory clean pmd + +This should find the following violations in Main.java: + + [pmd] .../src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java:6: UnusedPrivateField: Avoid unused private fields such as 'unusedField'. + [pmd] .../src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java:12: EmptyControlStatement: Empty if statement + [pmd] .../src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java:12: ControlStatementBraces: This statement should have braces + +## References + +* +* diff --git a/ant/simple-project/build.xml b/ant/simple-project/build.xml new file mode 100644 index 0000000..ca05bd6 --- /dev/null +++ b/ant/simple-project/build.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ant/simple-project/src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java b/ant/simple-project/src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java new file mode 100644 index 0000000..b33b97d --- /dev/null +++ b/ant/simple-project/src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java @@ -0,0 +1,14 @@ +package net.sourceforge.pmd.examples.ant.simpleproject; + +public class Main { + + // UnusedPrivateField + private String unusedField; + + public static void main(String[] args) { + System.out.println("Hello World!"); + + // EmptyControlStatement + if (args.length == 0); + } +}