From 77b905b1129e73cc0d7467ae5e27e8c5d4ecd484 Mon Sep 17 00:00:00 2001 From: Emmanuel Venisse Date: Fri, 6 May 2005 01:44:15 +0000 Subject: [PATCH] Initial version of Checkstyle Plugin git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@168490 13f79535-47bb-0310-9956-ffa450edef68 --- .../maven-checkstyle-plugin/pom.xml | 70 ++++++ .../plugin/checkstyle/CheckstyleReport.java | 229 ++++++++++++++++++ .../checkstyle/CheckstyleReportMojo.java | 76 ++++++ .../resources/META-INF/plexus/components.xml | 15 ++ .../main/resources/config/avalon_checks.xml | 207 ++++++++++++++++ .../src/main/resources/config/sun_checks.xml | 191 +++++++++++++++ .../main/resources/config/turbine_checks.xml | 202 +++++++++++++++ 7 files changed, 990 insertions(+) create mode 100644 sandbox/maven-reports/maven-checkstyle-plugin/pom.xml create mode 100644 sandbox/maven-reports/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java create mode 100644 sandbox/maven-reports/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportMojo.java create mode 100644 sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/META-INF/plexus/components.xml create mode 100644 sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/avalon_checks.xml create mode 100644 sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/sun_checks.xml create mode 100644 sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/turbine_checks.xml diff --git a/sandbox/maven-reports/maven-checkstyle-plugin/pom.xml b/sandbox/maven-reports/maven-checkstyle-plugin/pom.xml new file mode 100644 index 0000000000..980a876d9a --- /dev/null +++ b/sandbox/maven-reports/maven-checkstyle-plugin/pom.xml @@ -0,0 +1,70 @@ + + 4.0.0 + + mojo + org.apache.maven.plugins + 1.0-SNAPSHOT + + maven-checkstyle-plugin + 1.0-SNAPSHOT + maven-plugin + Maven Checkstyle Plugin + 2005 + + + org.apache.maven + maven-project + 2.0-SNAPSHOT + + + doxia + doxia-core + 1.0-alpha-2-SNAPSHOT + + + org.apache.maven.reporting + maven-reporting-api + 2.0-SNAPSHOT + + + plexus + plexus-utils + 1.0-alpha-2 + + + checkstyle + checkstyle + 3.4 + + + checkstyle + checkstyle-optional + 3.4 + + + antlr + antlr + 2.7.2 + + + regexp + regexp + 1.3 + + + commons-beanutils + commons-beanutils + 1.6.1 + + + commons-collections + commons-collections + 2.1 + + + commons-logging + commons-logging + 1.0.3 + + + diff --git a/sandbox/maven-reports/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java new file mode 100644 index 0000000000..c6cedcabb3 --- /dev/null +++ b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java @@ -0,0 +1,229 @@ +package org.apache.maven.plugin.checkstyle; + +/* + * Copyright 2004-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.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 com.puppycrawl.tools.checkstyle.api.AuditListener; +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.api.Configuration; +import com.puppycrawl.tools.checkstyle.ConfigurationLoader; +import com.puppycrawl.tools.checkstyle.Checker; +import com.puppycrawl.tools.checkstyle.DefaultLogger; +import com.puppycrawl.tools.checkstyle.ModuleFactory; +import com.puppycrawl.tools.checkstyle.PackageNamesLoader; +import com.puppycrawl.tools.checkstyle.XMLLogger; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Iterator; +import java.util.List; + +/** + * @author Emmanuel Venisse + * @version $Id: DependenciesReport.java,v 1.2 2005/02/23 00:08:02 brett Exp $ + */ +public class CheckstyleReport + 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" + }; + + private URL configFile = getClass().getResource( "/config/sun_checks.xml" ); + + private String extraFormatter = "plain"; + + private String resultFileName = "checkstyle-result.txt"; + + private String packageNamesFile; + + private boolean failedOnError = false; + + public void execute() + throws MavenReportException + { + // ---------------------------------------------------------------------- + // + // ---------------------------------------------------------------------- + + AuditListener listener = null; + + if ( StringUtils.isNotEmpty( extraFormatter ) ) + { + FileOutputStream out; + File resultFile = new File( getConfiguration().getOutputDirectory(), resultFileName ); + try + { + out = new FileOutputStream( resultFile ); + } + catch( IOException e ) + { + throw new MavenReportException( "Can't access to " + resultFile.getAbsolutePath(), e ); + } + + if ( "xml".equals( extraFormatter ) ) + { + listener = new XMLLogger( out, true ); + } + else if ( "plain".equals( extraFormatter ) ) + { + listener = new DefaultLogger( out, true ); + } + else + { + throw new MavenReportException( "Invalid format: (" + extraFormatter + "). Must be 'plain' or 'xml'." ); + } + } + + File[] files; + try + { + List filesList = getFilesToProcess( "**/*.java", null ); + files = new File[filesList.size()]; + int i = 0; + for ( Iterator iter = filesList.iterator(); iter.hasNext(); ) + { + files[i++] = (File) iter.next(); + } + } + catch( IOException e ) + { + throw new MavenReportException( "Can't parse " + getConfiguration().getSourceDirectory(), e ); + } + + Configuration config; + + try + { + //TODO: implements a PropertyResolver for resolve property like ${checkstyle.cache.file} + config = ConfigurationLoader.loadConfiguration( configFile.toString(), null ); + } + catch ( CheckstyleException e ) + { + throw new MavenReportException( "Error loading config file : " + configFile.toString(), e ); + } + + ModuleFactory moduleFactory = null; + + if ( StringUtils.isNotEmpty( packageNamesFile ) ) + { + try + { + moduleFactory = PackageNamesLoader.loadModuleFactory( packageNamesFile ); + } + catch ( CheckstyleException e ) + { + throw new MavenReportException( "Error loading package names file : " + packageNamesFile, e ); + } + } + + Checker checker = null; + + try + { + checker = new Checker(); + + checker.setModuleFactory( moduleFactory ); + + checker.configure( config ); + + FileOutputStream xmlOut = new FileOutputStream( new File( getConfiguration().getOutputDirectory(), "checkstyle-result.xml" ) ); + + //TODO: Use a Listener with sink + AuditListener xmlListener = new XMLLogger( xmlOut, true ); + + if ( listener != null ) + { + checker.addListener( listener ); + } + + checker.addListener( xmlListener ); + } + catch ( Exception e ) + { + throw new MavenReportException( "Unable to create Checker: " + e.getMessage(), e ); + } + + int nbErrors = checker.process( files ); + + if ( checker != null ) + { + checker.destroy(); + } + + if ( failedOnError && nbErrors > 0 ) + { + throw new MavenReportException( "There are " + nbErrors + " formatting errors." ); + } + } + + public String getOutputName() + { + return "checkstyle"; + } + + private List getFilesToProcess( 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() ), includes, excludesStr.toString() ); + } +} \ No newline at end of file diff --git a/sandbox/maven-reports/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportMojo.java b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportMojo.java new file mode 100644 index 0000000000..fa357b68d9 --- /dev/null +++ b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReportMojo.java @@ -0,0 +1,76 @@ +package org.apache.maven.plugin.checkstyle; + +/* + * 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 checkstyle + * @description A Maven2 plugin which generates a Checkstyle report + * + * @author Emmanuel Venisse + * @version $Id: PmdReportMojo.java,v 1.4 2005/02/23 00:08:54 brett Exp $ + */ +public class CheckstyleReportMojo + 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 CheckstyleReport(); + + report.setConfiguration( config ); + + try + { + report.generate(); + } + catch ( MavenReportException e ) + { + throw new MojoExecutionException( "An error is occurred in the Checkstyle report generation.", e ); + } + } +} diff --git a/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/META-INF/plexus/components.xml b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/META-INF/plexus/components.xml new file mode 100644 index 0000000000..a184ba1c09 --- /dev/null +++ b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/META-INF/plexus/components.xml @@ -0,0 +1,15 @@ + + + + + org.apache.maven.reporting.MavenReport + checkstyle + org.apache.maven.plugin.checkstyle.CheckstyleReport + per-lookup + + + diff --git a/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/avalon_checks.xml b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/avalon_checks.xml new file mode 100644 index 0000000000..50c020b5c8 --- /dev/null +++ b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/avalon_checks.xml @@ -0,0 +1,207 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/sun_checks.xml b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/sun_checks.xml new file mode 100644 index 0000000000..bc49e25a77 --- /dev/null +++ b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/sun_checks.xml @@ -0,0 +1,191 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/turbine_checks.xml b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/turbine_checks.xml new file mode 100644 index 0000000000..00868c3bd2 --- /dev/null +++ b/sandbox/maven-reports/maven-checkstyle-plugin/src/main/resources/config/turbine_checks.xml @@ -0,0 +1,202 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +