Grand を使うと Apache Ant ターゲットの依存関係を dot ファイルとして可視化できる.そもそも可視化したくなるほど依存関係が複雑なことが課題ではあると思うけど,既存の build.xml
を解析したり,新しく build.xml
を読むメンバーのために README.md
に図を貼っておいたりという用途はありそう👌
特に新しいツールではなく,また GitHub を見ると2020年頃からメンテナンスは止まってそうだけど,試す機会があってメモとして残しておく❗️
セットアップ
Grand は Grand 自身を Ant ターゲットとして実行できる.まずは GitHub の Releases から grand-1.9.7.jar
をダウンロードしておく.今回は lib/
ディレクトリに置いた.
Grand を実行するターゲットは可視化対象となる build.xml
に直接追加することもできるし,Grand 専用の build.xml
を作ることもできる.今回は後者のアイデアを採用して grand.xml
を以下のように設定した.
<project name="Grand"><target name="grand"><typedef resource="net/ggtools/grand/antlib.xml"classpath="lib/grand-1.9.7.jar"/><grand output="build.dot"buildfile="build.xml" /></target></project>
最終的に以下のようなディレクトリ構成になった👀
. ├── README.md ├── build.xml ├── grand.xml └── lib └── grand-1.9.7.jar
お試し1
まずは Apache Ant のドキュメント Writing a Simple Buildfileに載っている build.xml
を使う.
<project name="MyProject"default="dist"basedir="."><description> simple example build file </description><!-- set global properties for this build --><property name="src"location="src"/><property name="build"location="build"/><property name="dist"location="dist"/><target name="init"><!-- Create the time stamp --><tstamp/><!-- Create the build directory structure used by compile --><mkdir dir="${build}"/></target><target name="compile"depends="init"description="compile the source"><!-- Compile the Java code from ${src} into ${build} --><javac srcdir="${src}"destdir="${build}"/></target><target name="dist"depends="compile"description="generate the distribution"><!-- Create the distribution directory --><mkdir dir="${dist}/lib"/><!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --><jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar"basedir="${build}"/></target><target name="clean"description="clean up"><!-- Delete the ${build} and ${dist} directory trees --><delete dir="${build}"/><delete dir="${dist}"/></target></project>
そして ant -f grand.xml grand
コマンドを実行すると依存関係を可視化できた❗️
お試し2
次に Apache Ant のドキュメント Tutorial: Hello World with Apache Antに載っている build.xml
を使う.
<project name="HelloWorld"basedir="."default="main"><property name="src.dir"value="src"/><property name="build.dir"value="build"/><property name="classes.dir"value="${build.dir}/classes"/><property name="jar.dir"value="${build.dir}/jar"/><property name="main-class"value="oata.HelloWorld"/><target name="clean"><delete dir="${build.dir}"/></target><target name="compile"><mkdir dir="${classes.dir}"/><javac srcdir="${src.dir}"destdir="${classes.dir}"/></target><target name="jar"depends="compile"><mkdir dir="${jar.dir}"/><jar destfile="${jar.dir}/${ant.project.name}.jar"basedir="${classes.dir}"><manifest><attribute name="Main-Class"value="${main-class}"/></manifest></jar></target><target name="run"depends="jar"><java jar="${jar.dir}/${ant.project.name}.jar"fork="true"/></target><target name="clean-build"depends="clean,jar"/><target name="main"depends="clean,run"/></project>
そして ant -f grand.xml grand
コマンドを実行すると依存関係を可視化できた❗️
カスタマイズ用プロパティファイル
可視化した依存関係の色は以下のように決まっている🎨
黄色
: デフォルトターゲット緑色
:description
ありターゲット白色
:description
なしターゲット
カスタマイズ用プロパティファイルを使うと一部の設定を変更できる.今回は以下の設定で grand.properties
を作った.各ノードのフォントサイズを大きくして,description
ありターゲットも 白色(楕円形)
にしてみた.正直 description
は書かないこともあって,デフォルトの 緑色
と 白色
は同じで良いかな〜と思った.
dot.node.attributes=fontsize="20" dot.mainnode.attributes=fillcolor="white"
そして grand.xml
に property
タグの設定と grand
タグに outputconfigfile
オプションを追加すれば OK👌
<project name="Grand"><target name="grand"><property file="grand.properties"/><typedef resource="net/ggtools/grand/antlib.xml"classpath="lib/grand-1.9.7.jar"/><grand output="build.dot"buildfile="build.xml"outputconfigfile="grand.properties"/></target></project>
お試し1で使った build.xml
に対して ant -f grand.xml grand
コマンドを実行すると,ちゃんとフォントサイズが大きくなって,緑色(長方形)
もなくなっていた❗️