checkstyle +抑制滤镜

Gar*_*ams 5 java code-analysis static-analysis checkstyle

我有一个checkstyle抑制滤波器设置(例如,忽略单元测试代码中的幻数).

抑制xml文件与checkstyle xml文件位于同一文件夹中.但是,这个文件实际上有所不同:在我的Windows开发盒上它位于Linux CI服务器上的d:\ dev\shared\checkstyle\config中,它将位于/ root/repo/shared/checkstyle/config上另一个开发人员盒子上它可能在任何地方(他们检查他们的svn回购).

唯一"一致"的事情是抑制文件始终与checkstyle xml文件位于同一文件夹中.我无法弄清楚如何确保始终一致地拾取此文件.另外我不知道为什么checkstyle不支持checkstyle xml文件中的嵌入式抑制.

任何帮助?

Gre*_*tes 10

当我在Linux和Windows之间来回时,我遇到了与Checkstyle抑制配置相同的问题.以下是我在基于Ant的构建系统中解决它的方法:

基本上,我通过使用Ant构建脚本配置Checkstyle属性文件,将正确的,特定于平台的目录值注入主Checkstyle配置文件中.

我的主要Checkstyle配置文件有一个SuppressionFilter模块声明,如下所示.checkstyle-suppressions-file属性的值来自Checkstyle属性文件:

<module name="SuppressionFilter">
    <property name="file" value="${checkstyle-suppressions-file}"/>
</module>
Run Code Online (Sandbox Code Playgroud)

Checkstyle属性文件不是静态的,它是由来自名为的属性文件模板的Ant构建脚本生成的template-checkstyle.properties.以下是抑制文件属性的模板外观:

checkstyle-suppressions-file=@SCM_DIR@/checkstyle_suppressions.xml
Run Code Online (Sandbox Code Playgroud)

我的Ant构建脚本将此文件复制到名为的文件checkstyle.properties.该副本将特殊标记替换为找到抑制文件的目录的正确值:

<copy file="${scm.dir}/template-checkstyle.properties" tofile="${scm.dir}/checkstyle.properties">
    <filterset>
        <filter token="SCM_DIR" value="${scm.dir.unix}"/>
    </filterset>
</copy>
Run Code Online (Sandbox Code Playgroud)

现在,价值scm.dir.unix从何而来?好吧,它源自我构建的属性,请继续阅读.您需要使用您提到的目录值指定这样的值.

请注意,关于指定此目录的方式存在一个稍微不明显的问题.我说该scm.dir.unix值是从构建属性派生的,因为我发现主要的Checkstyle配置文件不能fileSuppressionFilter模块属性的值中包含反斜杠,即Windows路径分隔符.例如,指定类似的内容C:\foo\bar\baz会导致C:foobarbaz无法找到Checkstyle错误消息.我通过scm.dir使用Ant的pathconvert任务将目录构建属性"转换" 为"unix"格式来解决这个问题:

<pathconvert targetos="unix" property="scm.dir.unix">
    <path location="${scm.dir}"/>
</pathconvert>
Run Code Online (Sandbox Code Playgroud)

然后我checkstyle像这样调用Ant任务:

<checkstyle config="${scm.dir}/checkstyle_checks.xml"
            properties="${scm.dir}/checkstyle.properties">
    <!-- details elided -->
</checkstyle>
Run Code Online (Sandbox Code Playgroud)

checkstyle任务的调用将checkstyle.properties文件中包含的键/值对注入主Checkstyle配置.

如果您愿意,可以在此处查看完整脚本

希望这可以帮助


Rob*_*tto 6

在eclipse中我放了以下不需要我添加任何其他属性的内容:

<module name="SuppressionFilter">
    <property name="file" value="${samedir}/suppressions.xml"/>
</module>
Run Code Online (Sandbox Code Playgroud)