【问题标题】:How to exclude packages from CheckStyle analysis in a mavenized multi-module project?如何在 mavenized 多模块项目中从 CheckStyle 分析中排除包?
【发布时间】:2014-11-19 22:41:45
【问题描述】:

我有一个包含多个模块的 Maven 项目,包括 org.eclipse.cdt.core。出于某种原因,客户也想构建org.eclipse.cdt.core。

我想生成一份汇总的 Checkstyle 报告,其中排除了一些文件,包括所有 org.eclipse.cdt.core.*(包括子包)类。

为此,我将excludes标签指定为

<excludes>**/org/eclipse/cdt/core/**/*,org.eclipse.cdt.core/src/**/*,../org.eclipse.cdt.core/src/**/*,org.eclipse.cdt.core/**/*</excludes>

然后我运行mvn clean checkstyle-aggregate site,打开文件target/checkstyle-result.xml,发现里面有以下几行:

<file name="C:\dev\ide\org.eclipse.cdt.core\src\org\eclipse\cdt\core\AbstractExecutableExtensionBase.java">
<error line="0" severity="error" message="File does not end with a newline." source="com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck"/>
<error line="10" severity="error" message="Line is longer than 80 characters (found 81)." source="com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck"/>
<error line="15" severity="error" message="Line is longer than 80 characters (found 83)." source="com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck"/>

这意味着我的&lt;excludes&gt; 标签不起作用。

然后,我把行

<properties>
    <checkstyle.skip>true</checkstyle.skip>
</properties>

进入要从 Checkstyle 中排除的所有模块的 pom.xml 文件,但没有帮助。

如何在没有某些模块的结果的情况下生成 CheckStyle 报告(XML 或 HTML)(除了使用 180 MB 大的 target/checkstyle-result.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>mycompany-parentproject</groupId>
    <artifactId>com.mycompany.parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <tycho-version>0.21.0</tycho-version>
        <tycho-extras-version>0.21.0</tycho-extras-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <antrun-version>1.7</antrun-version>
    </properties>

    <pluginRepositories>
        <pluginRepository>
            <id>Codehaus repository</id>
            <url>http://repository.codehaus.org/</url>
        </pluginRepository>
    </pluginRepositories>

    <distributionManagement>
        <site>
            <id>${project.artifactId}-site</id>
            <url>${project.baseUri}</url>
        </site>
    </distributionManagement>
    <build>
        <plugins>
            <plugin>
                <!-- enable tycho build extension -->
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-maven-plugin</artifactId>
                <version>${tycho-version}</version>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>target-platform-configuration</artifactId>
                <version>${tycho-version}</version>
                <configuration>
                    <environments>
                        <environment>
                            <os>win32</os>
                            <ws>win32</ws>
                            <arch>x86_64</arch>
                        </environment>
                        <environment>
                            <os>win32</os>
                            <ws>win32</ws>
                            <arch>x86</arch>
                        </environment>
                        <environment>
                            <os>linux</os>
                            <ws>gtk</ws>
                            <arch>x86_64</arch>
                        </environment>
                        <environment>
                            <os>linux</os>
                            <ws>gtk</ws>
                            <arch>x86</arch>
                        </environment>
                    </environments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-versions-plugin</artifactId>
                <version>${tycho-version}</version>
            </plugin>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-packaging-plugin</artifactId>
                <version>${tycho-version}</version>
                <configuration>
                    <format>'mycompany_'yyyyMMddHHmm</format>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <modules>
        <module>../com.mycompany.module1</module>
        <module>../com.mycompany.module2</module>
        <module>../com.mycompany.module3</module>
        <module>../org.eclipse.cdt.core</module>
    </modules>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <reportPlugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-project-info-reports-plugin</artifactId>
                            <version>2.7</version>
                        </plugin>

                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-checkstyle-plugin</artifactId>
                            <version>2.8</version>
                            <configuration>
                                <configLocation>sun-coding-standard.checkstyle.xml</configLocation>
                                <excludes>**/org/eclipse/cdt/core/**/*,org.eclipse.cdt.core/src/**/*,../org.eclipse.cdt.core/src/**/*,org.eclipse.cdt.core/**/*</excludes>
                                <includeTestSourceDirectory>true</includeTestSourceDirectory>
                            </configuration>
                            <reportSets>
                                <reportSet>
                                    <id>aggregate</id>
                                    <reports>
                                        <report>aggregate</report>
                                    </reports>
                                </reportSet>
                            </reportSets>
                        </plugin>

                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>findbugs-maven-plugin</artifactId>
                            <version>3.0.0</version>
                            <configuration>
                                <xmlOutput>true</xmlOutput>
                            </configuration>
                            <reportSets>
                                <reportSet>
                                    <id>aggregate</id>
                                    <reports>
                                        <report>aggregate</report>
                                    </reports>
                                </reportSet>
                            </reportSets>
                        </plugin>
                    </reportPlugins>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <xmlOutput>true</xmlOutput>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <configuration>
                    <aggregate>true</aggregate>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
</project>

更新 1(25.09.2014 13:03 MSK):尝试通过添加来使用抑制过滤器

<configuration>
    <configLocation>sun-coding-standard.checkstyle.xml</configLocation>
    <suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
</configuration>

其中 checkstyle-suppressions.xml 等于

<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
    <suppress files="[\\/]org.eclipse.cdt.core[\\/]" checks="[a-zA-Z0-9]*"/>
    <suppress files="[\\/]org[\\/]eclipse[\\/]cdt[\\/]core[\\/]" checks="[a-zA-Z0-9]*"/>
    <suppress files="[\\/]org.eclipse.cdt.core[\\/]" checks="."/>
    <suppress files="[\\/]org[\\/]eclipse[\\/]cdt[\\/]core[\\/]" checks="."/>
</suppressions>

没用。

将&lt;suppressionsFileExpression&gt;checkstyle.suppressions.files&lt;/suppressionsFileExpression&gt; 添加到上述&lt;configuration&gt; 部分也无济于事。

【问题讨论】:

    标签: java maven maven-3 checkstyle


    【解决方案1】:

    这可能是一个愚蠢的问题,但是您的 checkstyle 定义中是否还有更多代码?

    通常我们有这样的东西:

    <fileset dir="src">
    <include name="**/*.java"/>
    <exclude name="com/myproject/util/*.java"/>
    </fileset>
    

    您需要说明从哪里开始扫描(源文件夹)、要包括什么类型的文件(通常是 *.java)以及要排除什么。 在我看来,你试图验证太多,而不是必要的东西。 Checkstyle 用于验证开发人员代码,而不是 IDE 配置文件。

    【讨论】:

    • 如何命名这个文件?
    猜你喜欢
    • 2016-01-02
    • 2021-02-27
    • 1970-01-01
    • 2015-12-02
    • 2011-07-05
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多