Skip to content

Commit

Permalink
[MPMD-289] - check: unable to find pmd.xml
Browse files Browse the repository at this point in the history
XML report is now created always, regardless of the format
configured.
  • Loading branch information
adangel committed Jan 19, 2020
1 parent 11634cc commit b46470f
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 60 deletions.
18 changes: 18 additions & 0 deletions src/it/MPMD-289-format-html-check/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

invoker.goals = clean verify
69 changes: 69 additions & 0 deletions src/it/MPMD-289-format-html-check/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugins.pmd.its</groupId>
<artifactId>MPMD-289-format-html-check</artifactId>
<version>1.0-SNAPSHOT</version>

<description>
Then report format is html, xml format should still be created so that the check goal still works.
</description>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<skipPmdError>false</skipPmdError>
<skip>false</skip>
<failOnViolation>true</failOnViolation>
<failurePriority>4</failurePriority>
<sourceEncoding>UTF-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<skipEmptyReport>false</skipEmptyReport>
<format>html</format>
<excludes>
<exclude>**/*Bean.java</exclude>
<exclude>**/generated/*.java</exclude>
</excludes>
<excludeRoots>
<excludeRoot>target/generated-sources/stubs</excludeRoot>
</excludeRoots>
<rulesets/>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mycompany.app;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

public class App
{

}
34 changes: 34 additions & 0 deletions src/it/MPMD-289-format-html-check/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

File buildLog = new File( basedir, 'build.log' )
assert buildLog.exists()

File pmdXml = new File( basedir, 'target/pmd.xml' )
assert pmdXml.exists()

File pmdHtmlReport = new File ( basedir, 'target/site/pmd.html' )
assert pmdHtmlReport.exists()

File cpdXml = new File( basedir, 'target/cpd.xml' )
assert cpdXml.exists()

File cpdHtmlReport = new File ( basedir, 'target/site/cpd.html' )
assert cpdHtmlReport.exists()
68 changes: 41 additions & 27 deletions src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
import net.sourceforge.pmd.cpd.Language;
import net.sourceforge.pmd.cpd.LanguageFactory;
import net.sourceforge.pmd.cpd.Match;
import net.sourceforge.pmd.cpd.Renderer;
import net.sourceforge.pmd.cpd.XMLRenderer;
import net.sourceforge.pmd.cpd.renderer.CPDRenderer;

/**
* Creates a report for PMD's CPD tool. See
Expand Down Expand Up @@ -168,12 +168,7 @@ private void execute( Locale locale )
{
Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );

generateReport( locale );

if ( !isHtml() && !isXml() )
{
writeNonHtml( cpd );
}
generateMavenSiteReport( locale );
}
finally
{
Expand Down Expand Up @@ -313,11 +308,14 @@ else if ( "jsp".equals( language ) )
cpd.go();
getLog().debug( "CPD finished." );

// if format is XML, we need to output it even if the file list is empty or we have no duplications
// always create XML format. we need to output it even if the file list is empty or we have no duplications
// so the "check" goals can check for violations
if ( isXml() )
writeXmlReport( cpd );

// html format is handled by maven site report, xml format as already bean rendered
if ( !isHtml() && !isXml() )
{
writeNonHtml( cpd );
writeFormattedReport( cpd );
}
}

Expand Down Expand Up @@ -345,7 +343,7 @@ private Iterator<Match> filterMatches( Iterator<Match> matches )
return filteredMatches.iterator();
}

private void generateReport( Locale locale )
private void generateMavenSiteReport( Locale locale )
{
CpdReportGenerator gen = new CpdReportGenerator( getSink(), filesToProcess, getBundle( locale ), aggregate );
Iterator<Match> matches = cpd.getMatches();
Expand Down Expand Up @@ -373,35 +371,51 @@ else if ( showWarn )
return encoding;
}

void writeNonHtml( CPD cpd )
private void writeFormattedReport( CPD cpd )
throws MavenReportException
{
Renderer r = createRenderer();
CPDRenderer r = createRenderer();
writeReport( cpd, r, format );

}

void writeXmlReport( CPD cpd ) throws MavenReportException
{
File targetFile = writeReport( cpd, new XMLRenderer( getOutputEncoding() ), "xml" );
if ( includeXmlInSite )
{
File siteDir = getReportOutputDirectory();
siteDir.mkdirs();
try
{
FileUtils.copyFile( targetFile, new File( siteDir, "cpd.xml" ) );
}
catch ( IOException e )
{
throw new MavenReportException( e.getMessage(), e );
}
}
}

private File writeReport( CPD cpd, CPDRenderer r, String extension ) throws MavenReportException
{
if ( r == null )
{
return;
return null;
}

String buffer = r.render( filterMatches( cpd.getMatches() ) );
File targetFile = new File( targetDirectory, "cpd." + format );
File targetFile = new File( targetDirectory, "cpd." + extension );
targetDirectory.mkdirs();
try ( Writer writer = new OutputStreamWriter( new FileOutputStream( targetFile ), getOutputEncoding() ) )
{
writer.write( buffer );
r.render( filterMatches( cpd.getMatches() ), writer );
writer.flush();

if ( includeXmlInSite )
{
File siteDir = getReportOutputDirectory();
siteDir.mkdirs();
FileUtils.copyFile( targetFile, new File( siteDir, "cpd." + format ) );
}
}
catch ( IOException ioe )
{
throw new MavenReportException( ioe.getMessage(), ioe );
}
return targetFile;
}

/**
Expand All @@ -423,10 +437,10 @@ private static ResourceBundle getBundle( Locale locale )
* @return the renderer based on the configured output
* @throws org.apache.maven.reporting.MavenReportException if no renderer found for the output type
*/
public Renderer createRenderer()
public CPDRenderer createRenderer()
throws MavenReportException
{
Renderer renderer = null;
CPDRenderer renderer = null;
if ( "xml".equals( format ) )
{
renderer = new XMLRenderer( getOutputEncoding() );
Expand All @@ -439,7 +453,7 @@ else if ( !"".equals( format ) && !"none".equals( format ) )
{
try
{
renderer = (Renderer) Class.forName( format ).newInstance();
renderer = (CPDRenderer) Class.forName( format ).getConstructor().newInstance();
}
catch ( Exception e )
{
Expand Down
Loading

0 comments on commit b46470f

Please sign in to comment.