Maven PMD插件用于编译XSLT文件

时间:2015-05-07 09:20:56

标签: maven maven-plugin pmd

是否可以使用Maven PMD插件来验证XSLT文件并生成报告?如果是这样,请提供插件配置的示例用法。

1 个答案:

答案 0 :(得分:0)

这不是一个优雅的解决方案,但我使用了以下设置:您需要添加xsl文件所在的文件夹作为源文件夹,并确保pmd插件扫描* .xsl文件并具有pmd-xml依赖关系。

我假设,xsl文件位于src/main/xsl,并使用xsl的文件扩展名。

我将采用PMD的5.2.3版本,因为这是maven-pmd-plugin的基础。我启用了唯一的内置规则集rulesets/xsl/xpath.xml。规则在pmd documentation

中描述

这里是完整的pom.xml文件:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.sourceforge.pmd.it</groupId>
    <artifactId>maven-example-xsl</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <build>
       <plugins>
           <plugin>
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>build-helper-maven-plugin</artifactId>
               <version>1.9.1</version>
               <configuration>
                   <sources>
                       <source>src/main/xsl</source>
                   </sources>
               </configuration>
               <executions>
                   <execution>
                       <goals><goal>add-source</goal></goals>
                   </execution>
               </executions>
           </plugin>
           <plugin>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.4</version>
                <configuration>
                    <rulesets>
                        <ruleset>rulesets/xsl/xpath.xml</ruleset>
                    </rulesets>
                    <printFailingErrors>true</printFailingErrors>
                    <includes>
                        <include>**/*.xsl</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals><goal>check</goal></goals>
                    </execution>
                </executions>
                <dependencies>
                   <dependency>
                       <groupId>net.sourceforge.pmd</groupId>
                       <artifactId>pmd-xml</artifactId>
                       <version>5.2.3</version>
                   </dependency>
               </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

这是一个示例xsl文件,它触发&#34; UseConcatOnce&#34;规则。将其存储在src/main/xsl/sample.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:value-of select="concat('double', concat('concat'))"/>
</xsl:template>
</xsl:stylesheet>

如果您现在运行mvn clean verify,您应该会看到失败的构建:

...
[INFO] --- maven-pmd-plugin:3.4:check (default) @ maven-example-xsl ---
[INFO] PMD Failure: sample.xsl:4 Rule:UseConcatOnce Priority:3 The xpath concat() function accepts as many arguments as required, you may be able to factorize this expression.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------