Initial PMD plugin

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@168471 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Emmanuel Venisse 2005-05-05 23:49:06 +00:00
parent a790b9d0c5
commit 079c0d7f50
7 changed files with 448 additions and 1 deletions

View File

@ -0,0 +1,8 @@
target
*~
*.log
.classpath
.project
*.ipr
*.iws
*.iml

View File

@ -0,0 +1,50 @@
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>mojo</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>maven-pmd-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>Maven PMD Plugin</name>
<inceptionYear>2005</inceptionYear>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>doxia</groupId>
<artifactId>doxia-core</artifactId>
<version>1.0-alpha-2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven.reporting</groupId>
<artifactId>maven-reporting-api</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>pmd</groupId>
<artifactId>pmd</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.0-FCS</version>
</dependency>
<dependency>
<groupId>saxpath</groupId>
<artifactId>saxpath</artifactId>
<version>1.0-FCS</version>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.0-alpha-2</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,176 @@
package org.apache.maven.plugin.pmd;
/*
* Copyright 2005 The Apache Software Foundation.
*
* Licensed 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.
*/
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.PMDException;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSetFactory;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.doxia.sink.Sink;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* Implement the PMD report.
*
* @author Brett Porter
* @version $Id: PmdReport.java,v 1.3 2005/02/23 00:08:53 brett Exp $
*/
public class PmdReport
extends AbstractMavenReport
{
protected static final String[] DEFAULT_EXCLUDES = {
// Miscellaneous typical temporary files
"**/*~",
"**/#*#",
"**/.#*",
"**/%*%",
"**/._*",
// CVS
"**/CVS",
"**/CVS/**",
"**/.cvsignore",
// SCCS
"**/SCCS",
"**/SCCS/**",
// Visual SourceSafe
"**/vssver.scc",
// Subversion
"**/.svn",
"**/.svn/**",
// Mac
"**/.DS_Store"
};
public void execute()
throws MavenReportException
{
Sink sink = null;
try
{
sink = getSink();
}
catch( IOException e )
{
throw new MavenReportException( "Can't obtain sink for PMD report.", e );
}
PMD pmd = new PMD();
RuleContext ruleContext = new RuleContext();
Report report = new Report();
PmdReportListener reportSink = new PmdReportListener( sink );
report.addListener( reportSink );
ruleContext.setReport( report );
RuleSetFactory ruleSetFactory = new RuleSetFactory();
RuleSet ruleSet = ruleSetFactory.createRuleSet(
pmd.getClass().getResourceAsStream( "/rulesets/controversial.xml" ) );
reportSink.beginDocument();
List files;
try
{
files = getFiles( "**/*.java", null );
}
catch( IOException e )
{
throw new MavenReportException( "Can't parse " + getConfiguration().getSourceDirectory(), e );
}
for ( Iterator i = files.iterator(); i.hasNext(); )
{
File file = (File) i.next();
FileReader fileReader;
try
{
fileReader = new FileReader( file );
}
catch ( FileNotFoundException e )
{
throw new MavenReportException( "Error opening source file: " + file, e );
}
try
{
// TODO: lazily call beginFile in case there are no rules
reportSink.beginFile( file );
pmd.processFile( fileReader, ruleSet, ruleContext );
reportSink.endFile( file );
}
catch ( PMDException e )
{
throw new MavenReportException( "Failure executing PMD for: " + file, e );
}
finally
{
try
{
fileReader.close();
}
catch ( IOException e )
{
throw new MavenReportException( "Error closing source file: " + file, e );
}
}
}
reportSink.endDocument();
}
public String getOutputName()
{
return "pmd";
}
private List getFiles( String includes, String excludes )
throws IOException
{
StringBuffer excludesStr = new StringBuffer();
if ( StringUtils.isNotEmpty( excludes ) )
{
excludesStr.append(excludes);
}
for ( int i = 0; i < DEFAULT_EXCLUDES.length; i++ )
{
if ( excludesStr.length() > 0 )
{
excludesStr.append( "," );
}
excludesStr.append( DEFAULT_EXCLUDES[i] );
}
return FileUtils.getFiles( new File( getConfiguration().getSourceDirectory() ), "**/*", excludesStr.toString() );
}
}

View File

@ -0,0 +1,123 @@
package org.apache.maven.plugin.pmd;
/*
* Copyright 2005 The Apache Software Foundation.
*
* Licensed 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.
*/
import net.sourceforge.pmd.ReportListener;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.stat.Metric;
import org.codehaus.doxia.sink.Sink;
import java.io.File;
/**
* Handle events from PMD, converting them into Doxia events.
*
* @author Brett Porter
* @version $Id: PmdReportListener.java,v 1.1.1.1 2005/02/17 07:16:22 brett Exp $
*/
public class PmdReportListener
implements ReportListener
{
private Sink sink;
private static final String TITLE = "PMD Results";
public PmdReportListener( Sink sink )
{
this.sink = sink;
}
public void ruleViolationAdded( RuleViolation ruleViolation )
{
sink.tableRow();
sink.tableCell();
sink.text( ruleViolation.getDescription() );
sink.tableCell_();
sink.tableCell();
// TODO: xref link the line number
sink.text( String.valueOf( ruleViolation.getLine() ) );
sink.tableCell_();
sink.tableRow_();
}
public void metricAdded( Metric metric )
{
// TODO: metrics
}
public void beginDocument()
{
sink.head();
sink.title();
sink.text( TITLE );
sink.title_();
sink.head_();
sink.body();
sink.section1();
sink.sectionTitle();
sink.text( TITLE );
sink.sectionTitle_();
sink.paragraph();
sink.text( "The following document contains the results of " );
sink.link( "http://pmd.sourceforge.net/" );
sink.text( "PMD" );
sink.link_();
sink.paragraph_();
// TODO overall summary
sink.section1_();
sink.sectionTitle();
sink.text( "Files" );
sink.sectionTitle_();
// TODO files summary
}
public void beginFile( File file )
{
sink.section2();
sink.sectionTitle();
sink.text( file.getPath() );
sink.sectionTitle_();
sink.table();
sink.tableRow();
sink.tableHeaderCell();
sink.text( "Violation" );
sink.tableHeaderCell_();
sink.tableHeaderCell();
sink.text( "Line" );
sink.tableHeaderCell_();
sink.tableRow_();
}
public void endFile( File file )
{
sink.table_();
sink.section2_();
}
public void endDocument()
{
sink.section1_();
sink.body_();
}
}

View File

@ -0,0 +1,76 @@
package org.apache.maven.plugin.pmd;
/*
* Copyright 2005 The Apache Software Foundation.
*
* Licensed 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.
*/
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.MavenReport;
import org.apache.maven.reporting.MavenReportConfiguration;
import org.apache.maven.reporting.MavenReportException;
import java.io.File;
import java.io.FileWriter;
import java.util.HashSet;
import java.util.Set;
/**
* @goal pmd
* @description A Maven2 plugin which generates a PMD report
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id: PmdReportMojo.java,v 1.4 2005/02/23 00:08:54 brett Exp $
*/
public class PmdReportMojo
extends AbstractMojo
{
/**
* @parameter alias="workingDirectory" expression="${project.build.directory}/site-generated"
* @required
*/
private String outputDirectory;
/**
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
public void execute()
throws MojoExecutionException
{
MavenReportConfiguration config = new MavenReportConfiguration();
config.setModel( project.getModel() );
config.setOutputDirectory( new File( outputDirectory ) );
MavenReport report = new PmdReport();
report.setConfiguration( config );
try
{
report.generate();
}
catch ( MavenReportException e )
{
throw new MojoExecutionException( "An error is occurred in the PMD report generation.", e );
}
}
}

View File

@ -0,0 +1,15 @@
<component-set>
<!-- TODO:
- this should be generated using cdc
- need to configure the reports
- the hint should perhaps use a qualifier -> this is equivalent to how we must deal with plugins outside of the maven group ID
-->
<components>
<component>
<role>org.apache.maven.reporting.MavenReport</role>
<role-hint>pmd</role-hint>
<implementation>org.apache.maven.plugin.pmd.PmdReport</implementation>
<instantiation-strategy>per-lookup</instantiation-strategy>
</component>
</components>
</component-set>

View File

@ -5,7 +5,6 @@
<groupId>org.apache.maven.plugins</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>