Analyzing JavaScript Code

The PMD plugin analyzes by default java. You can configure it to analyze JavaScript files instead as shown below.

The example assumes that the javascript source code is stored in the subfolder src/main/javascript and enables three built-in rulesets (basic, braces, unnecessary).

Note that you have to make sure that the build-helper-maven-plugin is executed, so that the additional source folder is actually added. To generate the site report, you can include e.g. the generate-sources phase:

mvn generate-sources site

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.7</version>
        <configuration>
          <sources>
            <source>${basedir}/src/main/javascript/</source>
          </sources>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>add-source</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.0.1</version>
        <configuration>
          <language>javascript</language>
          <rulesets>
            <ruleset>ecmascript-basic</ruleset>
            <ruleset>ecmascript-braces</ruleset>
            <ruleset>ecmascript-unnecessary</ruleset>
          </rulesets>
          <includes>
            <include>**/*.js</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>