Search code examples
jenkinsmsbuildpmdstatic-code-analysiscpd

How to generate xml report using CPD (Copy Paste Detector)?


I am using the CPD tool to find the Duplicate codes in my project. I have tried the command line options as given in this link CPD Usage. I want to generate the report in xml format and need to store it in a particular location. But it is showing the report in console itself and not store it anywhere. Refer this screenshot.

I tried this in CPD GUI and worked properly. But for my need i want to generate the report in xml file. Is this possible?

In the above link, they have given the information for ANT tool. So is there any way to do this in MSBuild tool?


Solution

  • Creating an XML file is possible using redirects for the output stream. CPD outputs the report to stdout. If you execute CPD in the command line, use the redirection operators:

    %PMD_PATH%\bin\cpd.bat --language cs --format xml --minimum-tokens 100 --files %SOURCE_DIR% > cpd-report.xml
    

    Inside MSBuild tool, you could use the Exec Task. Since it just calls cmd internally, the following should work (I didn't try it...):

    <PropertyGroup>
        <PmdPath>C:\pmd-bin-5.5.0</PmdPath>
        <SourceDir>C:\source</SourceDir>
    </PropertyGroup>
    <Target Name="CPD">
        <Exec Command="$(PmdPath)\bin\cpd.bat --language cs --format xml --minimum-tokens 100 --files $(SourceDir) > cpd-report.xml"/>
    </Target>
    

    You'll need to define the properties PmdPath and SourceDir with the correct directories.