检查风格 + 抑制滤波器

发布于 2024-07-07 08:18:28 字数 420 浏览 12 评论 0原文

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

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

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

有什么帮助吗?

I have a checkstyle suppression filter setup (e.g. ignore magic numbers in unit test code).

The suppression xml file resides in the same folder as the checkstyle xml file. However, where this file actually is varies:
on my windows dev box it is in d:\dev\shared\checkstyle\config
on the Linux CI server it will be in /root/repo/shared/checkstyle/config
on another developers box it could be anywhere (they check out their svn repo to).

The only "consistent" thing is that the suppression file is always in the same folder as the checkstyle xml file.
I cannot work out how to ensure that this file is always consistently picked up. Also I don't know why checkstyle does not support embedded suppression within the checkstyle xml file.

any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

避讳 2024-07-14 08:18:28

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

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

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

<module name="SuppressionFilter">
    <property name="file" value="${checkstyle-suppressions-file}"/>
</module>

Checkstyle 属性文件不是静态的,它是由 Ant 构建脚本从名为 template 的属性文件模板生成的-checkstyle.properties。 抑制文件属性的模板如下所示:

checkstyle-suppressions-file=@SCM_DIR@/checkstyle_suppressions.xml

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

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

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

<pathconvert targetos="unix" property="scm.dir.unix">
    <path location="${scm.dir}"/>
</pathconvert>

然后我调用 checkstyle Ant 任务如下:

<checkstyle config="${scm.dir}/checkstyle_checks.xml"
            properties="${scm.dir}/checkstyle.properties">
    <!-- details elided -->
</checkstyle>

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

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

希望这会有所帮助

I had this same problem with the Checkstyle suppression configuration when I was going back and forth between Linux and Windows. Here's how I solved it in my Ant-based build system:

Basically, I inject the proper, platform-specific directory value into the main Checkstyle configuration file by configuring a Checkstyle properties file with an Ant build script.

My main Checkstyle configuration file has a SuppressionFilter module declaration as shown below. The value of the checkstyle-suppressions-file property comes from a Checkstyle properties file:

<module name="SuppressionFilter">
    <property name="file" value="${checkstyle-suppressions-file}"/>
</module>

The Checkstyle properties file is not static, it is generated by an Ant build script from a properties file template called template-checkstyle.properties. Here's what the template looks like for the suppressions file property:

checkstyle-suppressions-file=@SCM_DIR@/checkstyle_suppressions.xml

My Ant build script copies this file to a file named checkstyle.properties. The copy has the special token replaced with the proper value of the directory in which the suppressions file is found:

<copy file="${scm.dir}/template-checkstyle.properties" tofile="${scm.dir}/checkstyle.properties">
    <filterset>
        <filter token="SCM_DIR" value="${scm.dir.unix}"/>
    </filterset>
</copy>

Now, where does the value of scm.dir.unix come from? Well, it's derived from a property of my build, read on. You'll need to specify such a value with the directory values that you mentioned.

Note that there is one slightly non-obvious issue concerning the way in which you specify this directory. I say that the scm.dir.unix value is derived from a build property because I observed that the main Checkstyle configuration file cannot contain backslashes, i.e. Windows path separator characters, in the value of the file property of the SuppressionFilter module. For example, specifying something like C:\foo\bar\baz leads to a Checkstyle error message saying that C:foobarbaz cannot be found. I work around this by "converting" the scm.dir directory build property to a "unix" format with Ant's pathconvert task:

<pathconvert targetos="unix" property="scm.dir.unix">
    <path location="${scm.dir}"/>
</pathconvert>

Then I call the checkstyle Ant task like this:

<checkstyle config="${scm.dir}/checkstyle_checks.xml"
            properties="${scm.dir}/checkstyle.properties">
    <!-- details elided -->
</checkstyle>

The call to the checkstyle task injects the key/value pairs contained in the checkstyle.properties file into the main Checkstyle configuration.

If you like, you can see the full scripts here

Hope this helps

万人眼中万个我 2024-07-14 08:18:28

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

<module name="SuppressionFilter">
    <property name="file" value="${samedir}/suppressions.xml"/>
</module>

In eclipse I put the following which did not require me to add any additional properties:

<module name="SuppressionFilter">
    <property name="file" value="${samedir}/suppressions.xml"/>
</module>
十级心震 2024-07-14 08:18:28

我使用 ant.file 变量和项目名称获取 build.xml 所在目录的绝对路径:

<project name="common" ... >
  <dirname property="thisdir" file="${ant.file.common}"/>

然后我可以将绝对路径连接到我的 checkstyle配置文件:

checkstyle.suppressions.file=${thisdir}/qclib/checkstyle-suppressions.xml

由于 thisdir 变量来自 ant,因此它似乎不需要路径分隔符转换。

I get the absolute path to the directory where build.xml resides using the ant.file variable and the name of the project:

<project name="common" ... >
  <dirname property="thisdir" file="${ant.file.common}"/>

Then I can concatenate an absolute path to my checkstyle config files:

checkstyle.suppressions.file=${thisdir}/qclib/checkstyle-suppressions.xml

Since the thisdir variable comes from ant, it does not seem to need path separator conversion.

凉世弥音 2024-07-14 08:18:28

如果您正在使用 Eclipse,并且抑制文件与外部 checkstyle 配置位于同一目录中,则可以像这样设置抑制过滤器:

<module name="SuppressionFilter">
    <property name="file" value="${config_dir}/my_suppressions.xml"/>
</module>

您还必须在 checkstyle 配置中定义 ${config_dir} 属性:

Eclipse Preferences - > “检查风格”-> 选择您的 cs 配置 -> “属性..”-> “附加属性..”

为 checkstyle 配置目录定义一个属性:

config_dir --->  ${config_loc}

If you are working with eclipse and you have the suppression file in the same directory as the external checkstyle config, you can set up a suppression filter like this:

<module name="SuppressionFilter">
    <property name="file" value="${config_dir}/my_suppressions.xml"/>
</module>

You also must define the ${config_dir} property in the checkstyle configuration:

Eclipse Preferences -> "Checkstyle" -> Choose your cs config -> "Properties .." -> "Additional Properties .."

Define a property for the checkstyle config dir:

config_dir --->  ${config_loc}
浪推晚风 2024-07-14 08:18:28

我认为 Robert 的答案可以扩展到 ant 和 Eclipse 的简单解决方案:

将抑制文件包含在您的配置 XML 中,如下所示:

<module name="SuppressionFilter">
    <property name="file" value="${samedir}/suppressions.xml"/>
</module>

现在,Eclipse 满意并找到了该文件。

为了让 ant 工作,请将您的目标更新为如下所示:

<checkstyle config="${checkstyle.config}/checkstyle-checks.xml">
    <!-- ... -->
    <property key="samedir" value="${checkstyle.config}"/>
</checkstyle>

希望这会有所帮助。

I think Robert's answer can be extended to an easy solution for ant and Eclipse:

Include the suppression file inside your config XML like this:

<module name="SuppressionFilter">
    <property name="file" value="${samedir}/suppressions.xml"/>
</module>

Now, Eclipse is satisfied and finds the file.

To get ant to work update your target to something like this:

<checkstyle config="${checkstyle.config}/checkstyle-checks.xml">
    <!-- ... -->
    <property key="samedir" value="${checkstyle.config}"/>
</checkstyle>

Hope this helps.

蓝天白云 2024-07-14 08:18:28

从 CheckStyle 4.26.0 开始,您可以在配置文件中使用预定义常量。

(https://github.com/jshiell/checkstyle-idea/issues/217):

  • ${basedir} & ${project_loc} - 映射到当前项目目录
  • ${workspace_loc} - 映射到当前 Eclipse 工作空间目录
  • ${config_loc} & ${samedir} - 映射到配置文件所在的目录

如果您想与 maven 共享配置,您将需要使用“propertyExpansion”在 POM 配置(在报告部分)中“别名”“eclipse 常量” ”配置元素:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-checkstyle-plugin</artifactId>
   <version>3.0.0</version>
   <configuration>
      <configLocation>${project.basedir}/quality/checkstyle/dap_checkstyle_checks.xml</configLocation>
      <propertyExpansion>basedir=${project.basedir}</propertyExpansion>
   </configuration>
   <reportSets>
      <reportSet>
         <reports>
            <report>checkstyle</report>
         </reports>
      </reportSet>
   </reportSets>
</plugin>

“propertyExpansion”的灵感来自:https:// github.com/checkstyle/checkstyle/blob/master/pom.xml#L582

Since CheckStyle 4.26.0 you can use predefined constants in your config files.

(https://github.com/jshiell/checkstyle-idea/issues/217) :

  • ${basedir} & ${project_loc} - gets mapped to the current project directory
  • ${workspace_loc} - gets mapped to the current Eclipse workspace directory
  • ${config_loc} & ${samedir} - get mapped to the directory the configuration file lies in

If you want to share the config with maven you will need to "alias" the "eclipse constants" in your POM configuration (in the reporting section) using the "propertyExpansion" configuration element :

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-checkstyle-plugin</artifactId>
   <version>3.0.0</version>
   <configuration>
      <configLocation>${project.basedir}/quality/checkstyle/dap_checkstyle_checks.xml</configLocation>
      <propertyExpansion>basedir=${project.basedir}</propertyExpansion>
   </configuration>
   <reportSets>
      <reportSet>
         <reports>
            <report>checkstyle</report>
         </reports>
      </reportSet>
   </reportSets>
</plugin>

The "propertyExpansion" is inspired from : https://github.com/checkstyle/checkstyle/blob/master/pom.xml#L582.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文