Exporting Metrics Using a Headless Build With Ant

To export the metrics using a headless build, there are two steps, both of which can be contained in an Ant build file:

<project name="metrics" default="export">

    <!-- This target actually exports the metrics. It runs
         inside Eclipse after the "launch" target launches
         Eclipse in headless mode -->
    <target name="export">
        <eclipse.refreshLocal
                resource="ECLIPSE_PROJECT_NAME"
                depth="infinite"/>
        
        <eclipse-metrics.export
                eclipseProject="ECLIPSE_PROJECT_NAME"
                dir="YOUR_EXPORT_DIRECTORY_HERE"
                html="true"
                htmlRowsPerPage="10"
                htmlDashboard="true"
                csv="true"/>
            <compilationUnits dir="src">
                <exclude name="**/*Bean.java"/>
            </compilationUnits>
        </eclipse-metrics.export
    </target>
    
    <!-- This target launches Eclipse in headless mode and runs
         the default target ("export") of this build file -->
    <target name="launch">
        <java fork="true"
              jar="ECLIPSE_INSTALL_DIR/plugins/org.eclipse.equinox.launcher_VERSION.jar">
            <jvmarg value="-Djava.awt.headless=true"/>    <!-- Only for MacOS X -->
            <arg value="-application"/>
            <arg value="org.eclipse.ant.core.antRunner"/>
            <arg value="-buildfile"/>
            <arg value="THE_NAME_OF_THIS_BUILD_FILE"/>
            <arg value="-data"/>
            <arg value="PATH_TO_YOUR_WORKSPACE"/>
        </java>
    </target>
    
</project>
        

Change the parts in capitals to appropriate values for your setup.

Run ant specifying the "launch" target. This should launch Eclipse in headless mode and produce the metrics. You can put the two targets in different Ant build files if you like. The "export" target must be the default target wherever it is placed (you can of course call it anything you like as long as it is the default target).

The above procedure does not work on some versions of Eclipse Europa running under Windows. This problem appears to have been fixed in Ganymede.

The attributes of the eclipse-metrics.export task are:

Attribute NameTypeRequiredDefaultDescription
eclipseProjectStringYesThe name of the project in the Eclipse workspace
dirStringYesThe directory to which the metrics will be written
htmlBooleanNotrueWhether to export the metrics as HTML
htmlRowsPerPageIntegerNoInteger.MAX_VALUEThe number of rows per page when exporting metrics as HTML
htmlDashboardBooleanNotrueWhether to produce the 'dashboard' page when exporting metrics as HTML
htmlJavaBooleanNofalseWhether to export the Java source and link to it from the HTML metrics tables
csvBooleanNotrueWhether to export the metrics as CSV

The eclipse-metrics.export task supports an optional compilationUnits child element. This is an ant FileSet. The dir attribute for this element must refer to one of the Eclipse project's source folders. The compilationUnits child element may be repeated as often as necessary to include / exclude all of the required source files. If this element is omitted, the configuration defined in the Metrics Properties page (within Eclipse) for the project will be used.


This plugin is provided by State Of Flow