TITLE:AntでPMDを実行する

AntでPMDを実行する

#adsense2

編集者:Terai Atsuhiro
作成日:2012-01-24
更新日:2019-08-30 (金) 15:42:28

概要

AntからPMDを実行します。

サンプルターゲット

<target name="pmd">
  <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" />
  <mkdir dir="${build.reports}" />
  <pmd rulesetfiles="java-basic,java-imports,java-strings" encoding="${compile.encoding}">
    <classpath refid="project.class.path" />
    <formatter type="xml" toFile="${build.reports}/pmd.xml" />
    <formatter type="text" toConsole="true" />
    <fileset dir="${src.dir}">
      <include name="**/*.java" />
    </fileset>
  </pmd>
</target>

解説

  1. PMDからダウンロードしたpmd-bin-x.x.ziplib以下にあるpmd-x.x.jarなどのjarファイルを%ANT_HOME%\libにコピー
  2. ant pmdで、pmd.xmlを生成し、jenkinsPMDプラグインなどで読み込む

toFile or toConsole

FindBugsのように、xmlファイルとコンソールの両方に結果を表示したい場合が多いのですが、

ant -v pmd

では、情報が多すぎるのでformatter要素のtoConsole属性を使用します。

  • PMD - Ant Task にあるように、<formatter type="html" toFile="pmd_report.html" toConsole="true"/>や、<formatter type="xml" toFile="${build.reports}/pmd.xml" toConsole="true" />toFiletoConsoleを指定しても、ファイルにのみ結果が書き出されて、コンソールには何も出力されない
  • <formatter type="text" toConsole="true" />と、toFile属性なしでtoConsole="true"とする必要がある?
    • <formatter type="text" toConsole="false" />は、toFile or toConsole needs to be specified in Formatterとなって、BUILD FAILED
  • ファイルとコンソールの両方に出力する場合は、formatterを複数指定しなくてはならない?

PMD 5.0.0

  • RuleSet short names now require a language prefix, 'basic' is now 'java-basic', and 'rulesets/basic.xml' is now 'rulesets/java/basic.xml'
    • 上記のサンプルで、rulesetfiles属性のbasicなどを、java-basicに変更
  • Removed -targetjdk use -version {name} {version} instead
    • 上記のサンプルから、targetjdk="${compile.source}"を削除

参考リンク

コメント