【问题标题】:Maven PMD Plugin to compile XSLT files用于编译 XSLT 文件的 Maven PMD 插件
【发布时间】:2015-05-09 04:20:06
【问题描述】:

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

【问题讨论】:

标签: maven maven-plugin pmd


【解决方案1】:

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

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

我将采用 5.2.3 版的 PMD,因为这是构建 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>

这是一个触发“UseConcatOnce”规则的示例 xsl 文件。将此存储在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] ------------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多