Java 检查样式+;抑制滤波器
我有一个checkstyle抑制过滤器设置(例如,忽略单元测试代码中的幻数) 抑制xml文件与checkstyle xml文件位于同一文件夹中。但是,此文件的实际位置有所不同: 在我的windows开发框中,它位于d:\dev\shared\checkstyle\config中 在Linux CI服务器上,它将位于/root/repo/shared/checkstyle/config中 在另一个开发者框中,它可能在任何地方(他们查看他们的svn回购协议) 唯一“一致”的是,抑制文件始终与checkstyle xml文件位于同一文件夹中。 我无法确定如何确保始终拾取此文件。我也不知道为什么checkstyle不支持在checkstyle xml文件中嵌入抑制Java 检查样式+;抑制滤波器,java,code-analysis,static-analysis,checkstyle,Java,Code Analysis,Static Analysis,Checkstyle,我有一个checkstyle抑制过滤器设置(例如,忽略单元测试代码中的幻数) 抑制xml文件与checkstyle xml文件位于同一文件夹中。但是,此文件的实际位置有所不同: 在我的windows开发框中,它位于d:\dev\shared\checkstyle\config中 在Linux CI服务器上,它将位于/root/repo/shared/checkstyle/config中 在另一个开发者框中,它可能在任何地方(他们查看他们的svn回购协议) 唯一“一致”的是,抑制文件始终与chec
有什么帮助吗?当我在Linux和Windows之间来回切换时,我在Checkstyle抑制配置方面遇到了同样的问题。下面是我如何在基于Ant的构建系统中解决它的: 基本上,我通过使用Ant构建脚本配置Checkstyle属性文件,将适当的、特定于平台的目录值注入到主Checkstyle配置文件中 我的主Checkstyle配置文件有一个
SuppressionFilter
模块声明,如下所示。检查样式抑制文件
属性的值来自检查样式属性文件:
<module name="SuppressionFilter">
<property name="file" value="${checkstyle-suppressions-file}"/>
</module>
我的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>
然后我调用checkstyle
Ant任务,如下所示:
<checkstyle config="${scm.dir}/checkstyle_checks.xml"
properties="${scm.dir}/checkstyle.properties">
<!-- details elided -->
</checkstyle>
<module name="SuppressionFilter">
<property name="file" value="${config_dir}/my_suppressions.xml"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="${samedir}/suppressions.xml"/>
</module>
<checkstyle config="${checkstyle.config}/checkstyle-checks.xml">
<!-- ... -->
<property key="samedir" value="${checkstyle.config}"/>
</checkstyle>
调用checkstyle
任务将checkstyle.properties
文件中包含的键/值对注入到主checkstyle配置中
如果您愿意,可以查看完整的脚本
希望这有助于我使用
ant.file
变量和项目名称获取build.xml
所在目录的绝对路径:
<project name="common" ... >
<dirname property="thisdir" file="${ant.file.common}"/>
由于
thisdir
变量来自ant,因此它似乎不需要路径分隔符转换。如果您正在使用eclipse,并且抑制文件与外部checkstyle配置位于同一目录中,则可以设置如下抑制过滤器:
<checkstyle config="${scm.dir}/checkstyle_checks.xml"
properties="${scm.dir}/checkstyle.properties">
<!-- details elided -->
</checkstyle>
<module name="SuppressionFilter">
<property name="file" value="${config_dir}/my_suppressions.xml"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="${samedir}/suppressions.xml"/>
</module>
<checkstyle config="${checkstyle.config}/checkstyle-checks.xml">
<!-- ... -->
<property key="samedir" value="${checkstyle.config}"/>
</checkstyle>
在eclipse中,我添加了以下内容,这些内容不需要我添加任何其他属性:
<module name="SuppressionFilter">
<property name="file" value="${samedir}/suppressions.xml"/>
</module>
我认为Robert的答案可以扩展到一个简单的解决方案,用于ant和Eclipse: 在配置XML中包含抑制文件,如下所示:
<checkstyle config="${scm.dir}/checkstyle_checks.xml"
properties="${scm.dir}/checkstyle.properties">
<!-- details elided -->
</checkstyle>
<module name="SuppressionFilter">
<property name="file" value="${config_dir}/my_suppressions.xml"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="${samedir}/suppressions.xml"/>
</module>
<checkstyle config="${checkstyle.config}/checkstyle-checks.xml">
<!-- ... -->
<property key="samedir" value="${checkstyle.config}"/>
</checkstyle>
现在,Eclipse感到满意并找到了该文件
要让ant工作,请将目标更新为以下内容:
<checkstyle config="${scm.dir}/checkstyle_checks.xml"
properties="${scm.dir}/checkstyle.properties">
<!-- details elided -->
</checkstyle>
<module name="SuppressionFilter">
<property name="file" value="${config_dir}/my_suppressions.xml"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="${samedir}/suppressions.xml"/>
</module>
<checkstyle config="${checkstyle.config}/checkstyle-checks.xml">
<!-- ... -->
<property key="samedir" value="${checkstyle.config}"/>
</checkstyle>
希望这有帮助。由于CheckStyle 4.26.0,您可以在配置文件中使用预定义的常量 ():
- ${basedir}&${project_loc}-映射到当前项目目录
- ${workspace\u loc}-映射到当前Eclipse工作区目录
- ${config_loc}&${samedir}-映射到配置文件所在的目录
<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>
org.apache.maven.plugins
.谢谢你的提示,我的ant构建现在很好,忽略了测试类中的神奇数字(这是一个开始!)。我不得不对您提供的代码进行一次更改(供以后可能会用到的其他人参考),该任务缺少一个“filtering=true”;至少在运行我的ant版本时是这样。