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

はじめに

http://blogs.yahoo.co.jp/dk521123/34440556.html
http://blogs.yahoo.co.jp/dk521123/34977781.html
http://blogs.yahoo.co.jp/dk521123/33846615.html
で、JUnitを回すことができたので、
今度は、 プラグインを入れて、管理する。

対象プラグイン

* 挙げだしたらきりがなさそうで初級なので代表的なものだけ
 * コードカバレッジ測定ツール:Cobertura
 * 静的解析ツール:Checkstyle
 * プログラム静的解析ツール:FindBugs

準備

 * Antで、各プラグインを実行するように設定する

Coberturaの設定

 * 以下の関連記事を参照のこと。
http://blogs.yahoo.co.jp/dk521123/35318328.html

Checkstyleの設定

 * 以下の関連記事を参照のこと。
http://blogs.yahoo.co.jp/dk521123/35320406.html

FindBugsの設定

 * 以下の関連記事を参照のこと。
http://blogs.yahoo.co.jp/dk521123/35316512.html

手順

■ 環境設定:プラグインのインストール

* Jenkins が起動し、ブラウザでJenkinsのホーム画面を開いているものとして...
[http://localhost:8080/jenkins/]
 [1] Jenkisのホーム画面から[Jenkinsの管理]-[プラグインの管理]-[利用可能]から以下の項目にチェックを付けて
    「再起動せずにインストールする」ボタン押下

  + Checkstyle Plug-in
  + Cobertura Plugin
  + FindBugs Plug-in

 => インストールを完了後、リンク「ページの先頭へ戻る」でメイン画面に戻る

■ JenkinsのJobへの適用例

[1] 各モジュールを起動できる Ant の build.xml を作成する(以下の「build.xml」を参照のこと)

[2] 適用したいジョブを選択し、左側のリンク[設定]-[ビルド後の処理の追加]-[Checkstyle警告の集計](※)を選択
    # ※同様に他のプラグインについては、[FindBugs警告の集計]や[Cobertura カバレッジレポートの集計]を選択

[3] Jenkinsの[(対象プロジェクト。ここでは「SampleJava」)]-[設定]を選択し、以下の設定をする

 [3-1] 「ビルド」欄で、以下を設定
 * Antの呼び出し
   + 使用するAnt : 
   + ターゲット : 「Antの対象ターゲット」(ここでは「run.all」)

 [3-2] 「ビルド後の処理」を押下し、以下を設定し、「保存」押下
 * ビルド後の処理
   + Checkstyle警告の集計
   - 集計するファイル : 「Checkstyleの結果出力先」
                            (ここでは「SampleJava/checkstyle/checkstyle-result.xml」)
   + findbugs警告の集計
   - 集計するファイル : 「findbugsの結果出力先」
                            (ここでは「SampleJava/report/findbugs_report.xml」)
   + Cobertura カバレッジ・レポートの集計
   - Cobertura XMLレポート パターン : 「Coberturaの結果出力先」
                                          (ここでは「SampleJava/cobertura/coverage.xml」)
   - ソースエンコーディング : 「Cobertura結果の文字コード」
                                 (ここでは「UTF-8」)
   + JUnitテスト結果の集計
   - テスト結果XML : 「JUnitの結果出力先」(ここでは「SampleJava/junit/*.xml」)
[4] 「ビルド実行」押下

 => これで実行できる(「コンソール出力」で詳細な実行結果も見れる)

build.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="run.all" name="SampleJava">
  <property name="junit.path"
    location="C:/eclipse/plugins/org.junit_4.12.0.v201504281640/junit.jar" />
  <property name="hamcrest.path"
    location="C:/eclipse/plugins/org.hamcrest.core_1.3.0.v201303031735.jar" />
  <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="checkstyle.output.dir" location="${basedir}/checkstyle" />
  <property name="src.dir" location="${basedir}/src" />
  <property name="build.dir" location="${basedir}/class" />
  <property name="test.dir" location="${basedir}/test" />
  <property name="junit.output.dir" location="junit" />
  <property name="build.checkstyle.dir" location="${build.dir}/checkstyle" />
  <property name="cobertura.dir" location="${lib.dir}/cobertura" />
  <property name="cobertura.output.dir" location="${basedir}/cobertura" />
  <property name="build.reports.dir" location="${build.dir}/reports" />
  <property name="build.coverage.dir" location="${build.dir}/coverage" />
  <property name="build.reports.dir" location="${build.dir}/reports" />
  <property name="build.instrumented.dir" location="${build.dir}/instrumented-classes" />
  <property name="findbugs.dir" value="${basedir}/libs/findbugs" />
  <property name="report.dir" value="${basedir}/report" />
  <path id="src.classpath">
    <pathelement location="${build.dir}" />
  </path>
  <path id="cobertura.classpath">
    <fileset dir="${cobertura.dir}">
      <include name="cobertura-2.1.1.jar" />
      <include name="lib/*.jar" />
    </fileset>
  </path>
  <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>
  <taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
  <target name="run.all">
    <antcall target="coverage.report" />
    <antcall target="run.checkstyle" />
    <antcall target="run.findbugs" />
  </target>
  <target name="instrument" depends="compile.for.test">
    <delete dir="${build.instrumented.dir}" />
    <mkdir dir="${build.instrumented.dir}" />
    <cobertura-instrument todir="${build.instrumented.dir}">
      <fileset dir="${build.dir}">
        <include name="**/*.class" />
      </fileset>
    </cobertura-instrument>
  </target>
  <target name="compile.for.test" 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" />
    <javac srcdir="${test.dir}" destdir="${build.dir}" debug="true" debuglevel="vars,lines,source" verbose="true" includeantruntime="false" classpath="${junit.path}" />
  </target>
  <target name="test" depends="instrument" description="Unit test the application">
    <delete dir="${junit.output.dir}" />
    <mkdir dir="${junit.output.dir}" />
    <junit fork="yes" showoutput="yes" dir="${junit.output.dir}" errorProperty="test.failed" failureProperty="test.failed">
      <jvmarg value="-XX:-UseSplitVerifier" />
      <sysproperty key="net.sourceforge.cobertura.datafile"
        file="${basedir}/cobertura.ser" />
      <formatter type="xml" />
      <classpath refid="src.classpath" />
      <classpath>
        <fileset dir="${lib.dir}">
          <include name="**/*.jar" />
        </fileset>
        <pathelement path="${junit.path}" />
        <pathelement path="${hamcrest.path}" />
        <pathelement path="${junit.output.dir}" />
        <pathelement path="${build.dir}" />
      </classpath>
      <batchtest todir="${junit.output.dir}">
        <fileset dir="${test.dir}">
          <include name="**/*Test.java" />
        </fileset>
      </batchtest>
    </junit>
  </target>
  <target name="coverage.report" depends="test">
    <delete dir="${cobertura.output.dir}" />
    <mkdir dir="${cobertura.output.dir}" />
    <cobertura-report format="xml" encoding="UTF-8"
      destdir="${cobertura.output.dir}">
      <fileset dir="${src.dir}">
        <include name="**/*.java"/>
        <exclude name="**/package-info.java" />
      </fileset>
    </cobertura-report>
  </target>
  <target name="run.checkstyle" description="Run checkstyle">
    <delete dir="${checkstyle.output.dir}" />
    <mkdir dir="${checkstyle.output.dir}" />
    <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" />
      <formatter type="xml"
        toFile="${checkstyle.output.dir}/checkstyle-result.xml" />
    </checkstyle>
  </target>
  <target name="run.findbugs" description="Run findbugs">
    <mkdir dir="${report.dir}" />
    <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"
      classpath="${findbugs.dir}/lib/findbugs-ant.jar">
    </taskdef>
    <findbugs home="${findbugs.dir}" output="xml"
      outputFile="${report.dir}/findbugs_report.xml">
      <class location="${build.dir}" />
    </findbugs>
  </target>
</project>


関連記事

■ 関連記事シリーズ

Jenkins ~ 初級設定編 / (1) Jenkins の設定 ~

http://blogs.yahoo.co.jp/dk521123/34440556.htmlし

Jenkins ~ 初級設定編 / (2) Ant の設定 ~

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

Jenkins ~ 初級設定編 / (3) Jenkins で JUnit を定期的に実行する ~

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

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

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

■ 前提条件

 * 上の4つの記事を見れば、わかることだが、
   最低条件の前提条件とその関連記事を記載しておく

[1] JDK/Eclipseを設定する

 * 以下の関連記事を参照のこと
http://blogs.yahoo.co.jp/dk521123/31934496.html

[2] Subversionを設定する

 * 以下の関連記事を参照のこと
http://blogs.yahoo.co.jp/dk521123/30541036.html

[3] Subversiveプラグインを設定する(EclipseSubversionを連携させるため)

 * 以下の関連記事を参照のこと
http://blogs.yahoo.co.jp/dk521123/6745594.html

[4] Jenkins を設定する

 * Jenkins自体の設定は、以下の関連記事を参照のこと
http://blogs.yahoo.co.jp/dk521123/34440556.html

■ その他

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

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

FindBugs プラグイン ~プログラム静的解析ツール~

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