这不是一个优雅的解决方案,但我可以使用以下设置:您需要将 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] ------------------------------------------------------------------------