-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
tools/ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
14 changes: 14 additions & 0 deletions
14
ant/simple-project/src/net/sourceforge/pmd/examples/ant/simpleproject/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |