检查风格 + 抑制滤波器
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当我在 Linux 和 Windows 之间来回切换时,Checkstyle 抑制配置也遇到了同样的问题。 以下是我在基于 Ant 的构建系统中解决该问题的方法:
基本上,我通过使用 Ant 构建脚本配置 Checkstyle 属性文件,将正确的、特定于平台的目录值注入到主 Checkstyle 配置文件中。
我的主 Checkstyle 配置文件有一个
SuppressionFilter
模块声明,如下所示。checkstyle-suppressions-file
属性的值来自 Checkstyle 属性文件:Checkstyle 属性文件不是静态的,它是由 Ant 构建脚本从名为
template 的属性文件模板生成的-checkstyle.properties
。 抑制文件属性的模板如下所示:我的 Ant 构建脚本将此文件复制到名为
checkstyle.properties
的文件中。 该副本将特殊标记替换为找到抑制文件的目录的正确值:现在,
scm.dir.unix
的值从何而来? 好吧,它派生自我的构建的属性,请继续阅读。 您需要使用您提到的目录值来指定这样的值。请注意,关于指定此目录的方式,有一个不太明显的问题。 我说
scm.dir.unix
值是从构建属性派生的,因为我观察到主 Checkstyle 配置文件在的值中不能包含反斜杠,即 Windows 路径分隔符字符。
属性。 例如,指定类似SuppressionFilter
模块的 fileC:\foo\bar\baz
的内容会导致出现 Checkstyle 错误消息,指出无法找到C:foobarbaz
。 我通过使用 Ant 的pathconvert
任务将scm.dir
目录构建属性“转换”为“unix”格式来解决此问题:然后我调用
checkstyle Ant 任务如下:
对
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 thecheckstyle-suppressions-file
property comes from a Checkstyle properties file: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: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: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 thefile
property of theSuppressionFilter
module. For example, specifying something likeC:\foo\bar\baz
leads to a Checkstyle error message saying thatC:foobarbaz
cannot be found. I work around this by "converting" thescm.dir
directory build property to a "unix" format with Ant'spathconvert
task:Then I call the
checkstyle
Ant task like this:The call to the
checkstyle
task injects the key/value pairs contained in thecheckstyle.properties
file into the main Checkstyle configuration.If you like, you can see the full scripts here
Hope this helps
在 Eclipse 中,我添加了以下内容,不需要我添加任何其他属性:
In eclipse I put the following which did not require me to add any additional properties:
我使用
ant.file
变量和项目名称获取build.xml
所在目录的绝对路径:然后我可以将绝对路径连接到我的 checkstyle配置文件:
由于
thisdir
变量来自 ant,因此它似乎不需要路径分隔符转换。I get the absolute path to the directory where
build.xml
resides using theant.file
variable and the name of the project:Then I can concatenate an absolute path to my checkstyle config files:
Since the
thisdir
variable comes from ant, it does not seem to need path separator conversion.如果您正在使用 Eclipse,并且抑制文件与外部 checkstyle 配置位于同一目录中,则可以像这样设置抑制过滤器:
您还必须在 checkstyle 配置中定义 ${config_dir} 属性:
Eclipse Preferences - > “检查风格”-> 选择您的 cs 配置 -> “属性..”-> “附加属性..”
为 checkstyle 配置目录定义一个属性:
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:
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:
我认为 Robert 的答案可以扩展到 ant 和 Eclipse 的简单解决方案:
将抑制文件包含在您的配置 XML 中,如下所示:
现在,Eclipse 满意并找到了该文件。
为了让 ant 工作,请将您的目标更新为如下所示:
希望这会有所帮助。
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:
Now, Eclipse is satisfied and finds the file.
To get ant to work update your target to something like this:
Hope this helps.
从 CheckStyle 4.26.0 开始,您可以在配置文件中使用预定义常量。
(https://github.com/jshiell/checkstyle-idea/issues/217):
如果您想与 maven 共享配置,您将需要使用“propertyExpansion”在 POM 配置(在报告部分)中“别名”“eclipse 常量” ”配置元素:
“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) :
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 :
The "propertyExpansion" is inspired from : https://github.com/checkstyle/checkstyle/blob/master/pom.xml#L582.