diff --git a/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java b/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java index 48567a9b..469cc358 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java +++ b/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java @@ -86,7 +86,8 @@ public abstract class AbstractPmdReport /** * Set the output format type, in addition to the HTML report. Must be one of: "none", "csv", "xml", "txt" or the * full class name of the PMD renderer to use. See the net.sourceforge.pmd.renderers package javadoc for available - * renderers. XML is required if the pmd:check goal is being used. + * renderers. XML is produced in any case, since this format is needed + * for the check goals (pmd:check, pmd:cpd-check). */ @Parameter( property = "format", defaultValue = "xml" ) protected String format = "xml"; diff --git a/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java b/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java index d9fb6065..61630947 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java +++ b/src/main/java/org/apache/maven/plugins/pmd/CpdReport.java @@ -51,6 +51,7 @@ import net.sourceforge.pmd.cpd.Language; import net.sourceforge.pmd.cpd.LanguageFactory; import net.sourceforge.pmd.cpd.Match; +import net.sourceforge.pmd.cpd.SimpleRenderer; import net.sourceforge.pmd.cpd.XMLRenderer; import net.sourceforge.pmd.cpd.renderer.CPDRenderer; @@ -312,7 +313,7 @@ else if ( "jsp".equals( language ) ) // so the "check" goals can check for violations writeXmlReport( cpd ); - // html format is handled by maven site report, xml format as already bean rendered + // html format is handled by maven site report, xml format has already bean rendered if ( !isHtml() && !isXml() ) { writeFormattedReport( cpd ); @@ -449,6 +450,10 @@ else if ( "csv".equals( format ) ) { renderer = new CSVRenderer(); } + else if ( "txt".equals( format ) ) + { + renderer = new SimpleRenderer(); + } else if ( !"".equals( format ) && !"none".equals( format ) ) { try diff --git a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java index 64a00684..f4d7f49e 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java +++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java @@ -499,9 +499,9 @@ private void executePmd() Report report = renderer.asReport(); writeXmlReport( report ); - // write any other format except for xml and html. xml as been just produced. + // write any other format except for xml and html. xml has just been produced. // html format is produced by the maven site formatter. Excluding html here - // avoids usind PMD's own html formatter, which doesn't fit into the maven site + // avoids using PMD's own html formatter, which doesn't fit into the maven site // considering the html/css styling if ( !isHtml() && !isXml() ) { diff --git a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java index e64d5325..91d504ab 100644 --- a/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java +++ b/src/test/java/org/apache/maven/plugins/pmd/CpdReportTest.java @@ -99,6 +99,34 @@ public void testDefaultConfiguration() assertTrue( lowerCaseContains( str, "tmp = tmp + str.substring( i, i + 1);" ) ); } + /** + * Test CPDReport with the text renderer given as "format=txt" + * + * @throws Exception + */ + public void testTxtFormat() + throws Exception + { + File testPom = + new File( getBasedir(), + "src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml" ); + CpdReport mojo = (CpdReport) lookupMojo( "cpd", testPom ); + mojo.execute(); + + // check if the CPD files were generated + File generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + generatedFile = new File( getBasedir(), "target/test/unit/custom-configuration/target/cpd.txt" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + // check the contents of cpd.txt + String str = readFile( generatedFile ); + // Contents that should NOT be in the report + assertFalse( lowerCaseContains( str, "public static void main( String[] args )" ) ); + // Contents that should be in the report + assertTrue( lowerCaseContains( str, "public void duplicateMethod( int i )" ) ); + } + /** * Test CPDReport using custom configuration * diff --git a/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml b/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml new file mode 100644 index 00000000..5ea54155 --- /dev/null +++ b/src/test/resources/unit/custom-configuration/cpd-txt-format-configuration-plugin-config.xml @@ -0,0 +1,51 @@ + + + + 4.0.0 + custom.configuration + custom-configuration + jar + 1.0-SNAPSHOT + 2006 + Maven CPD Plugin Txt Format Configuration Test + http://maven.apache.org + + txt-format-configuration + + + org.apache.maven.plugins + maven-pmd-plugin + + + ${basedir}/target/test/unit/custom-configuration/target/site + ${basedir}/target/test/unit/custom-configuration/target + txt + false + 30 + + + ${basedir}/src/test/resources/unit/custom-configuration/ + + UTF-8 + + + + +