2

我在 Eclipse 中有一个 Maven WebApp 项目(只是一个 HelloWorld Servlet)并正在运行 tomcat 服务器。如果代码包含 out.print 语句,我的最终目标是使构建失败。我在 pom.xml 中定义了 maven pmd 插件 2.5,如下所示,并在名为 sop.xml 的文件中定义了一个自定义规则集,如下所示。

但是当我右键单击项目 -> 运行方式 -> Maven 构建并尝试打包它时,构建成功并创建了war文件。我希望构建失败,因为 .java 代码包含 out.print 语句。

我是 Maven 的新手,并在这方面请求您的所有帮助。谢谢。

规则集文件(sop.xml):

<?xml version="1.0"?>
<ruleset name="Custom ruleset" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0
http://pmd.sourceforge.net/ruleset_2_0_0.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>This ruleset checks my code for println statements</description>
<rule ref="rulesets/JavaLogging.xml" message="Must handle exceptions">
</rule>
</ruleset>

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"                          
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MavenWebGroupId</groupId>
<artifactId>MavenWebArtifactId</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MavenWebName</name>
<description>MavenWebDescription</description>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <skip>false</skip>
                <targetJdk>${compile.source}</targetJdk>
                <rulesets>
                    <!-- Custom local file system rule set -->
                    <ruleset>c:\rulesets\sop.xml</ruleset>
                </rulesets>
                <linkXref>true</linkXref>
                <failOnViolation>true</failOnViolation>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>build</artifactId>
                    <version>${project.version}</version>
                </dependency>
            </dependencies>
        </plugin>

        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.0</version>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.6</version>
    </dependency>
</dependencies>
    </project>
4

1 回答 1

1

看起来maven-pmd-plugin在打包过程中没有执行。要在打包期间调用此插件,请将其定义如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>2.5</version>
    <configuration>
     ...
    </configuration>
    <dependencies>
    ...
    </dependencies>
    <executions>
        <execution>
            <id>run-pmd</id>
            <phase>prepare-package</phase>
            <goals>
                 <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

注意<executions>标记。

于 2012-09-19T13:50:36.063 回答