【Ant】 Checkstyle を Ant で実行するには

  ■ 環境

OS      : Windows10
Eclipse : Mars Release (4.5.0)
Ant     : Eclpse付属

 

  ■ 設定

[1] 以下のサイトから、Checkstyle をダウンロードする

# 今回は、「checkstyle-6.10.1-bin.zip」
http://checkstyle.sourceforge.net/
http://sourceforge.net/projects/checkstyle/files/checkstyle/
[2] zipを解凍し、「checkstyle-x.xx.x-all.jar(x:version)」「conf」を
    対象のプロジェクト配下に配置する

[例] プロジェクト名「SampleJava」

SampleJava
 + src
 + libs
    + Checkstyle<- 今回はここに置いた
        + checkstyle-6.10.1-all.jar
        + conf
           + checkstyle_checks.xml
           + import-control.xml
           + java.header
           + suppressions.xml

[3] build.xmlを書く(後はそれを実行するだけ)

  build.xml

やっとできた...
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="run.checkstyle" name="SampleJava">
  <property name="lib.dir" location="${basedir}/libs" />
  <property name="checkstyle.dir" location="${lib.dir}/checkstyle" />
  <property name="checkstyle.conf.dir" location="${lib.dir}/checkstyle/conf" />
  <property name="src.dir" location="${basedir}/src" />
  <property name="build.dir" location="${basedir}/class" />
  <property name="build.checkstyle.dir" location="${build.dir}/checkstyle" />
  <property name="build.reports.dir" location="${build.dir}/reports" />
  <path id="src.classpath">
    <pathelement location="${build.dir}" />
  </path>
  <taskdef
    resource="com/puppycrawl/tools/checkstyle/ant/checkstyle-ant-task.properties"
    classpath="${checkstyle.dir}/checkstyle-6.10.1-all.jar" />
  <target name="compile" description="Javaコンパイル">
    <delete dir="${build.dir}" />
    <mkdir dir="${build.dir}" />
    <javac srcdir="${src.dir}" destdir="${build.dir}" debug="true" debuglevel="vars,lines,source" includeantruntime="false" />
  </target>
  <target name="run.checkstyle" description="実行">
    <checkstyle config="${checkstyle.conf.dir}/checkstyle_checks.xml" failOnViolation="false">
      <fileset dir="${src.dir}" includes="**/*.java" />
      <property key="checkstyle.header.file" value="${checkstyle.conf.dir}/java.header"/>
      <property key="checkstyle.suppressions.file" value="${checkstyle.conf.dir}/suppressions.xml"/>
      <property key="checkstyle.importcontrol.file" value="${checkstyle.conf.dir}/import-control.xml"/>
    </checkstyle>
  </target>
</project>

  出力結果

Buildfile: C:\eclipse\workspace\SampleJava\build.xml
run.checkstyle:
[checkstyle] Running Checkstyle 6.10.1 on 5 files
[checkstyle] C:\eclipse\workspace\SampleJava\src\com\ant\sample\AntSample.java:0: Missing package-info.java file.
[checkstyle] C:\eclipse\workspace\SampleJava\src\com\ant\sample\AntSample.java:0: ファイルが新しい行で終了していません。
... 略 ...
BUILD SUCCESSFUL
Total time: 1 second

 

 

  関連記事

  Checkstyle に関するトラブルシューティング

http://blogs.yahoo.co.jp/dk521123/35340394.html

  Jenkins ~ 初級設定編 / (4) プラグインを設定 ~

* 本記事を書くきっかけになった記事
http://blogs.yahoo.co.jp/dk521123/35294010.html

  eclipse-cs / Eclipse Checkstyle Plug-in ~静的解析ツール~

http://blogs.yahoo.co.jp/dk521123/32468160.html