用checkstyle检查java代码的格式
来源:互联网 发布:知乎日报 阅读模式 编辑:程序博客网 时间:2024/03/28 18:34
也如checkstyle其名所示,它只检查,不进行beautifier操作。所以,它应比较适合用于项目管理,用于dailybuild.如果你想借助工具来beautifier你的java源代码,可以参考我写的jalopy安装使用.
下载安装
wget http://umn.dl.sourceforge.net/sourceforge/checkstyle/checkstyle-3.4.tar.gz
tar zxvf checkstyle-3.4.tar.gz
cd checkstyle-3.4
此时,checkstyle自身及其所需的jar包都在这个目录,平常我们只用checkstyle-all-3.4.jar一个包就可以了。子目录contrib下有几xsl文件,是用于检查结果的样式文件。
配置一个检查标准
参考资料2,对如果配置checkstyle作了详细了说明,我这儿用的也是他的配置文件。但ant脚本,建议参考我下面的,他的有一个小错误(我已经在他的文章评论里说了一下。)
进行源代码的自动检查
build.xml的内容如下:
<!DOCTYPE project>
<!-- ANT make file checkstype --><!-- See http://jakarta.apache.org/ant for info about ANT -->
<project name="checkstyle" default="checkstyle" basedir=".">
<target name="init">
<tstamp/>
<!-- CheckStyle配置,这里你替换成你实际的环境 -->
<property name="project.docs.dir" value="${basedir}/contrib" />
<property name="project.src.dir" value="/path/to/your/source/codes" />
<property name="project.checkstyleReport.dir" value="${basedir}/build" />
<property name="checkstyle.jar" value="${basedir}/checkstyle-all-3.4.jar"/>
<property name="checkstyle.config" value="${project.docs.dir}/mycheck.xml"/>
<property name="checkstyle.report.style" value="${project.docs.dir}/checkstyle-noframes.xsl"/>
<property name="checkstyle.result" value="${project.checkstyleReport.dir}/checkstyle_result.xml"/>
<property name="checkstyle.report" value="${project.checkstyleReport.dir}/checkstyle_report.html"/>
</target>
<!--CheckStyle脚步-->
<taskdef resource="checkstyletask.properties" classpath="${checkstyle.jar}" />
<target name="checkstyle" depends="init" description=" 对java源代码进行检查并产生检查报告. ">
<checkstyle config="${checkstyle.config}" failOnViolation="false" failureProperty="checkstyle.failure">
<formatter type="xml" tofile="${checkstyle.result}"/>
<fileset dir="${project.src.dir}" includes="**/*.java"/>
</checkstyle>
<!-- 生成报告,其格式取决于${checkstyle.report.style} -->
<style in="${checkstyle.result}" out="${checkstyle.report}" style="${checkstyle.report.style}"/>
</target>
<!-- 当有不规范的情况发生时将检查结果发送到 -->
<target name="checkstyle-nightly"
depends="checkstyle"
if="checkstyle.failure"
description="Sends email if checkstyle detected code conventions violations.">
<mail from="发信方的email"
tolist="收信方的email"
mailhost="发信方email的服务器"
subject=" checkstyle result from project reports"
files="${checkstyle.report}"/>
</target></project>
现在,运行ant checkstyle-nightly,如果一切正常,你就会在收信方的邮箱里找到相应的邮件了。
- 这个build.xml默认要放chechstyle-3.4那个目录下,checkstyle的配置文件叫mycheck.xml放在contrib子目录下;
- 根据你的ant的安装情况,可能需要将javamail的包复制到$ANT_HOME/lib下。
参考资料:
- Checkstyle Home Page
- Olics, CheckStyle使用详解
- Ant 安装、配置
- 用checkstyle检查java代码的格式
- 用checkstyle检查java代码的格式
- 检查代码格式checkstyle
- Java代码检查 Checkstyle
- checkstyle检查java代码
- 有效检查Java代码的工具:PMD、Checkstyle、findbugs、Jalopy
- Eclipse中使用Checkstyle,checkstyle插件检查java代码的自定义配置文件:
- Eclipse中使用Checkstyle,checkstyle插件检查java代码的自定义配置文件
- java中代码检查checkStyle结果分析
- java中代码检查checkStyle结果分析 .
- JAVA 静态代码分析--规范检查-checkstyle
- java中代码检查checkStyle结果分析
- 用checkstyle实现svn的代码规范性检查
- 用checkstyle实现svn的代码规范性检查
- 用checkstyle实现svn的代码规范性检查
- 静态代码检查-CheckStyle
- CheckStyle代码样式检查
- CheckStyle检查代码
- 四、标准建模语言UML支持环境
- 我来开个头
- VC中的一些常用方法(20条) [转帖]
- 华为面试的一道题目
- post提交数据
- 用checkstyle检查java代码的格式
- 今天总算把blog架设起来了,^_^
- 为每个多文档子窗口创建状态栏
- 使用Form和QueryString集合
- 害怕 - 林俊杰 - 第二天堂
- 财富箴言
- 2004-11-19 14:25:00 一条小生命不幸远去
- javadoc,在 Java 的注释上做文章
- Tomcat 部署应用问题一网打尽