【问题标题】:Maven Webapp Project not recognizing the custom ruleset during buildMaven Webapp 项目在构建期间无法识别自定义规则集
【发布时间】:2012-09-19 21:50:36
【问题描述】:

我在 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>

【问题讨论】:

    标签: eclipse maven pmd


    【解决方案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>
    

    注意&lt;executions&gt; 标签。

    【讨论】:

    • 谢谢。现在我收到另一个错误'找不到资源'c:\rulesets\sop.xml'。尽管我检查了 sop.xml 是否存在于同一位置。我应该把它放在不同的地方吗?
    • 在将 sop.xml 放入 tomcat 本身的 maven 项目的 src/main/resources 文件夹中后,我能够纠正此错误。
    • 你也可以把sop.xml放到src/main/config目录下,引用为${basedir}/src/main/config/sop.xml。如果我的回答对你有帮助,请采纳。
    猜你喜欢
    • 2016-02-03
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 2013-02-14
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多