Skip to content

Commit 995d0f0

Browse files
authored
Add ant example (#9)
1 parent 126101c commit 995d0f0

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed

.github/workflows/build.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,23 @@ jobs:
120120
run: |
121121
cd gradle/simple-project
122122
.ci/build.sh
123+
124+
ant_simple_project:
125+
runs-on: ubuntu-latest
126+
127+
steps:
128+
- uses: actions/checkout@v4
129+
- uses: actions/setup-java@v4
130+
with:
131+
distribution: 'temurin'
132+
java-version: '17'
133+
- uses: actions/cache@v4
134+
with:
135+
path: ant/simple-project/ant/tools
136+
key: ${{ runner.os }}-ant-${{ hashFiles('build.xml', '.ci/build.sh') }}
137+
restore-keys: |
138+
${{ runner.os }}-ant-
139+
- name: Run .ci/build.sh
140+
run: |
141+
cd ant/simple-project
142+
.ci/build.sh

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ This repository contains separate folders for specific use cases:
99
* Using PMD
1010
* With Maven: [maven/simple-project/](maven/simple-project/)
1111
* With Gradle: [gradle/simple-project/](gradle/simple-project/)
12+
* With Ant: [ant/simple-project/](ant/simple-project/)

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
# exit immediately if a command fails
4+
set -e
5+
6+
ANT_VERSION=1.10.15
7+
PMD_VERSION=7.7.0
8+
9+
BASEDIR="$(pwd)"
10+
mkdir -p tools
11+
12+
echo
13+
echo "======================================================="
14+
echo "Downloading ant ${ANT_VERSION}"
15+
echo "======================================================="
16+
export ANT_HOME="${BASEDIR}/tools/apache-ant-${ANT_VERSION}"
17+
if [ ! -d "${ANT_HOME}" ]; then
18+
wget --no-verbose "https://dlcdn.apache.org/ant/binaries/apache-ant-${ANT_VERSION}-bin.zip" -O "${BASEDIR}/tools/apache-ant-${ANT_VERSION}-bin.zip"
19+
unzip -q -d "${BASEDIR}/tools" "${BASEDIR}/tools/apache-ant-${ANT_VERSION}-bin.zip"
20+
echo "Ant ${ANT_VERSION} installed at ${ANT_HOME}"
21+
else
22+
echo "Ant ${ANT_VERSION} already installed: ${ANT_HOME}"
23+
fi
24+
25+
echo
26+
echo "======================================================="
27+
echo "Downloading PMD ${PMD_VERSION}"
28+
echo "======================================================="
29+
export PMD_HOME="${BASEDIR}/tools/pmd-bin-${PMD_VERSION}"
30+
if [ ! -d "${PMD_HOME}" ]; then
31+
if [[ "${PMD_VERSION}" == *-SNAPSHOT ]]; then
32+
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
33+
else
34+
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
35+
fi
36+
unzip -q -d "${BASEDIR}/tools" "${BASEDIR}/tools/pmd-dist-${PMD_VERSION}-bin.zip"
37+
echo "PMD ${PMD_VERSION} installed at: ${PMD_HOME}"
38+
else
39+
echo "PMD ${PMD_VERSION} already installed: ${PMD_HOME}"
40+
fi
41+
42+
43+
echo
44+
echo "======================================================="
45+
echo "Building project..."
46+
echo "======================================================="
47+
${ANT_HOME}/bin/ant -Dpmd.home="$PMD_HOME" clean pmd cpd
48+
49+
50+
echo
51+
echo "======================================================="
52+
echo "Verify build/pmd-report.xml..."
53+
echo "======================================================="
54+
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)
55+
56+
echo -e "\n\n\x1b[32mTest successful\e[0m"

ant/simple-project/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tools/
2+
build/

ant/simple-project/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# simple-project
2+
3+
Demonstrates the usage of PMD with Apache Ant.
4+
5+
Run with
6+
7+
ant -Dpmd.home=path-to-pmd-bin-directory clean pmd
8+
9+
This should find the following violations in Main.java:
10+
11+
[pmd] .../src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java:6: UnusedPrivateField: Avoid unused private fields such as 'unusedField'.
12+
[pmd] .../src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java:12: EmptyControlStatement: Empty if statement
13+
[pmd] .../src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java:12: ControlStatementBraces: This statement should have braces
14+
15+
## References
16+
17+
* <https://docs.pmd-code.org/latest/pmd_userdocs_tools_ant.html>
18+
* <https://ant.apache.org/manual/index.html>

ant/simple-project/build.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<project default="pmd" basedir=".">
2+
3+
<property name="pmd.home" value="please specify property at command line via -Dpmd.home=..."/>
4+
<property name="src" location="src"/>
5+
<property name="build" location="build"/>
6+
7+
<path id="pmd.classpath">
8+
<fileset dir="${pmd.home}/lib/">
9+
<include name="*.jar"/>
10+
</fileset>
11+
</path>
12+
13+
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath" />
14+
<taskdef name="cpd" classname="net.sourceforge.pmd.ant.CPDTask" classpathref="pmd.classpath" />
15+
16+
<target name="init">
17+
<tstamp/>
18+
<mkdir dir="${build}"/>
19+
</target>
20+
21+
<target name="compile" depends="init" description="compile the source">
22+
<javac srcdir="${src}" destdir="${build}" includeAntRuntime="false"/>
23+
</target>
24+
25+
<target name="pmd" depends="compile" description="Analyze code with PMD">
26+
<pmd rulesetfiles="rulesets/java/quickstart.xml" failOnRuleViolation="false" noCache="true">
27+
<auxclasspath>
28+
<pathelement location="${build}"/>
29+
</auxclasspath>
30+
<formatter type="xml" toFile="${build}/pmd-report.xml" />
31+
<formatter type="text" toConsole="true" />
32+
<fileset dir="${src}/">
33+
<include name="**/*.java"/>
34+
</fileset>
35+
</pmd>
36+
</target>
37+
38+
<target name="cpd" depends="compile" description="Find duplicates with CPD">
39+
<cpd minimumTokenCount="100" outputFile="${build}/cpd.txt" language="java" >
40+
<fileset dir="${src}">
41+
<include name="**/*.java"/>
42+
</fileset>
43+
</cpd>
44+
</target>
45+
46+
<target name="clean" description="clean up">
47+
<delete dir="${build}"/>
48+
</target>
49+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.sourceforge.pmd.examples.ant.simpleproject;
2+
3+
public class Main {
4+
5+
// UnusedPrivateField
6+
private String unusedField;
7+
8+
public static void main(String[] args) {
9+
System.out.println("Hello World!");
10+
11+
// EmptyControlStatement
12+
if (args.length == 0);
13+
}
14+
}

0 commit comments

Comments
 (0)