maven-antrun-pluginを使用してMaven2経由でStepCounterを使用する設定
StepCounterをダウンロード
StepCounterはMaven2のセントラルリポジトリで管理されていないため、ここからStepCounterをダウンロードしてきます。今日現在の最新版は1.14です。
ローカルリポジトリにStepCounterを登録
ダウンロードしたファイルの中にあるstepcounter.jarをmvn install:install-fileコマンドを使用して、ローカルリポジトリにStepCounterを登録します。
mvn install:install-file -Dfile=C:\apache-maven-2.0.9\install-lib\stepcounter.jar -DgroupId=tk.eclipse.plugin -DartifactId=stepcounter -Dversion=1.14.0 -Dpackaging=jar -DgeneratePom=true
pom.xmlの修正
pom.xmlの/project/build/plugins以下にantrunの設定として記述します。
<build> <plugins> … <!-- StepCounter --> <plugin> <dependencies> <dependency> <groupId>tk.stepcounter.ant</groupId> <artifactId>stepcounter</artifactId> <version>1.14.0</version> </dependency> </dependencies> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>site</phase> <configuration> <tasks> <taskdef name="stepcounter" classname="tk.stepcounter.ant.StepCounterTask" classpath ="stepcounter.jar"/> <stepcounter format="csv" output="${project.build.directory}/stepcounter-sources.csv"> <fileset dir="${project.build.sourceDirectory}" includes="**/*.java" /> </stepcounter> <stepcounter format="csv" output="${project.build.directory}/stepcounter-testsources.csv"> <fileset dir="${project.build.testSourceDirectory}" includes="**/*.java" /> </stepcounter> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
この例では、ソース用とテストソース用にそれぞれ集計結果を分けて出力していますが、StepCounterの出力結果にはパッケージ名が記載されないので、ソースが多い場合にはパッケージ単位など、もう少し細かく指定したほうがよさそうです。
実行結果
出力オプションにformat="csv"を付けるかどうかで出力内容が変わります。
結果ファイルをプログラムが読む場合にはcsvにしたほうがよさそうです。
オプションなし
ファイル 種類 実行 空行 コメント 合計 ---------------------------------------------------------------------- HelloWorld.java Java 8 6 5 19 HelloWorldApplication.java Java 15 5 0 20 ---------------------------------------------------------------------- 合計 23 11 5 39
オプションあり
HelloWorld.java,Java,8,6,5,19 HelloWorldApplication.java,Java,15,5,0,20