jeromecris jeromecris - 4 years ago 173
Javascript Question

Analyzing Javascript with PMD Maven

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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download