maven-plugins/clover/plugin.jelly
2006-09-21 22:08:47 +00:00

632 lines
24 KiB
XML

<?xml version="1.0"?>
<!--
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<project
xmlns:multiproject="multiproject"
xmlns:j="jelly:core"
xmlns:ant="jelly:ant"
xmlns:maven="jelly:maven"
xmlns:java="java"
xmlns:test="test"
xmlns:doc="doc"
xmlns:u="jelly:util">
<!--
========================================================================
Initialize the Clover plugin.
========================================================================
-->
<goal name="clover:init-internal">
<ant:path id="clover.classpath">
<ant:pathelement path="${plugin.getDependencyPath('com.cenqua.clover:clover')}"/>
</ant:path>
<ant:taskdef resource="clovertasks" classpathref="clover.classpath"/>
<ant:typedef resource="clovertypes" classpathref="clover.classpath"/>
<!-- Check for the deprecated maven.clover.database.dir property -->
<j:choose>
<j:when test="${context.getVariable('maven.clover.database.dir') != null}">
<ant:echo>Usage of the maven.clover.database.dir property is deprecated. Use the maven.clover.database property instead.</ant:echo>
<j:set var="maven.clover.database"
value="${maven.clover.database.dir}/clover_coverage.db"/>
</j:when>
<j:otherwise>
<ant:dirname property="maven.clover.database.dir" file="${maven.clover.database}"/>
</j:otherwise>
</j:choose>
<!-- Add the Clover jar to the CP so that it is systematically there. This is required
for example for running unit tests on Clovered code. -->
<maven:addPath id="maven.dependency.classpath" refid="clover.classpath"/>
<ant:mkdir dir="${maven.clover.build.classes}"/>
<ant:mkdir dir="${maven.clover.database.dir}"/>
<!-- Add the Clover license to the classpath. -->
<j:if test="${!empty(context.getVariable('maven.clover.license.path'))}">
<j:invokeStatic className="java.lang.System" method="setProperty">
<j:arg type="java.lang.String" value="clover.license.path"/>
<j:arg type="java.lang.String" value="${maven.clover.license.path}"/>
</j:invokeStatic>
</j:if>
<u:file var="historyDirAsFile" name="${maven.clover.history.dir}"/>
</goal>
<goal name="clover:init">
<!-- Ensure intialization is called only once -->
<j:if test="${!cloverInitialized}">
<attainGoal name="clover:init-internal"/>
<j:set var="cloverInitialized" value="true"/>
</j:if>
</goal>
<!--
========================================================================
Generate test coverage reports with Clover.
========================================================================
-->
<goal name="clover" description="Generate test coverage reports with Clover">
<!-- Is it a multiproject setup? -->
<!-- TODO: Find a way to call clover:init only once below -->
<j:choose>
<j:when test="${maven.clover.multiproject}">
<attainGoal name="clover:multiproject"/>
</j:when>
<j:otherwise>
<attainGoal name="clover:test"/>
</j:otherwise>
</j:choose>
<attainGoal name="clover:report"/>
</goal>
<!--
========================================================================
Prepare for Clover. Any goal that executes after this goal and that
runs the <javac> Ant task will result in sources being Clovered.
Clover sets the Ant build.compiler property that tells the <javac>
task which compiler to compile java sources with.
This goal also modifies some properties from the test plugin. You'll
need to call clover:off to reset them as they were initially.
========================================================================
-->
<goal name="clover:on-internal" prereqs="clover:init">
<!-- Save properties so that they can be reset afterwards -->
<maven:get var="ignoreTestFailureOld" plugin="maven-test-plugin"
property="maven.test.failure.ignore"/>
<maven:get var="junitForkOld" plugin="maven-test-plugin"
property="maven.junit.fork"/>
<!-- Make sure that the report is generated whether the tests pass or
not -->
<maven:set plugin="maven-test-plugin" property="maven.test.failure.ignore"
value="true"/>
<!-- We tell the test plugin to run tests in a forked JVM. Due to a
limitation in Clover, test coverage data are flushed upon JVM
shutdown. Running test in a forked JVM allows to write properly the
results before the goal finishes and thus it is possible to generate
Clover reports in the same Maven session. -->
<maven:set plugin="maven-test-plugin" property="maven.junit.fork"
value="true"/>
<echo>Setting Clover compiler</echo>
<!-- Transforming Ant path structure into a list of dirs that can be used
to define an Ant fileset. We're using <pathconvert> to automatically
separate dirs with a known delimiter independently of any platform -->
<ant:pathconvert property="compileSrcSetString" pathSep="||||"
refid="maven.compile.src.set"/>
<u:tokenize var="srcDirs" delim="||||">${compileSrcSetString}</u:tokenize>
<ant:clover-setup
initstring="${maven.clover.database}"
flushpolicy="${maven.clover.flushpolicy}"
flushinterval="${maven.clover.flushinterval}"
relative="${maven.clover.relative}">
<j:forEach var="srcDir" items="${srcDirs}">
<ant:fileset dir="${srcDir}"
includes="${maven.clover.includes}"
excludes="${maven.clover.excludes}"/>
</j:forEach>
<!-- Conditionally instrument test code -->
<j:if test="${context.getVariable('maven.clover.instrument.tests') == 'true'}">
<ant:pathconvert property="testSrcSetString" pathSep="||||"
refid="maven.test.compile.src.set"/>
<u:tokenize var="testDirs" delim="||||">${testSrcSetString}</u:tokenize>
<j:forEach var="testDir" items="${testDirs}">
<ant:fileset dir="${testDir}"
includes="${maven.clover.includes}"
excludes="${maven.clover.excludes}"/>
</j:forEach>
</j:if>
<!-- Define method contexts provided by users -->
<u:tokenize var="contextMethodNames" delim=",">${maven.clover.context.method.names}</u:tokenize>
<u:tokenize var="contextMethodRegexps" delim=",">${maven.clover.context.method.regexps}</u:tokenize>
<j:forEach var="contextMethodName" items="${contextMethodNames}">
<ant:methodContext name="${contextMethodName}" regexp="${contextMethodRegexps.remove(0)}"/>
</j:forEach>
<!-- Define statement contexts provided by users -->
<u:tokenize var="statementMethodNames" delim=",">${maven.clover.context.statement.names}</u:tokenize>
<u:tokenize var="statementMethodRegexps" delim=",">${maven.clover.context.statement.regexps}</u:tokenize>
<j:forEach var="statementMethodName" items="${statementMethodNames}">
<ant:statementContext name="${statementMethodName}" regexp="${statementMethodRegexps.remove(0)}"/>
</j:forEach>
</ant:clover-setup>
<echo>Now using primary build.compiler : ${build.compiler}</echo>
<!-- Ensure that Clovered classes are generated in a directory different than the one
used by the Java and Test plugins for runtime classes. This is to prevent shipping
Clovered classes by mistake. -->
<j:set var="mavenBuildDestOld" value="${context.parent.getVariable('maven.build.dest')}"/>
<j:set var="maven.build.dest" value="${maven.clover.build.classes}" scope="parent" />
</goal>
<goal name="clover:on" prereqs="clover:init"
description="Activates Clover">
<!-- Ensure clover:on is only called once -->
<j:if test="${!isCloverOn}">
<attainGoal name="clover:on-internal"/>
<j:set var="isCloverOn" value="true"/>
</j:if>
</goal>
<!--
========================================================================
Restore properties of the test plugin the same way they were before
the call to clover:on.
========================================================================
-->
<goal name="clover:off-internal">
<!-- Disable Clover -->
<ant:clover-setup
initstring="${maven.clover.database}"
enabled="false"/>
<j:set var="maven.build.dest" value="${mavenBuildDestOld}" scope="parent" />
<j:expr value="${context.removeVariable('build.compiler')}"/>
<j:expr value="${pom.getPluginContext('maven-test-plugin').setVariable(
'maven.test.failure.ignore', ignoreTestFailureOld)}"/>
<maven:set plugin="maven-test-plugin" property="maven.junit.fork"
value="${junitForkOld}"/>
<!-- Reset Ant variables compileSrcSetString and testSrcSetString. This is
required because in Ant a variable is set forever. Setting them to
null using the Jelly set tag seems to do the trick -->
<j:set var="compileSrcSetString" value="${null}" scope="parent"/>
<j:if test="${context.getVariable('maven.clover.instrument.tests') == 'true'}">
<j:set var="testSrcSetString" value="${null}" scope="parent"/>
</j:if>
</goal>
<goal name="clover:off" description="Deactivates Clover">
<!-- Ensure clover:off is only called once -->
<j:if test="${isCloverOn}">
<attainGoal name="clover:off-internal"/>
<j:set var="isCloverOn" value="false"/>
</j:if>
</goal>
<!--
========================================================================
Execute unit tests on Clovered code and thus generate Clover database
data.
========================================================================
-->
<goal name="clover:test"
description="Compile project code with Clover and executes the unit tests">
<attainGoal name="clover:on"/>
<!-- Only run clover if there are tests to execute -->
<j:choose>
<j:when test="${unitTestSourcesPresent == 'true'}">
<attainGoal name="test:test"/>
</j:when>
<j:when test="${sourcesPresent == 'true'}">
<attainGoal name="java:compile"/>
<attainGoal name="java:jar-resources"/>
</j:when>
<j:otherwise>
<ant:echo>No sources and not tests to run Clover on</ant:echo>
</j:otherwise>
</j:choose>
<attainGoal name="clover:off"/>
</goal>
<!--
========================================================================
Execute unit tests and Clover on a single class and generate a Swing
clover report. Need to pass the "testcase" property
(maven -Dtestcase=myclass).
========================================================================
-->
<goal name="clover:test-single"
description="Compile code with Clover and execute a single unit test">
<j:choose>
<j:when test="${unitTestSourcesPresent == 'true'}">
<attainGoal name="clover:on"/>
<attainGoal name="test:single"/>
<attainGoal name="clover:swing-report"/>
<attainGoal name="clover:off"/>
</j:when>
<j:otherwise>
<ant:echo>No tests to run Clover on</ant:echo>
</j:otherwise>
</j:choose>
</goal>
<!--
========================================================================
Generate test coverage reports. It decides what kind of reports should
be generated.
========================================================================
-->
<goal name="clover:report" prereqs="clover:init"
description="Generates the Clover reports">
<!-- Skip reports if no coverage database has been created. -->
<u:file var="cloverDatabaseAsFile" name="${maven.clover.database}"/>
<j:choose>
<j:when test="${cloverDatabaseAsFile.exists()}">
<j:if test="${context.getVariable('maven.clover.report.xml') == 'true'}">
<attainGoal name="clover:xml-report"/>
</j:if>
<j:if test="${context.getVariable('maven.clover.report.html') == 'true'}">
<attainGoal name="clover:html-report"/>
</j:if>
<j:if test="${context.getVariable('maven.clover.report.swing') == 'true'}">
<attainGoal name="clover:swing-report"/>
</j:if>
<j:if test="${context.getVariable('maven.clover.report.pdf') == 'true'}">
<attainGoal name="clover:pdf-report"/>
</j:if>
</j:when>
<j:otherwise>
<ant:echo>No Clover database found, skipping report generation</ant:echo>
</j:otherwise>
</j:choose>
</goal>
<!--
========================================================================
Generate XML test coverage report.
========================================================================
-->
<goal name="clover:xml-report"
description="Generate XML test coverage reports with Clover">
<attainGoal name="clover:on"/>
<ant:clover-report span="${maven.clover.span}">
<ant:current outfile="${maven.build.dir}/clover.xml" alwaysReport="true"
title="${pom.name} - ${pom.currentVersion}">
<ant:format type="xml" orderBy="${maven.clover.orderBy}">
<!-- Add context filtering -->
<j:set var="contextFilters" value="${maven.clover.context.method.names} ${maven.clover.context.statement.names} ${maven.clover.context.block.names}"/>
<j:if test="${!empty(contextFilters.trim())}">
<ant:setProperty name="filter" value="${contextFilters}"/>
</j:if>
</ant:format>
</ant:current>
</ant:clover-report>
<attainGoal name="clover:off"/>
</goal>
<!--
========================================================================
Generate HTML test coverage report.
========================================================================
-->
<goal name="clover:html-report"
description="Generate HTML test coverage reports with Clover">
<attainGoal name="clover:on"/>
<j:set var="cloverReportDirectory" value="${maven.docs.dest}/clover"/>
<ant:mkdir dir="${cloverReportDirectory}"/>
<ant:clover-report span="${maven.clover.span}">
<ant:current outfile="${cloverReportDirectory}" alwaysReport="true"
title="${pom.name} - ${pom.currentVersion}">
<ant:format type="html" orderBy="${maven.clover.orderBy}">
<!-- Add context filtering -->
<j:set var="contextFilters" value="${maven.clover.context.method.names} ${maven.clover.context.statement.names} ${maven.clover.context.block.names}"/>
<j:if test="${!empty(contextFilters.trim())}">
<ant:setProperty name="filter" value="${contextFilters}"/>
</j:if>
</ant:format>
</ant:current>
<!-- Only generate history report if there is at least one history point -->
<j:if test="${historyDirAsFile.exists() and !empty(historyDirAsFile.listFiles())}">
<j:set var="cloverHistoryReportDirectory" value="${maven.docs.dest}/clover/history"/>
<ant:mkdir dir="${cloverHistoryReportDirectory}"/>
<ant:historical outfile="${cloverHistoryReportDirectory}"
historyDir="${maven.clover.history.dir}"
title="${pom.name} - ${pom.currentVersion} - History">
<ant:format type="html"/>
</ant:historical>
</j:if>
</ant:clover-report>
<attainGoal name="clover:off"/>
</goal>
<!--
========================================================================
Generate Swing test coverage report.
========================================================================
-->
<goal name="clover:swing-report"
description="Generate Swing test coverage reports with Clover">
<attainGoal name="clover:on"/>
<ant:clover-view/>
<attainGoal name="clover:off"/>
</goal>
<!--
========================================================================
Generate PDF test coverage report.
========================================================================
-->
<goal name="clover:pdf-report"
description="Generate PDF test coverage reports with Clover">
<attainGoal name="clover:on"/>
<j:set var="cloverReportDirectory" value="${maven.docs.dest}/clover"/>
<j:set var="cloverPDFReportFile" value="${cloverReportDirectory}/clover.pdf"/>
<ant:mkdir dir="${cloverReportDirectory}"/>
<ant:clover-report span="${maven.clover.span}">
<ant:current outfile="${cloverPDFReportFile}" alwaysReport="true"
title="${pom.name} - ${pom.currentVersion}" summary="true">
<ant:format type="pdf" orderBy="${maven.clover.orderBy}">
<!-- Add context filtering -->
<j:set var="contextFilters" value="${maven.clover.context.method.names} ${maven.clover.context.statement.names} ${maven.clover.context.block.names}"/>
<j:if test="${!empty(contextFilters.trim())}">
<ant:setProperty name="filter" value="${contextFilters}"/>
</j:if>
</ant:format>
</ant:current>
<!-- Only generate history report if there is at least one history point -->
<j:if test="${historyDirAsFile.exists() and !empty(historyDirAsFile.listFiles())}">
<j:set var="cloverHistoryReportDirectory" value="${maven.docs.dest}/clover/history"/>
<j:set var="cloverPDFHistoryReportFile" value="${cloverHistoryReportDirectory}/clover-history.pdf"/>
<ant:mkdir dir="${cloverHistoryReportDirectory}"/>
<ant:historical outfile="${cloverPDFHistoryReportFile}"
historyDir="${maven.clover.history.dir}"
title="${pom.name} - ${pom.currentVersion} - History">
<ant:format type="pdf"/>
</ant:historical>
</j:if>
</ant:clover-report>
<attainGoal name="clover:off"/>
</goal>
<!--
========================================================================
Register the Clover plugin for site generation.
========================================================================
-->
<goal name="maven-clover-plugin:register">
<j:if test="${unitTestSourcesPresent == 'true'
|| context.getVariable('maven.clover.multiproject') == 'true'}">
<doc:registerReport
name="Clover"
pluginName="maven-clover-plugin"
link="clover/index"
target="_blank"
description="Clover test coverage report."/>
<!-- Only generate history report if there is at least one history point -->
<j:if test="${historyDirAsFile.exists() and !empty(historyDirAsFile.listFiles())}">
<doc:registerReport
name="Clover History"
pluginName="maven-clover-plugin"
link="clover/history/historical"
target="_blank"
description="Clover test coverage history report."/>
</j:if>
</j:if>
</goal>
<!--
========================================================================
Deregister the Clover plugin from site generation.
========================================================================
-->
<goal name="maven-clover-plugin:deregister">
<j:if test="${unitTestSourcesPresent == 'true'
|| context.getVariable('maven.clover.multiproject') == 'true'}">
<doc:deregisterReport name="Clover"/>
<!-- Only deregister the history report if there is at least one history point -->
<j:if test="${historyDirAsFile.exists() and !empty(historyDirAsFile.listFiles())}">
<doc:deregisterReport name="Clover History"/>
</j:if>
</j:if>
</goal>
<!--
========================================================================
Goal that is executed during the site generation
========================================================================
-->
<goal name="maven-clover-plugin:report">
<!-- If maven.clover.execute.during.report is set to false then don't
execute Clover during site report generation -->
<j:choose>
<j:when test="${context.getVariable('maven.clover.execute.during.report')}">
<attainGoal name="clover"/>
</j:when>
<j:otherwise>
<attainGoal name="clover:report"/>
</j:otherwise>
</j:choose>
</goal>
<!--
========================================================================
Fail the build if the test coverage is lower than a threshold
========================================================================
-->
<goal name="clover:check" prereqs="clover:init"
description="Fail the build if the coverage is lower than the specified threshold">
<ant:clover-check haltOnFailure="true" initString="${maven.clover.database}">
<j:if test="${!empty(context.getVariable('maven.clover.check.target'))}">
<ant:setProperty name="target" value="${maven.clover.check.target}"/>
</j:if>
<u:tokenize var="packages" delim=",">${maven.clover.check.packages}</u:tokenize>
<u:tokenize var="targets" delim=",">${maven.clover.check.targets}</u:tokenize>
<j:forEach var="package" items="${packages}">
<ant:package name="${package}" target="${targets.remove(0)}"/>
</j:forEach>
<!-- Add context filtering -->
<j:set var="contextFilters" value="${maven.clover.context.method.names} ${maven.clover.context.statement.names} ${maven.clover.context.block.names}"/>
<j:if test="${!empty(contextFilters.trim())}">
<ant:setProperty name="filter" value="${contextFilters}"/>
</j:if>
</ant:clover-check>
</goal>
<!--
========================================================================
Record a history point
========================================================================
-->
<goal name="clover:save-history" prereqs="clover:init"
description="Record a history point">
<ant:clover-historypoint historyDir="${maven.clover.history.dir}" span="${maven.clover.span}"/>
</goal>
<!--
========================================================================
Merge different Clover databases. This goal is meant to be used in
a top level master project.
========================================================================
-->
<goal name="clover:merge" prereqs="clover:init"
description="Merge Clover databases">
<maven:get var="_multiproject_basedir" plugin="maven-multiproject-plugin" property="maven.multiproject.basedir" />
<ant:clover-merge initString="${maven.clover.database}">
<ant:cloverDbSet dir="${_multiproject_basedir}">
<ant:include name="${maven.clover.merge.databases}"/>
</ant:cloverDbSet>
</ant:clover-merge>
</goal>
<!--
========================================================================
Runs Clover on a multiproject and merge the Clover databases.This
goal does not generate a report; call clover:report for that or add
Clover as a report in your POM.
========================================================================
-->
<goal name="clover:multiproject" description="Run Clover on a multiproject">
<!-- TODO: Shouldn't be required as we're using maven:set but it doesn't
work if it's not there! -->
<multiproject:dependency-handle />
<maven:set plugin="maven-multiproject-plugin" property="goal"
value="clover:test"/>
<attainGoal name="multiproject:goal"/>
<attainGoal name="clover:merge"/>
</goal>
</project>