Using Flex PMD for Flex Code Review using ANT

Hi All,

Adobe Technical Services launched FlexPMD – a tool to audit  ActionScript source code and detect common problems (anti patterns, unused code, badly written Flex components ). The tool started as an internal project in Adobe and now it has been made open source.

I can say that using this tool is a must for any complex Flex projects, because it enables you to detect common problems at an early phase. For the moment it can be invoked from Maven/Ant and Mac OS X automaton, in the future it will probably also be available as a Flash Builder plugin.

Below I will detailed out how to use this with ANT script, to get a report.

Step 1: Download the required jars.
http://opensource.adobe.com/wiki/display/flexpmd/Downloads

Step 2: Copy the jars in Lib folder e.g  c:\myproject\lib

Step 3: Write an ant script and add those jars into Classpath.

Flex PMD example
<mkdir dir=”${basedir}/flex/” />
<mkdir dir=”${basedir}/bin1/” />
<property name=”src.dir” value=”${basedir}/flex/” />
bin1/” />

<property name=”flexpmd.version” value=”1.1″ />
<property name=”flexpmd.dir” value=”jars/flex-pmd-ant-task-1.1/” />

<!–****************************************************
FlexPMD
*****************************************************–>

<taskdef name=”flexPmd”
classname=”com.adobe.ac.pmd.ant.FlexPmdAntTask”
classpath=”${flexpmd.dir}/flex-pmd-ant-task-1.1.jar”>
<classpath>
<pathelement location=”${flexpmd.dir}/flex-pmd-ruleset-api-1.1.jar”/>
<pathelement location=”${flexpmd.dir}/flex-pmd-ruleset-1.1.jar”/>
<pathelement location=”${flexpmd.dir}/flex-pmd-core-1.1.jar”/>
<pathelement location=”${flexpmd.dir}/as3-plugin-utils-1.1.jar”/>
<pathelement location=”${flexpmd.dir}/as3-parser-api-1.1.jar”/>
<pathelement location=”${flexpmd.dir}/pmd-4.2.5.jar”/>
<pathelement location=”${flexpmd.dir}/commons-lang-2.4.jar”/>
<pathelement location=”${flexpmd.dir}/flex-pmd-files-1.1.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>

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

Step 4: Run the ant scripts  e.g  >ant flexPmdWithDefaultRuleset

Step 5: After running the ant task, you will see the task output details on the Console.

flexPmdWithDefaultRuleset:
[flexPmd] 01-Jun-2010 20:34:09 com.adobe.ac.pmd.engines.AbstractFlexPmdEngine loadRuleset
[flexPmd] INFO: Ruleset: c:\DOCUME~1\603\LOCALS~1\Temp\default_flex5303689465491432723.xml
[flexPmd] 01-Jun-2010 20:34:09 com.adobe.ac.pmd.engines.AbstractFlexPmdEngine loadRuleset
[flexPmd] INFO: Rules number in the ruleSet: 90
[flexPmd] 01-Jun-2010 20:35:01 com.adobe.ac.pmd.engines.AbstractFlexPmdEngine computeViolations
[flexPmd] INFO: It took 51497ms to compute violations
[flexPmd] 01-Jun-2010 20:35:01 com.adobe.ac.pmd.engines.AbstractFlexPmdEngine computeViolationNumber
[flexPmd] INFO: Violations number found: 3256
[flexPmd] 01-Jun-2010 20:35:01 com.adobe.ac.pmd.engines.FlexPmdXmlEngine writeReport
[flexPmd] INFO: Creating report in
[flexPmd] 01-Jun-2010 20:35:01 com.adobe.ac.pmd.engines.AbstractFlexPmdEngine writeAnyReport
[flexPmd] INFO: It took 390ms to write the Xml report
BUILD SUCCESSFUL

The main things is that it works well on MXML files. Covers all the action script code written.

Also we can easily transform it to some HTML Report by using XSL. Simply give the path of generated report and path of XSL and the location of HTML report.

<xslt in=”${bin.dir}/pmd.xml” style=”pmd-nicerhtml.xsl” out=”bin1/pmd.html” />
Tip: XSL can be found inside pmd-4.2.5.jar, etc folder


Thanks
R Vashi

Advertisements

3 thoughts on “Using Flex PMD for Flex Code Review using ANT

  1. Rakesh

    Hi Vashi,

    Thanks for the article. Its very neatly written and covers all the basic needs to get a proper output. Thanks for your time and effort.

    Reply
  2. Jay

    Thanks for the example. It helped me get started with Flex PMD.

    One note that you might want to look into is the quotes came out as the Microsoft Smart Quotes when I pasted it into my editor. It looks wordpress is doing that.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s