Ant: catching all marked lines in text files for creating a changes document

MartianSoftware has released (a long time ago) two Ant task: <snip> and <rundoc>. While I am not sure about the benefits of <rundoc> (as I dont know docbook really well) <snip> seems to be a nice task. Extracting parts of a file with Ant on board utilities is not that easy.

Using the snip task extracting is a few-liner. Just think we have a class

/**
 * This is normal JavaDoc comment.
 * @@snip:changes@@
 * This is a comment which should be in the changes.txt!
 * @@endSnip@@
 * @author Jan Materne
 */
public class MyProgram {
}

Ant I want to generate the document containing this line
This is a comment which should be in the changes.txt!

<project>
<property name="build.dir" value="build"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="snip">
        <!-- Getting snip task -->
        <mkdir dir="${build.dir}"/>
        <get src="http://www.martiansoftware.com/lab/snip/snip-0.11.jar" dest="${build.dir}/snip.jar"/>
        <taskdef name="snip" classname="com.martiansoftware.snip.Snip" classpath="${build.dir}/snip.jar"/>
        <!-- Collecting the information -->
        <snip>
            <fileset dir="src" includes="**/*.java" />
        </snip>
        <!-- Use these informaion -->
        <echo file="${build.dir}/changes.txt">
        Changes
        -----------------------------------------------------------------
        ${snip.changes}
        </echo>
        <!-- For easier test: output the file -->
        <concat><fileset file="${build.dir}/changes.txt"/></concat>
    </target>

</project>

I can see, that the information is gathered:

..>ant snip
Buildfile: build.xml
snip:
      [get] Getting: http://www.martiansoftware.com/lab/snip/snip-0.11.jar
      [get] To: ...\build\snip.jar
   [concat]
   [concat]         Changes
   [concat]         -----------------------------------------------------------------
   [concat]
   [concat]  * This is a comment which should be in the changes.txt!
   [concat]

BUILD SUCCESSFUL

Fine, let’s try having more information with a second class:

/**
 * This is normal JavaDoc comment.
 * @@snip:changes@@
 * This is another comment which should be in the changes.txt!
 * @@endSnip@@
 *
 * @author Jan Materne
 *
 * @@snip:changes@@
 * Maybe another line in the comments.
 * @@endSnip@@
 */
public class Class2 {
}

And I want to get:

...>ant snip
Buildfile: build.xml

snip:
[get] Getting: http://www.martiansoftware.com/lab/snip/snip-0.11.jar
[get] To: ...\build\snip.jar
[concat]
[concat]         Changes
[concat]         -----------------------------------------------------------------
[concat]
[concat]  * This is a comment which should be in the changes.txt!
[concat]  * This is another comment which should be in the changes.txt!
[concat]  * Maybe another line in the comments.
[concat]

BUILD SUCCESSFUL

But the result doesnt change. Thats because <snip> sets properties directly when getting the @@endSnip@@ mark. And – as Ant properties are immutable – once set a property cant be extended.

It would be better to extend <snip> for first collecting all values and then storing the values into Ant properties.

I thought I could start collecting these lines with Ant built-in facilities could be done like

    <target name="ant" depends="clean">
        <mkdir dir="${build.dir}"/>
        <!-- Collecting and using the information -->
        <concat destfile="${build.dir}/changes.txt">
            <header>
                Changes
                -----------------------------------------------------------------
            </header>
            <fileset dir="src" includes="**/*.java" />
            <filterchain>
                <!-- Something special here -->
            </filterchain>
        </concat>
        <!-- For easier test: output the file -->
        <concat><fileset file="${build.dir}/changes.txt"/></concat>
    </target>

But I couldnt get one file after the next in that filterchain. By default I get one line after the next and then I have troubles for storing the current state (in @@snip-@@endSnip section or out). When using a <tokenfilter><filetokenizer/> I’ll get not only one whole file – I’ll get one token containing ALL catched files including the specified headers. And I dont want to run THAT on a larger codebase …

Ok, it is difficult to gather all content BETWEEN two special markers. But it is very easy to catch all lines with a PREPENDING marker:

    <target name="ant">
        <mkdir dir="${build.dir}"/>
        <!-- Collecting and using the information -->
        <echo file="${build.dir}/changes.txt">
        Changes
        -----------------------------------------------------------------
        </echo>
        <concat destfile="${build.dir}/changes.txt" append="true">
            <fileset dir="src" includes="**/*.java" />
            <filterchain>
	<linecontainsregexp>
                    <regexp pattern="^&#91; \*&#93;*CHANGES:"/>
                </linecontainsregexp>
                <replaceregex pattern="^&#91; \*&#93;*CHANGES: *" replace=""/>
            </filterchain>
        </concat>
        <!-- For easier test: output the file -->
        <concat><fileset file="${build.dir}/changes.txt"/></concat>
    </target>
/**
 * This is normal JavaDoc comment.
 * CHANGES: This is a comment which should be in the changes.txt!
 * @author Jan Materne
 */
public class MyProgram {
}
Explore posts in the same categories: Ant, en

Schlagwörter: , ,

You can comment below, or link to this permanent URL from your own site.

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s


%d Bloggern gefällt das: