Author: olamy Date: Mon May 21 19:59:38 2012 New Revision: 1341178 URL: http://svn.apache.org/viewvc?rev=1341178&view=rev Log: [MPMD-148] Add support for javascript / ecmascript Submitted by Andreas Dangel.
Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml (with props) maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/Sample.js (with props) Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java?rev=1341178&r1=1341177&r2=1341178&view=diff ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java (original) +++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java Mon May 21 19:59:38 2012 @@ -19,6 +19,16 @@ package org.apache.maven.plugin.pmd; * under the License. */ +import net.sourceforge.pmd.PMD; +import org.apache.maven.doxia.siterenderer.Renderer; +import org.apache.maven.model.ReportPlugin; +import org.apache.maven.project.MavenProject; +import org.apache.maven.reporting.AbstractMavenReport; +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.PathTool; +import org.codehaus.plexus.util.ReaderFactory; +import org.codehaus.plexus.util.StringUtils; + import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -31,17 +41,6 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; -import net.sourceforge.pmd.PMD; - -import org.apache.maven.doxia.siterenderer.Renderer; -import org.apache.maven.model.ReportPlugin; -import org.apache.maven.project.MavenProject; -import org.apache.maven.reporting.AbstractMavenReport; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.PathTool; -import org.codehaus.plexus.util.ReaderFactory; -import org.codehaus.plexus.util.StringUtils; - /** * Base class for the PMD reports. * @@ -203,13 +202,17 @@ public abstract class AbstractPmdReport */ protected List<MavenProject> reactorProjects; - /** {@inheritDoc} */ + /** + * {@inheritDoc} + */ protected MavenProject getProject() { return project; } - /** {@inheritDoc} */ + /** + * {@inheritDoc} + */ protected Renderer getSiteRenderer() { return siteRenderer; @@ -222,8 +225,8 @@ public abstract class AbstractPmdReport { File xrefLoc = test ? xrefTestLocation : xrefLocation; - String relativePath = PathTool.getRelativePath( outputDirectory.getAbsolutePath(), - xrefLoc.getAbsolutePath() ); + String relativePath = + PathTool.getRelativePath( outputDirectory.getAbsolutePath(), xrefLoc.getAbsolutePath() ); if ( StringUtils.isEmpty( relativePath ) ) { relativePath = "."; @@ -237,9 +240,8 @@ public abstract class AbstractPmdReport else { // Not yet generated - check if the report is on its way - @SuppressWarnings( "unchecked" ) - List<ReportPlugin> reportPlugins = project.getReportPlugins(); - for ( ReportPlugin plugin : reportPlugins ) + @SuppressWarnings( "unchecked" ) List<ReportPlugin> reportPlugins = project.getReportPlugins(); + for ( ReportPlugin plugin : reportPlugins ) { String artifactId = plugin.getArtifactId(); if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) ) @@ -278,7 +280,7 @@ public abstract class AbstractPmdReport { excludeRoots = new File[0]; } - + Collection<File> excludeRootFiles = new HashSet<File>( excludeRoots.length ); for ( int i = 0; i < excludeRoots.length; i++ ) @@ -317,8 +319,8 @@ public abstract class AbstractPmdReport { for ( MavenProject localProject : reactorProjects ) { - @SuppressWarnings( "unchecked" ) - List<String> localCompileSourceRoots = localProject.getCompileSourceRoots(); + @SuppressWarnings( "unchecked" ) List<String> localCompileSourceRoots = + localProject.getCompileSourceRoots(); for ( String root : localCompileSourceRoots ) { File sroot = new File( root ); @@ -326,8 +328,8 @@ public abstract class AbstractPmdReport } if ( includeTests ) { - @SuppressWarnings( "unchecked" ) - List<String> localTestCompileSourceRoots = localProject.getTestCompileSourceRoots(); + @SuppressWarnings( "unchecked" ) List<String> localTestCompileSourceRoots = + localProject.getTestCompileSourceRoots(); for ( String root : localTestCompileSourceRoots ) { File sroot = new File( root ); @@ -347,11 +349,12 @@ public abstract class AbstractPmdReport for ( PmdFileInfo finfo : directories ) { + getLog().debug( "Searching for files in directory " + finfo.getSourceDirectory().toString() ); File sourceDirectory = finfo.getSourceDirectory(); if ( sourceDirectory.isDirectory() && !excludeRootFiles.contains( sourceDirectory ) ) { - @SuppressWarnings( "unchecked" ) - List<File> newfiles = FileUtils.getFiles( sourceDirectory, including, excluding ); + @SuppressWarnings( "unchecked" ) List<File> newfiles = + FileUtils.getFiles( sourceDirectory, including, excluding ); for ( Iterator<File> it2 = newfiles.iterator(); it2.hasNext(); ) { files.put( it2.next(), finfo ); @@ -388,8 +391,8 @@ public abstract class AbstractPmdReport */ private String getExcludes() { - @SuppressWarnings( "unchecked" ) - Collection<String> patterns = new LinkedHashSet<String>( FileUtils.getDefaultExcludesAsList() ); + @SuppressWarnings( "unchecked" ) Collection<String> patterns = + new LinkedHashSet<String>( FileUtils.getDefaultExcludesAsList() ); if ( excludes != null ) { patterns.addAll( excludes ); @@ -402,7 +405,9 @@ public abstract class AbstractPmdReport return "html".equals( format ); } - /** {@inheritDoc} */ + /** + * {@inheritDoc} + */ public boolean canGenerateReport() { if ( aggregate && !project.isExecutionRoot() ) @@ -436,7 +441,9 @@ public abstract class AbstractPmdReport return true; } - /** {@inheritDoc} */ + /** + * {@inheritDoc} + */ protected String getOutputDirectory() { return outputDirectory.getAbsolutePath(); Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java?rev=1341178&r1=1341177&r2=1341178&view=diff ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java (original) +++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java Mon May 21 19:59:38 2012 @@ -84,6 +84,17 @@ public class PmdReport private String targetJdk; /** + * The programming language to be analyzed by PMD. Valid values are currently <code>java</code> + * and <code>ecmascript</code> or <code>javascript</code>. + * <p> + * <b>Note:</b> if the parameter targetJdk is given, then this language parameter will be ignored. + * </p> + * + * @parameter default-value="java" + */ + private String language; + + /** * The rule priority threshold; rules with lower priority * than this will not be evaluated. * @@ -237,6 +248,11 @@ public class PmdReport try { files = getFilesToProcess(); + if ( files.isEmpty() && !"java".equals( language ) ) + { + getLog().warn( + "No files found to process. Did you add your additional source folders like javascript? (see also build-helper-maven-plugin)" ); + } } catch ( IOException e ) { @@ -378,14 +394,23 @@ public class PmdReport throws MavenReportException { PMDConfiguration configuration = new PMDConfiguration(); + LanguageVersion languageVersion = null; if ( null != targetJdk ) { - LanguageVersion languageVersion = LanguageVersion.findByTerseName( "java " + targetJdk ); + languageVersion = LanguageVersion.findByTerseName( "java " + targetJdk ); if ( languageVersion == null ) { throw new MavenReportException( "Unsupported targetJdk value '" + targetJdk + "'." ); } + } + else if ( "javascript".equals( language ) || "ecmascript".equals( language ) ) + { + languageVersion = LanguageVersion.ECMASCRIPT; + } + if ( languageVersion != null ) + { + getLog().debug( "Using language " + languageVersion ); configuration.setDefaultLanguageVersion( languageVersion ); } Modified: maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java?rev=1341178&r1=1341177&r2=1341178&view=diff ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java (original) +++ maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/PmdReportTest.java Mon May 21 19:59:38 2012 @@ -86,6 +86,37 @@ public class PmdReportTest } + public void testJavascriptConfiguration() + throws Exception + { + File testPom = new File( getBasedir(), + "src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml" ); + PmdReport mojo = (PmdReport) lookupMojo( "pmd", testPom ); + mojo.execute(); + + // check if the PMD files were generated + File generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/pmd.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + // these are the rulesets, that have been applied... + generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/ecmascript-basic.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/ecmascript-braces.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + generatedFile = + new File( getBasedir(), "target/test/unit/default-configuration/target/ecmascript-unnecessary.xml" ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + generatedFile = new File( getBasedir(), "target/test/unit/default-configuration/target/site/pmd.html" ); + renderer( mojo, generatedFile ); + assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) ); + + String str = readFile( generatedFile ); + assertTrue( str.indexOf( "Avoid using global variables" ) != -1 ); + } + public void testFileURL() throws Exception { Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml?rev=1341178&view=auto ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml (added) +++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml Mon May 21 19:59:38 2012 @@ -0,0 +1,73 @@ +<!-- +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> + <modelVersion>4.0.0</modelVersion> + <groupId>def.configuration</groupId> + <artifactId>default-configuration</artifactId> + <packaging>jar</packaging> + <version>1.0-SNAPSHOT</version> + <inceptionYear>2006</inceptionYear> + <name>Maven PMD Plugin Javascript Configuration Test</name> + <url>http://maven.apache.org</url> + <build> + <finalName>default-configuration</finalName> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-pmd-plugin</artifactId> + <configuration> + <project implementation="org.apache.maven.plugin.pmd.stubs.DefaultConfigurationMavenProjectStub"/> + <outputDirectory>${basedir}/target/test/unit/default-configuration/target/site</outputDirectory> + <targetDirectory>${basedir}/target/test/unit/default-configuration/target</targetDirectory> + <format>xml</format> + <sourceEncoding>UTF-8</sourceEncoding> + <language>javascript</language> + <rulesets> + <ruleset>ecmascript-basic</ruleset> + <ruleset>ecmascript-braces</ruleset> + <ruleset>ecmascript-unnecessary</ruleset> + </rulesets> + + <includes> + <include>**/*.js</include> + </includes> + <compileSourceRoots> + <compileSourceRoot>${basedir}/src/test/resources/unit/default-configuration/js/</compileSourceRoot> + </compileSourceRoots> + </configuration> + <dependencies> + <dependency> + <groupId>pmd</groupId> + <artifactId>pmd</artifactId> + <version>3.6</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jxr-plugin</artifactId> + </plugin> + </plugins> + </reporting> +</project> Propchange: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/javascript-configuration-plugin-config.xml ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/Sample.js URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/Sample.js?rev=1341178&view=auto ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/Sample.js (added) +++ maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/Sample.js Mon May 21 19:59:38 2012 @@ -0,0 +1,5 @@ +(function() { + + globalVariable = 1; + +})(); \ No newline at end of file Propchange: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/Sample.js ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/default-configuration/js/Sample.js ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision