Improving your code with FlexPMD

Last week Adobe released a nice tool called FlexPMD – it can help you and your development team to improve the quality of the code you generate by looking for common bad practices like i.e. unused or inefficient code.

To better understand how FlexPMD works and what it exactly does, have a look at “About FlexPMD”.

You can use default or custom rulesets to control what FlexPMD is looking for. With the Custom Ruleset Creator you can – guess what – create your own rulesets and check your code against these rules.

There are three ways to use FlexPMD with your projects:

  • from the command line
  • from Ant
  • from Maven

As we use FDT for all our programming, the easiest way to integrate FlexPMD with our current projects is via Ant. Unfortunately Adobe’s example is faulty, so I thought I should post a correct build.xml for all of you that are interested in using FlexPMD with Ant.

<project name="Flex PMD example" default="flexPmdWithDefaultRuleset" basedir="." >

    <description>Flex PMD example</description>

    <property name="src.dir" value="${basedir}/classes/" />
    <property name="bin.dir" value="${basedir}/../bin/" />
    
    <property name="flexpmd.version" value="1.0.RC3" />
    <property name="flexpmd.dir" value="${basedir}/../flexpmd" />

    <!--**************************************************** 
                FlexPMD
        *****************************************************-->

    <taskdef name="flexPmd" 
        classname="com.adobe.ac.pmd.ant.FlexPmdAntTask" 
        classpath="${flexpmd.dir}/flex-pmd-ant-task-${flexpmd.version}.jar">
        <classpath>
            <pathelement location="${flexpmd.dir}/flex-pmd-ruleset-api-${flexpmd.version}.jar"/>
            <pathelement location="${flexpmd.dir}/flex-pmd-ruleset-${flexpmd.version}.jar"/>
            <pathelement location="${flexpmd.dir}/flex-pmd-core-${flexpmd.version}.jar"/>
            <pathelement location="${flexpmd.dir}/as3-plugin-utils-${flexpmd.version}.jar"/>
            <pathelement location="${flexpmd.dir}/as3-parser-${flexpmd.version}.jar"/>
            <pathelement location="${flexpmd.dir}/pmd-4.2.2.jar"/>
            <pathelement location="${flexpmd.dir}/commons-lang-2.4.jar"/>
            <pathelement location="${flexpmd.dir}/flex-pmd-files-${flexpmd.version}.jar"/>
            <pathelement location="${flexpmd.dir}/as3-parser-api-${flexpmd.version}.jar"/>
            <pathelement location="${flexpmd.dir}/plexus-utils-1.0.2.jar"/>
        </classpath>
    </taskdef>

    <target name="flexPmdWithCustomRuleset">
        <flexPmd 
            sourceDirectory="${src.dir}" 
            outputDirectory="${bin.dir}" 
            ruleSet="${flexpmd.dir}/pmd.xml"/>
    </target>

    <target name="flexPmdWithDefaultRuleset">
        <flexPmd 
            sourceDirectory="${src.dir}" 
            outputDirectory="${bin.dir}"/>
    </target>

</project>

The Ant script creates a file containing all the found violations. You can view the file with Adobe’s online viewer.

You can download all versions of FlexPMD here: FlexPMD @ Open Source Adobe.

So hopefully – in the future – we’ll all write better code :)

Cheers.

Comments