1

I'm currently trying out PMD as a possible static analysis tool that our company can use. I've analyzed Java files with no problems whatsoever, but I couldn't seem to do it with Javascript, everytime I execute pmd:pmd it just analyses java files again. Anyways, here is a snippet of my POM.xml:

*Using Maven 3.3.1

<build>
  <plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
        <sources>
            ${basedir}/src/main/webapp/js
        </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.7</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>

So I just followed the instructions indicated in this but can't seem to make it work. Can you guys please tell me if I'm missing some required setups / configurations ? TIA.

jeromecris
  • 45
  • 12

1 Answers1

1

Your configuration is correct (assuming the missing <plugins> tags are simply edit issues). Most probably you are not running the proper maven targets.

With your current configuration, PMD will just be run as a report during site generation, that is mvn site. However, if doing that, the build-helper-maven-plugin:add-source target would not run, and the sources would not be found.

The most basic (useless) way around this, is simply calling mvn generate-sources site.

You can have the add-source run automatically on mvn site by changing the plugin config as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>config-js</id>
                    <phase>pre-site</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>${basedir}/src/main/javascript</sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Here, the phase set to pre-site does the magic of hooking the execution to the beginning of the site lifecycle.

If you want to be able to run PMD using mvn pmd:pmd or mvn pmd:check, then your configuration should be slightly different. The PMD plugin should not be part of the <reporting> section, but part of <build>. Unfortunately, the PMD maven plugin doesn't hook itself to a lifecycle event, so on this case, we have to manually make sure build-helper-maven-plugin:add-source is run. Once again, we can do so with mvn generate-sources pmd:pmd

Johnco
  • 4,037
  • 4
  • 34
  • 43
  • 1
    `build-helper-maven-plugin:add-source` should not be needed anymore with maven-pmd-plugin 3.7 - as long as you configure `compileSourceRoots` like in the [example](https://maven.apache.org/plugins/maven-pmd-plugin/examples/javascriptReport.html) - **if you use maven 3.3.9** (see [MPMD-212](https://issues.apache.org/jira/browse/MPMD-212) and [MPMD-206](https://issues.apache.org/jira/browse/MPMD-206)) – adangel Feb 06 '17 at 19:25
  • @johnco It is working now. Just had to transfer the pmd plugin inside and executed mvn pmd:pmd. All I need to do now is to pass the result to the maven site. Thank you very much. – jeromecris Feb 07 '17 at 00:06