Skip to content

Commit

Permalink
Add ant example (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel authored Oct 25, 2024
1 parent 126101c commit 995d0f0
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
56 changes: 56 additions & 0 deletions ant/simple-project/.ci/build.sh
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions ant/simple-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tools/
build/
18 changes: 18 additions & 0 deletions ant/simple-project/README.md
Original file line number Diff line number Diff line change
@@ -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

* <https://docs.pmd-code.org/latest/pmd_userdocs_tools_ant.html>
* <https://ant.apache.org/manual/index.html>
49 changes: 49 additions & 0 deletions ant/simple-project/build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<project default="pmd" basedir=".">

<property name="pmd.home" value="please specify property at command line via -Dpmd.home=..."/>
<property name="src" location="src"/>
<property name="build" location="build"/>

<path id="pmd.classpath">
<fileset dir="${pmd.home}/lib/">
<include name="*.jar"/>
</fileset>
</path>

<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath" />
<taskdef name="cpd" classname="net.sourceforge.pmd.ant.CPDTask" classpathref="pmd.classpath" />

<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
</target>

<target name="compile" depends="init" description="compile the source">
<javac srcdir="${src}" destdir="${build}" includeAntRuntime="false"/>
</target>

<target name="pmd" depends="compile" description="Analyze code with PMD">
<pmd rulesetfiles="rulesets/java/quickstart.xml" failOnRuleViolation="false" noCache="true">
<auxclasspath>
<pathelement location="${build}"/>
</auxclasspath>
<formatter type="xml" toFile="${build}/pmd-report.xml" />
<formatter type="text" toConsole="true" />
<fileset dir="${src}/">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>

<target name="cpd" depends="compile" description="Find duplicates with CPD">
<cpd minimumTokenCount="100" outputFile="${build}/cpd.txt" language="java" >
<fileset dir="${src}">
<include name="**/*.java"/>
</fileset>
</cpd>
</target>

<target name="clean" description="clean up">
<delete dir="${build}"/>
</target>
</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 995d0f0

Please sign in to comment.