AntでCheckstyleを実行する
Total: 9714, Today: 1, Yesterday: 1
Posted by aterai at
Last-modified:
Summary
AntからCheckstyleを実行してソースコードのコーディングルールをチェックします。
サンプルターゲット
build.xmlに記述するターゲットサンプル
<condition property="have.checkstyle">
<available file="${env.CHECKSTYLE_HOME}" />
</condition>
<target name="checkstyle" if="have.checkstyle">
<path id="checkstyle.classpath">
<fileset dir="${env.CHECKSTYLE_HOME}">
<include name="*-all.jar" />
</fileset>
</path>
<taskdef resource="com/puppycrawl/tools/checkstyle/ant/checkstyle-ant-task.properties"
classpathref="checkstyle.classpath" />
<mkdir dir="${build.reports}" />
<checkstyle config="${sca.dir}/checks.xml" failOnViolation="false">
<formatter type="plain" />
<formatter type="xml" toFile="${build.reports}/checkstyle-result.xml" />
<fileset dir="${src.dir}" excludes="**/module-info.java" includes="**/*.java" />
</checkstyle>
</target>
Description
- Releases · checkstyle/checkstyleからダウンロードした
checkstyle-x.x-all.jarを環境変数%CHECKSTYLE_HOME%を設定した場所にコピー - 上記のような
checkstyleターゲットをbuild.xmlに追加し、ant checkstyleでcheckstyle-result.xmlを生成、jenkinsのCheckstyleプラグインなどで読み込む - チェックするルールをカスタマイズする場合は、任意の
${sca.dir}ディレクトリ以下にchecks.xmlを記述し、これをconfig属性で参照する
メモ
module-info.java
8.4ではJava 9のmodule-info.javaに未対応なので、<exclude name="**/module-info.java" />などで回避C:\temp\jst\all.xml:47: Unable to process files: [C:\...\src\java\module-info.java]
Unable to create Root Module
Checkstyleのバージョン更新に伴うモジュールの廃止などの変更で、configファイル(上記の例では${user.dir}/checks.xml)が不正になったときに発生する場合がある
8.2
remove FileContentsHolder module as FileContents object is available for filters on TreeWalker in TreeWalkerAudit Event . Author: Timur, Roman Ivanov #3573<module name="FileContentsHolder" />を削除する必要があるLeftCurlyCheck: clarify behavior of 'nlow' option after removal of 'maxLineLength'. Author: Roman Ivanov #3855<property name="maxLineLength" value="100"/>を削除する必要がある<module name="LeftCurly"> <property name="maxLineLength" value="100"/> </module>
8.1
Make SuppressionCommentFilter and SuppressWithNearbyCommentFilter children of TreeWalker. Author: Timur #4714<module name="SuppressionCommentFilter" />を<module name="TreeWalker">の子要素になるよう移動削除する必要がある
エラー
AvoidEscapedUnicodeCharacters
Checksytle 8.7で、c.getFontMetrics(c.getFont()).charWidth('あ');のようなコードをチェックすると、error Unable to process filesとエラーになるWindows環境で、ファイルのエンコーディングはUTF-8になっているため?JTextField#setText("あああ")などStringは問題なしwidth = c.getFontMetrics(c.getFont()).charWidth('\u3042');にすれば問題ないが、AvoidEscapedUnicodeCharactersと警告される
// @SuppressWarnings("AvoidEscapedUnicodeCharacters")
@Override protected synchronized void damage(Rectangle r) {
if (Objects.nonNull(r)) {
JTextComponent c = getComponent();
x = r.x;
y = r.y;
// width = c.getFontMetrics(c.getFont()).charWidth('w');
// width = c.getFontMetrics(c.getFont()).charWidth('\u3042');
// Checksytle 8.7: error Unable to process files...
width = c.getFontMetrics(c.getFont()).charWidth('あ');
height = r.height;
c.repaint();
}
}
- 以下のように
file.encodingを指定すればエラーにならないAntのcheckstyleタスクではencodingは指定できない?のでANT_OPTSに-Dfile.encoding=UTF-8を設定する必要がある- コマンドラインで
ant -Dfile.encoding=UTF-8 checkstyleとしても効果がない?
"$JAVA_HOME/bin/java" -Dfile.encoding=utf-8 -jar "$CHECKSTYLE_HOME/checkstyle-8.7-all.jar" -c ./checks.xml OverTypeMode/src/java/