maven-plugins/pmd/plugin.jelly

248 lines
9.1 KiB
XML

<?xml version="1.0"?>
<!--
/*
* Copyright 2001-2004 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.
*/
-->
<!--
=============================================================================
PMD Plugin. Generate PMD reports using the PMD framework.
=============================================================================
-->
<project xmlns:j="jelly:core" xmlns:doc="doc" xmlns:ant="jelly:ant" xmlns:util="jelly:util" xmlns:maven="jelly:maven">
<!--
========================================================================
Register a PMD report.
========================================================================
-->
<goal name="maven-pmd-plugin:register">
<j:if test="${sourcesPresent}">
<doc:registerReport
name="PMD Report"
pluginName="pmd"
description="Verification of coding rules."
link="pmd-report"/>
<j:if test="${context.getVariable('maven.pmd.cpd.enable') == 'true'}">
<doc:registerReport
name="CPD Report"
pluginName=""
description="Detection of copy-paste code."
link="cpd-report"/>
</j:if>
</j:if>
</goal>
<!--
========================================================================
Deregister a PMD report.
========================================================================
-->
<goal name="maven-pmd-plugin:deregister">
<j:if test="${sourcesPresent}">
<doc:deregisterReport name="PMD Report"/>
<j:if test="${context.getVariable('maven.pmd.cpd.enable') == 'true'}">
<doc:deregisterReport name="CPD Report"/>
</j:if>
</j:if>
</goal>
<!--
========================================================================
Main PMD plugin goal.
========================================================================
-->
<goal name="pmd" description="Static Code Analyzer">
<!-- Only run PMD if it is enabled -->
<j:set var="enable" value="${maven.pmd.enable}"/>
<j:if test="${enable == 'true'}">
<attainGoal name="pmd:report"/>
</j:if>
</goal>
<!--
========================================================================
Generate the PMD report.
========================================================================
-->
<goal name="pmd:report"
description="Generate source code report with PMD">
<!-- Create the dirs if we start from a previously cleaned project -->
<mkdir dir="${maven.build.dir}"/>
<mkdir dir="${maven.docs.dest}"/>
<!-- Setup the classpath for pmd plugins -->
<path id="pmd-classpath">
<pathelement path="${plugin.getDependencyPath('pmd:pmd')}" />
<pathelement path="${plugin.getDependencyPath('jaxen:jaxen')}" />
<pathelement path="${plugin.getDependencyPath('saxpath:saxpath')}" />
<pathelement path="${pom.getDependencyClasspath()}" />
<path refid="maven.dependency.classpath" />
</path>
<!-- Define a PMD task with the rulesets and all jars in the
classpath -->
<taskdef name="pmd"
classpathref="pmd-classpath"
classname="net.sourceforge.pmd.ant.PMDTask" />
<!-- Run the PMD task - need a way to define the rulesets dynamically -->
<echo>Running the PMD task with ${maven.pmd.rulesetfiles} ...</echo>
<!-- Prepare empty raw report because no file is generated if no rule is
violated and that causes the report generation to fail later on -->
<echo file="${maven.build.dir}/pmd-raw-report.xml"
message="&lt;?xml version='1.0'?&gt;&lt;pmd/&gt;"/>
<j:choose>
<j:when test="${sourcesPresent == 'true'}">
<maven:get var="defaultTargetJdk" plugin="maven-java-plugin" property="maven.compile.source"/>
<!-- maven.compile.source is not set in java plugin contrary to documentation -->
<j:if test="${defaultTargetJdk == null}">
<j:set var="defaultTargetJdk" value="1.3" />
</j:if>
<maven:property var="targetjdk" name="maven.pmd.targetjdk" defaultValue="${defaultTargetJdk}"/>
<pmd
rulesetfiles="${maven.pmd.rulesetfiles}"
failonerror="${maven.pmd.failonerror}"
failOnRuleViolation="${maven.pmd.failonruleviolation}"
targetjdk="${targetjdk}">
<formatter type="xml" toFile="${maven.build.dir}/pmd-raw-report.xml"/>
<formatter type="text" toConsole="${maven.pmd.console}" toFile="${maven.build.dir}/pmd-report.txt"/>
<fileset dir="${pom.build.sourceDirectory}"
includes="${maven.pmd.includes}"
excludes="${maven.pmd.excludes}">
<!-- FIXME: This is a bad cut and paste -->
<!-- handle source modifications -->
<j:forEach var="sm" items="${pom.build.sourceModifications}">
<available property="classPresent" classname="${sm.className}"/>
<j:if test="${classPresent != 'true'}">
<j:forEach var="exclude" items="${sm.excludes}">
<exclude name="${exclude}"/>
</j:forEach>
<j:forEach var="include" items="${sm.includes}">
<include name="${include}"/>
</j:forEach>
</j:if>
</j:forEach>
</fileset>
<!-- run on tests -->
<j:if test="${(unitTestSourcesPresent == 'true') and (context.getVariable('maven.pmd.check.tests'))}">
<pathconvert property="testSrcSetString" pathSep="||||"
refid="maven.test.compile.src.set"/>
<util:tokenize var="testDirs" delim="||||">${testSrcSetString}</util:tokenize>
<j:forEach var="testDir" items="${testDirs}">
<fileset dir="${testDir}"
includes="${maven.pmd.includes}"
excludes="${maven.pmd.excludes}"/>
</j:forEach>
</j:if>
</pmd>
</j:when>
<j:otherwise>
<ant:echo>No java source files to process.</ant:echo>
</j:otherwise>
</j:choose>
<!-- Run JSL to transform the report into XDOC -->
<j:set var="stylesheet" value="${maven.pmd.stylesheet}"/>
<j:if test="${stylesheet == null or stylesheet.equals('')}">
<j:set var="stylesheet" value="${plugin.resources}/pmd.jsl"/>
</j:if>
<echo>Converting the PMD report to xdoc ...</echo>
<echo>Using stylesheet: ${stylesheet}</echo>
<doc:jsl
input="${maven.build.dir}/pmd-raw-report.xml"
output="pmd-report.xml"
stylesheet="${stylesheet}"
outputMode="xml"
prettyPrint="true"
/>
<!-- Generate CPD report if it is enabled -->
<j:set var="enable" value="${maven.pmd.cpd.enable}"/>
<j:if test="${enable == 'true' and sourcesPresent == 'true'}">
<attainGoal name="pmd:cpd-report"/>
</j:if>
</goal>
<!--
========================================================================
Generate the CPD report.
========================================================================
-->
<goal name="pmd:cpd-report"
description="Generate duplicate source code report with CPD">
<!-- Define a CPD task from the PMD jar -->
<taskdef name="cpd"
classpath="${plugin.getDependencyPath('pmd:pmd')}"
classname="net.sourceforge.pmd.cpd.CPDTask"/>
<!-- Run the CPD task -->
<echo>Running the CPD task with minimumTokenCount = ${maven.pmd.cpd.minimumtokencount} ...</echo>
<ant:mkdir dir="${maven.build.dir}" />
<cpd minimumTokenCount="${maven.pmd.cpd.minimumtokencount}"
outputFile="${maven.build.dir}/cpd-raw-report.txt">
<!-- cut and paste from pmd:report -->
<fileset dir="${pom.build.sourceDirectory}"
includes="${maven.pmd.includes}"
excludes="${maven.pmd.excludes}">
<!-- FIXME: This is a bad cut and paste -->
<!-- handle source modifications -->
<j:forEach var="sm" items="${pom.build.sourceModifications}">
<available property="classPresent" classname="${sm.className}"/>
<j:if test="${classPresent != 'true'}">
<j:forEach var="exclude" items="${sm.excludes}">
<exclude name="${exclude}"/>
</j:forEach>
<j:forEach var="include" items="${sm.includes}">
<include name="${include}"/>
</j:forEach>
</j:if>
</j:forEach>
</fileset>
</cpd>
<!-- Convert raw text report into XDOC -->
<j:set var="genDocs" value="${maven.gen.docs}" />
<ant:mkdir dir="${genDocs}" />
<doc:text-xdoc
title="CPD Report"
section="CPD Report"
inputFile="${maven.build.dir}/cpd-raw-report.txt"
output="${genDocs}/cpd-report.xml"
/>
</goal>
</project>