Skip to content

Commit b46470f

Browse files
committed
[MPMD-289] - check: unable to find pmd.xml
XML report is now created always, regardless of the format configured.
1 parent 11634cc commit b46470f

File tree

7 files changed

+247
-60
lines changed

7 files changed

+247
-60
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals = clean verify
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<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">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<groupId>org.apache.maven.plugins.pmd.its</groupId>
26+
<artifactId>MPMD-289-format-html-check</artifactId>
27+
<version>1.0-SNAPSHOT</version>
28+
29+
<description>
30+
Then report format is html, xml format should still be created so that the check goal still works.
31+
</description>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-pmd-plugin</artifactId>
38+
<version>@project.version@</version>
39+
<configuration>
40+
<skipPmdError>false</skipPmdError>
41+
<skip>false</skip>
42+
<failOnViolation>true</failOnViolation>
43+
<failurePriority>4</failurePriority>
44+
<sourceEncoding>UTF-8</sourceEncoding>
45+
<minimumTokens>100</minimumTokens>
46+
<skipEmptyReport>false</skipEmptyReport>
47+
<format>html</format>
48+
<excludes>
49+
<exclude>**/*Bean.java</exclude>
50+
<exclude>**/generated/*.java</exclude>
51+
</excludes>
52+
<excludeRoots>
53+
<excludeRoot>target/generated-sources/stubs</excludeRoot>
54+
</excludeRoots>
55+
<rulesets/>
56+
</configuration>
57+
<executions>
58+
<execution>
59+
<goals>
60+
<goal>check</goal>
61+
<goal>cpd-check</goal>
62+
</goals>
63+
</execution>
64+
</executions>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
69+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.mycompany.app;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
public class App
23+
{
24+
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
21+
File buildLog = new File( basedir, 'build.log' )
22+
assert buildLog.exists()
23+
24+
File pmdXml = new File( basedir, 'target/pmd.xml' )
25+
assert pmdXml.exists()
26+
27+
File pmdHtmlReport = new File ( basedir, 'target/site/pmd.html' )
28+
assert pmdHtmlReport.exists()
29+
30+
File cpdXml = new File( basedir, 'target/cpd.xml' )
31+
assert cpdXml.exists()
32+
33+
File cpdHtmlReport = new File ( basedir, 'target/site/cpd.html' )
34+
assert cpdHtmlReport.exists()

src/main/java/org/apache/maven/plugins/pmd/CpdReport.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
import net.sourceforge.pmd.cpd.Language;
5252
import net.sourceforge.pmd.cpd.LanguageFactory;
5353
import net.sourceforge.pmd.cpd.Match;
54-
import net.sourceforge.pmd.cpd.Renderer;
5554
import net.sourceforge.pmd.cpd.XMLRenderer;
55+
import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
5656

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

171-
generateReport( locale );
172-
173-
if ( !isHtml() && !isXml() )
174-
{
175-
writeNonHtml( cpd );
176-
}
171+
generateMavenSiteReport( locale );
177172
}
178173
finally
179174
{
@@ -313,11 +308,14 @@ else if ( "jsp".equals( language ) )
313308
cpd.go();
314309
getLog().debug( "CPD finished." );
315310

316-
// if format is XML, we need to output it even if the file list is empty or we have no duplications
311+
// always create XML format. we need to output it even if the file list is empty or we have no duplications
317312
// so the "check" goals can check for violations
318-
if ( isXml() )
313+
writeXmlReport( cpd );
314+
315+
// html format is handled by maven site report, xml format as already bean rendered
316+
if ( !isHtml() && !isXml() )
319317
{
320-
writeNonHtml( cpd );
318+
writeFormattedReport( cpd );
321319
}
322320
}
323321

@@ -345,7 +343,7 @@ private Iterator<Match> filterMatches( Iterator<Match> matches )
345343
return filteredMatches.iterator();
346344
}
347345

348-
private void generateReport( Locale locale )
346+
private void generateMavenSiteReport( Locale locale )
349347
{
350348
CpdReportGenerator gen = new CpdReportGenerator( getSink(), filesToProcess, getBundle( locale ), aggregate );
351349
Iterator<Match> matches = cpd.getMatches();
@@ -373,35 +371,51 @@ else if ( showWarn )
373371
return encoding;
374372
}
375373

376-
void writeNonHtml( CPD cpd )
374+
private void writeFormattedReport( CPD cpd )
377375
throws MavenReportException
378376
{
379-
Renderer r = createRenderer();
377+
CPDRenderer r = createRenderer();
378+
writeReport( cpd, r, format );
379+
380+
}
380381

382+
void writeXmlReport( CPD cpd ) throws MavenReportException
383+
{
384+
File targetFile = writeReport( cpd, new XMLRenderer( getOutputEncoding() ), "xml" );
385+
if ( includeXmlInSite )
386+
{
387+
File siteDir = getReportOutputDirectory();
388+
siteDir.mkdirs();
389+
try
390+
{
391+
FileUtils.copyFile( targetFile, new File( siteDir, "cpd.xml" ) );
392+
}
393+
catch ( IOException e )
394+
{
395+
throw new MavenReportException( e.getMessage(), e );
396+
}
397+
}
398+
}
399+
400+
private File writeReport( CPD cpd, CPDRenderer r, String extension ) throws MavenReportException
401+
{
381402
if ( r == null )
382403
{
383-
return;
404+
return null;
384405
}
385406

386-
String buffer = r.render( filterMatches( cpd.getMatches() ) );
387-
File targetFile = new File( targetDirectory, "cpd." + format );
407+
File targetFile = new File( targetDirectory, "cpd." + extension );
388408
targetDirectory.mkdirs();
389409
try ( Writer writer = new OutputStreamWriter( new FileOutputStream( targetFile ), getOutputEncoding() ) )
390410
{
391-
writer.write( buffer );
411+
r.render( filterMatches( cpd.getMatches() ), writer );
392412
writer.flush();
393-
394-
if ( includeXmlInSite )
395-
{
396-
File siteDir = getReportOutputDirectory();
397-
siteDir.mkdirs();
398-
FileUtils.copyFile( targetFile, new File( siteDir, "cpd." + format ) );
399-
}
400413
}
401414
catch ( IOException ioe )
402415
{
403416
throw new MavenReportException( ioe.getMessage(), ioe );
404417
}
418+
return targetFile;
405419
}
406420

407421
/**
@@ -423,10 +437,10 @@ private static ResourceBundle getBundle( Locale locale )
423437
* @return the renderer based on the configured output
424438
* @throws org.apache.maven.reporting.MavenReportException if no renderer found for the output type
425439
*/
426-
public Renderer createRenderer()
440+
public CPDRenderer createRenderer()
427441
throws MavenReportException
428442
{
429-
Renderer renderer = null;
443+
CPDRenderer renderer = null;
430444
if ( "xml".equals( format ) )
431445
{
432446
renderer = new XMLRenderer( getOutputEncoding() );
@@ -439,7 +453,7 @@ else if ( !"".equals( format ) && !"none".equals( format ) )
439453
{
440454
try
441455
{
442-
renderer = (Renderer) Class.forName( format ).newInstance();
456+
renderer = (CPDRenderer) Class.forName( format ).getConstructor().newInstance();
443457
}
444458
catch ( Exception e )
445459
{

0 commit comments

Comments
 (0)