1、设置 user.properties
- # 项目名称
- project.name=WebService_Restful
- # 项目所在的路径
- project.dir=D://workspaces//webservice//Restful
- # 源文件所在的路径
- project.src.dir=${project.dir}//src//main//java
- <project name="checkstyle" default="checkstyle" basedir=".">
- <!-- 导入属性文件 -->
- <property file="user.properties" />
- <!-- - - - - - - - - - - - - - - - - -
- target: init
- - - - - - - - - - - - - - - - - - -->
- <target name="init">
- <tstamp />
- <!-- 报告的生成位置 -->
- <property name="project.checkstyle.report.dir" value="${project.dir}/report" />
- <!-- 检测结果的文件名 -->
- <property name="project.check.result.name" value="${project.name}-checkstyle-result-${DSTAMP}.xml" />
- <!-- 检测报告的文件名 -->
- <property name="project.check.report.name" value="${project.name}-checkstyle-report-${DSTAMP}.html" />
- <!-- 所使用的配置文件 -->
- <property name="checkstyle.config" value="${basedir}/sunrising_checks.xml" />
- <!-- 报告风格 -->
- <property name="checkstyle.report.style" value="${basedir}/contrib/checkstyle-noframes.xsl" />
- <!-- 检测结果 -->
- <property name="checkstyle.result" value="${project.checkstyle.report.dir}/${project.check.result.name}" />
- <!-- 检测报告 -->
- <property name="checkstyle.report" value="${project.checkstyle.report.dir}/${project.check.report.name}" />
- <!-- 新建报告所在的文件夹 -->
- <mkdir dir="${project.checkstyle.report.dir}" />
- </target>
- <taskdef resource="checkstyletask.properties" classpath="${basedir}/lib/checkstyle-all-5.0-beta01.jar" />
- <!-- - - - - - - - - - - - - - - - - -
- target: checkstyle
- - - - - - - - - - - - - - - - - - -->
- <target name="checkstyle" depends="init" description="Check java code and report ">
- <echo>"${checkstyle.config}"</echo>
- <checkstyle config="${checkstyle.config}" failOnViolation="false" failureProperty="checkstyle.failure">
- <formatter type="xml" tofile="${checkstyle.result}" />
- <fileset dir="${project.src.dir}" includes="**/*.java" />
- </checkstyle>
- <xslt in="${checkstyle.result}" out="${checkstyle.report}" style="${checkstyle.report.style}" />
- </target>
- <!-- - - - - - - - - - - - - - - - - -
- target: mailTarget
- - - - - - - - - - - - - - - - - - -->
- <target name="mailCheckStyleReport">
- <mail mailhost="mail.mydomain.cn" mailport="25" subject="Checkstyle violation(s) in project ${ant.project.name}" user="myname@mydomain.cn" password="mypass" files="myattchment">
- <from address="ci@mydomain.cn" />
- <replyto address="cimanager@mydomain.cn" />
- <to address="myname@mydomain.cn" />
- <message>TEST MAIL FROM CheckStyle</message>
- </mail>
- </target>
- </project>
3、注意
A)CheckStyle的jar位于当前basedir目录的lib中;
B)javax.activation.jar和mail.jar放到apache-ant-1.7.0/lib目录下,否则mailTarget不可用。