FlexCPD (Copy & and Paste Detector)

FlexCPD is reusing the JavaCPD engine.
This project consists in giving a stream of tokens to the CPD engine using the FlexPMD lexer and let the CPD engine detects duplications in those streams.

How to invoke it?

At the moment FlexCPD can be invoked from:

  • The command-line
  • Ant
  • Maven

In command-line:

$ java -jar flex-pmd-cpd-command-line-1.0.RC5.jar 
Nov 25, 2009 4:06:41 AM com.adobe.ac.cpd.commandline.FlexCPD areCommandLineOptionsCorrect
SEVERE: Usage: java com.adobe.ac.cpd.commandline.FlexCPD (-s|--sourceDirectory) <sourceDirectory> 
(-o|--outputFile) <outputFile> [(-m|--minimumTokens) <minimumTokens>]]

In Ant, you add this to your build.xml:

build.xml
    <taskdef name="cpd" classname="com.adobe.ac.cpd.ant.FlexCpdAntTask"
         classpath="${flexpmd.libs}/flex-pmd-cpd-ant-task-${flexpmd.version}.jar">
      <classpath>
         <pathelement location="${flexpmd.libs}/flex-pmd-files-${flexpmd.version}.jar" />
         <pathelement location="${flexpmd.libs}/flex-pmd-cpd-${flexpmd.version}.jar" />
         <pathelement location="${flexpmd.libs}/as3-plugin-utils-${flexpmd.version}.jar" />
         <pathelement location="${flexpmd.libs}/as3-parser-${flexpmd.version}.jar" />
         <pathelement location="${flexpmd.libs}/as3-parser-api$-{flexpmd.version}.jar" />
         <pathelement location="${flexpmd.libs}/pmd-4.2.5.jar" />
      </classpath>
     </taskdef>

    <target name="cpd">
       <cpd minimumTokenCount="50" outputFile="${flexpmd.output}/cpd.xml">
          <fileset dir="${src}">
             <include name="**/*.as"/>
             <include name="**/*.mxml"/>
          </fileset>
       </cpd>
    </target>

In Maven:

pom.xml
            <plugin>
                <groupId>com.adobe.ac</groupId>
                <artifactId>flex-pmd-cpd-maven-plugin</artifactId>
                <version>1.0.RC5</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <minimumTokenCount>50</minimumTokenCount>
                </configuration>
            </plugin>

How to interpret the results

In Hudson, there is a plugin called DRY which let's the user dive into the CPD results:

Here is the global trend of the project on duplicate code:

Here is the result for a single build, highlighting the numbers of fixed and added duplications:

Here is the duplications on a single file: