A Sample Groovy MetricSet
Here is an example of a Groovy MetricSet file:
import org.gmetrics.metric.cyclomatic.CyclomaticComplexityMetric
metricset {
description 'A sample MetricSet'
metricset("MyMetricSet.groovy")
metric(org.gmetrics.metric.abc.AbcMetric) {
functions = ['average', 'maximum']
}
metric(CyclomaticComplexityMetric)
metricset("somepath/MyOtherMetricSet.groovy") {
ClassLineCount {
enabled = false
}
MethodLineCount name:'CustomMethodLineCount'
}
}
Things to note:
- The Groovy MetricSet file itself must be accessible on the classpath.
- The "outer" metricset defines the contents of the MetricSet (within a closure). It can include an optional description and any combination of metricset (other MetricSets) and metric statements (individual Metrics).
About the "inner" metricset statements:
- Each metricset statement loads a MetricSet file. The path specifies a Groovy file. By default, the paths specified are relative to the classpath. But these paths may be optionally prefixed by any of the valid java.net.URL prefixes, such as "file:" (to load from a relative or absolute path on the filesystem), or "http:".
- The MetricSet can be customized by following the MetricSet path with an (optional) closure, containing configuration of the properties for individual metrics within the MetricSet. Specify the name of the metric (as a String), followed by its configuration. (Remember to specify the metric name, not the class name. In most cases, the metric name is the class name without the "Metric" suffix). The name of the Metric is followed by:
- A Map of property names and values. See 'MethodLineCount' within the example.
- A closure containing property assignments statements. See 'ClassLineCount' within the example.
About the metric statements:
- Each metric statements loads a single Metric.
- The metric statement must specify the class name for a Metric. The Metric class must be available on the classpath.
- A metric can optionally provide configuration of the Metric properties by specifying a closure containing property assignment statements. See AbcMetric within the example. As within MetricSet statements, properties set this way can be of type String, int, long or boolean.
- Setting the enabled property of a Metric turns it off so that it will not show up in the output report(s).
Turning off a Metric
You can turn off a Metric by setting its enabled boolean property to false. It defaults to true. This applies to all descendants of org.gmetrics.metric.AbstractMetric (i.e., all Metrics supplied with GMetrics).