java checkstyle + 抑制过滤器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/198244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
checkstyle + suppression filters
提问by Gary McWilliams
I have a checkstyle suppression filter setup (e.g. ignore magic numbers in unit test code).
我有一个 checkstyle 抑制过滤器设置(例如忽略单元测试代码中的幻数)。
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).
抑制 xml 文件与 checkstyle xml 文件位于同一文件夹中。但是,该文件的实际位置各不相同:在我的 Windows 开发框中,它在 Linux CI 服务器上的 d:\dev\shared\checkstyle\config 中,它将在另一个开发人员框上的 /root/repo/shared/checkstyle/config 中它可以在任何地方(他们查看他们的 svn 存储库)。
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.
唯一“一致”的是抑制文件始终与 checkstyle xml 文件位于同一文件夹中。我无法弄清楚如何确保始终一致地提取此文件。另外我不知道为什么 checkstyle 不支持 checkstyle xml 文件中的嵌入式抑制。
any help?
有什么帮助吗?
回答by Greg Mattes
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:
当我在 Linux 和 Windows 之间来回切换时,Checkstyle 抑制配置也遇到了同样的问题。这是我在基于 Ant 的构建系统中解决它的方法:
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.
基本上,我通过使用 Ant 构建脚本配置 Checkstyle 属性文件,将正确的、特定于平台的目录值注入到主 Checkstyle 配置文件中。
My main Checkstyle configuration file has a SuppressionFiltermodule declaration as shown below. The value of the checkstyle-suppressions-fileproperty comes from a Checkstyle properties file:
我的主要 Checkstyle 配置文件有一个SuppressionFilter模块声明,如下所示。该checkstyle-suppressions-file属性的值来自 Checkstyle 属性文件:
<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 属性文件不是静态的,它是由 Ant 构建脚本从名为template-checkstyle.properties. 以下是抑制文件属性的模板:
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:
我的 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>
Now, where does the value of scm.dir.unixcome from? Well, it's derivedfrom a property of my build, read on. You'll need to specify such a value with the directory values that you mentioned.
现在,价值scm.dir.unix从何而来?好吧,它来自我构建的一个属性,请继续阅读。您需要使用您提到的目录值指定这样的值。
Note that there is one slightly non-obvious issue concerning the way in which you specify this directory. I say that the scm.dir.unixvalue 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 fileproperty of the SuppressionFiltermodule. For example, specifying something like C:\foo\bar\bazleads to a Checkstyle error message saying that C:foobarbazcannot be found. I work around this by "converting" the scm.dirdirectory build property to a "unix" format with Ant's pathconverttask:
请注意,关于您指定此目录的方式,有一个稍微不明显的问题。我说该scm.dir.unix值是从构建属性派生的,因为我观察到主 Checkstyle 配置文件不能file在SuppressionFilter模块的属性值中包含反斜杠,即 Windows 路径分隔符。例如,指定类似的内容C:\foo\bar\baz会导致 Checkstyle 错误消息说C:foobarbaz无法找到。我通过scm.dir使用 Ant 的pathconvert任务将目录构建属性“转换”为“unix”格式来解决这个问题:
<pathconvert targetos="unix" property="scm.dir.unix">
<path location="${scm.dir}"/>
</pathconvert>
Then I call the checkstyleAnt task like this:
然后我checkstyle像这样调用Ant 任务:
<checkstyle config="${scm.dir}/checkstyle_checks.xml"
properties="${scm.dir}/checkstyle.properties">
<!-- details elided -->
</checkstyle>
The call to the checkstyletask injects the key/value pairs contained in the checkstyle.propertiesfile into the main Checkstyle configuration.
对checkstyle任务的调用将checkstyle.properties文件中包含的键/值对注入到主 Checkstyle 配置中。
If you like, you can see the full scripts here
如果您愿意,可以在此处查看完整脚本
Hope this helps
希望这可以帮助
回答by Robert Hutto
In eclipse I put the following which did not require me to add any additional properties:
在 Eclipse 中,我放置了以下不需要我添加任何其他属性的内容:
<module name="SuppressionFilter">
<property name="file" value="${samedir}/suppressions.xml"/>
</module>
回答by gibbss
I get the absolute path to the directory where build.xmlresides using the ant.filevariable and the name of the project:
我build.xml使用ant.file变量和项目名称获取所在目录的绝对路径:
<project name="common" ... >
<dirname property="thisdir" file="${ant.file.common}"/>
Then I can concatenate an absolute path to my checkstyle config files:
然后我可以将绝对路径连接到我的 checkstyle 配置文件:
checkstyle.suppressions.file=${thisdir}/qclib/checkstyle-suppressions.xml
Since the thisdirvariable comes from ant, it does not seem to need path separator conversion.
由于该thisdir变量来自ant,它似乎不需要路径分隔符转换。
回答by J?rg
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:
如果您正在使用 eclipse 并且您在与外部 checkstyle 配置相同的目录中有抑制文件,您可以设置一个抑制过滤器,如下所示:
<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:
您还必须在 checkstyle 配置中定义 ${config_dir} 属性:
Eclipse Preferences -> "Checkstyle" -> Choose your cs config -> "Properties .." -> "Additional Properties .."
Eclipse 首选项 -> "Checkstyle" -> 选择你的 cs 配置 -> "Properties .." -> "Additional Properties .."
Define a property for the checkstyle config dir:
为 checkstyle 配置目录定义一个属性:
config_dir ---> ${config_loc}
回答by Sebastian vom Meer
I think Robert's answer can be extended to an easy solution for ant and Eclipse:
我认为罗伯特的答案可以扩展为ant 和 Eclipse的简单解决方案:
Include the suppression file inside your config XML like this:
在您的配置 XML 中包含抑制文件,如下所示:
<module name="SuppressionFilter">
<property name="file" value="${samedir}/suppressions.xml"/>
</module>
Now, Eclipse is satisfied and finds the file.
现在,Eclipse 已满足并找到该文件。
To get ant to work update your target to something like this:
要让 ant 工作,请将您的目标更新为如下所示:
<checkstyle config="${checkstyle.config}/checkstyle-checks.xml">
<!-- ... -->
<property key="samedir" value="${checkstyle.config}"/>
</checkstyle>
Hope this helps.
希望这可以帮助。
回答by Jérémie DERUETTE
Since CheckStyle 4.26.0 you can use predefined constants in your config files.
从 CheckStyle 4.26.0 开始,您可以在配置文件中使用预定义的常量。
(https://github.com/jshiell/checkstyle-idea/issues/217) :
(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
- ${basedir} & ${project_loc} - 映射到当前项目目录
- ${workspace_loc} - 映射到当前的 Eclipse 工作区目录
- ${config_loc} & ${samedir} - 映射到配置文件所在的目录
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 :
如果要与 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>
The "propertyExpansion" is inspired from : https://github.com/checkstyle/checkstyle/blob/master/pom.xml#L582.
“propertyExpansion”的灵感来自:https: //github.com/checkstyle/checkstyle/blob/master/pom.xml#L582。

