1、设置user.properties
- #项目名称
- project.name=WebService_Restful
- #项目所在的路径
- project.dir=D://workspaces//webservice//Restful
- #源文件所在的路径
- project.src.dir=${project.dir}//src//main//java
- <projectname="checkstyle"default="checkstyle"basedir=".">
- <!--导入属性文件-->
- <propertyfile="user.properties"/>
- <!-------------------
- target:init
- ------------------->
- <targetname="init">
- <tstamp/>
- <!--报告的生成位置-->
- <propertyname="project.checkstyle.report.dir"value="${project.dir}/report"/>
- <!--检测结果的文件名-->
- <propertyname="project.check.result.name"value="${project.name}-checkstyle-result-${DSTAMP}.xml"/>
- <!--检测报告的文件名-->
- <propertyname="project.check.report.name"value="${project.name}-checkstyle-report-${DSTAMP}.html"/>
- <!--所使用的配置文件-->
- <propertyname="checkstyle.config"value="${basedir}/sunrising_checks.xml"/>
- <!--报告风格-->
- <propertyname="checkstyle.report.style"value="${basedir}/contrib/checkstyle-noframes.xsl"/>
- <!--检测结果-->
- <propertyname="checkstyle.result"value="${project.checkstyle.report.dir}/${project.check.result.name}"/>
- <!--检测报告-->
- <propertyname="checkstyle.report"value="${project.checkstyle.report.dir}/${project.check.report.name}"/>
- <!--新建报告所在的文件夹-->
- <mkdirdir="${project.checkstyle.report.dir}"/>
- </target>
- <taskdefresource="checkstyletask.properties"classpath="${basedir}/lib/checkstyle-all-5.0-beta01.jar"/>
- <!-------------------
- target:checkstyle
- ------------------->
- <targetname="checkstyle"depends="init"description="Checkjavacodeandreport">
- <echo>"${checkstyle.config}"</echo>
- <checkstyleconfig="${checkstyle.config}"failOnViolation="false"failureProperty="checkstyle.failure">
- <formattertype="xml"tofile="${checkstyle.result}"/>
- <filesetdir="${project.src.dir}"includes="**/*.java"/>
- </checkstyle>
- <xsltin="${checkstyle.result}"out="${checkstyle.report}"style="${checkstyle.report.style}"/>
- </target>
- <!-------------------
- target:mailTarget
- ------------------->
- <targetname="mailCheckStyleReport">
- <mailmailhost="mail.mydomain.cn"mailport="25"subject="Checkstyleviolation(s)inproject${ant.project.name}"user="myname@mydomain.cn"password="mypass"files="myattchment">
- <fromaddress="ci@mydomain.cn"/>
- <replytoaddress="cimanager@mydomain.cn"/>
- <toaddress="myname@mydomain.cn"/>
- <message>TESTMAILFROM CheckStyle</message>
- </mail>
- </target>
- </project>
3、注意
A)CheckStyle的jar位于当前basedir目录的lib中;
B)javax.activation.jar和mail.jar放到apache-ant-1.7.0/lib目录下,否则mailTarget不可用。
本文介绍如何使用Apache Ant配合CheckStyle进行代码规范检查,并通过邮件发送检查报告。具体包括配置user.properties文件定义项目路径,创建build.xml脚本来执行检查任务,以及发送包含检查结果的邮件。
1万+

被折叠的 条评论
为什么被折叠?



