ant task to compute lines of code

Discussion in 'Java' started by Laura Schmidt, Nov 18, 2012.

  1. Hello,

    I want to find out the total lines of code of a project using an ant task.

    On the commandline I would do this like this:

    find src -name *.java | wc -l

    In ant, I only managed to output all the code:

    <target name="loc">
    <exec executable="find">
    <arg line="src -name *.java -exec cat {} ;"/>
    </exec>
    </target>

    So I can get the loc like this:

    ant -f project.xml loc | wc -l

    But how can I integrate the "wc -l" in the ant task?

    Thanks
    Laura
     
    Laura Schmidt, Nov 18, 2012
    #1
    1. Advertising

  2. In article <k8avks$nl8$-online.net>,
    Laura Schmidt <> wrote:

    > I want to find out the total lines of code of a project using an ant task.
    >
    > On the commandline I would do this like this:
    >
    > find src -name *.java | wc -l
    >
    > In ant, I only managed to output all the code:
    >
    > <target name="loc">
    > <exec executable="find">
    > <arg line="src -name *.java -exec cat {} ;"/>
    > </exec>
    > </target>
    >
    > So I can get the loc like this:
    >
    > ant -f project.xml loc | wc -l
    >
    > But how can I integrate the "wc -l" in the ant task?


    The Exec task should work:

    <http://ant.apache.org/manual/Tasks/exec.html>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Nov 18, 2012
    #2
    1. Advertising

  3. On 11/18/2012 04:43 PM, Laura Schmidt wrote:
    > Hello,
    >
    > I want to find out the total lines of code of a project using an ant task.
    >
    > On the commandline I would do this like this:
    >
    > find src -name *.java | wc -l


    No, you wouldn't. That would give you the number of source files - at
    best. Worst case is an error from "find" indicating invalid command
    line arguments. Other variants are also possible, e.g. including
    directories or symlinks in the count.

    More successful approaches

    find src -type f -name \*.java -exec cat {} + | wc -l

    {
    echo 0
    find -type f -name \*.java -exec wc -l {} + \
    | sed -n 's#^ *\([0-9]\+\) \+total$#\1+#p'
    echo n
    } | dc

    Btw, all these also count empty lines. For non empty lines something
    like this could work

    {
    echo 0
    find -type f -name \*.java -exec egrep -hc '[^[:space:]]' {} + \
    | sed 'a +'
    echo n
    } | dc


    Kind regards

    robert
     
    Robert Klemme, Nov 18, 2012
    #3
  4. Laura Schmidt

    Arne Vajhøj Guest

    On 11/18/2012 10:43 AM, Laura Schmidt wrote:
    > Hello,
    >
    > I want to find out the total lines of code of a project using an ant task.
    >
    > On the commandline I would do this like this:
    >
    > find src -name *.java | wc -l
    >
    > In ant, I only managed to output all the code:
    >
    > <target name="loc">
    > <exec executable="find">
    > <arg line="src -name *.java -exec cat {} ;"/>
    > </exec>
    > </target>
    >
    > So I can get the loc like this:
    >
    > ant -f project.xml loc | wc -l
    >
    > But how can I integrate the "wc -l" in the ant task?


    I would code an ant task to do it.

    Actually I have.

    :)

    See below.

    Arne

    ================

    code:

    package dk.vajhoej.anttasks.codestats;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.tools.ant.DirectoryScanner;
    import org.apache.tools.ant.Task;
    import org.apache.tools.ant.types.FileSet;

    public class JavaCodeStatsTask extends Task {
    private String label;
    private boolean cf;
    private boolean cl;
    private List<FileSet> allfs = new ArrayList<FileSet>();
    @Override
    public void execute() {
    int nf = 0;
    int nl = 0;
    for(FileSet fs : allfs) {
    DirectoryScanner ds = fs.getDirectoryScanner();
    for(String fnm : ds.getIncludedFiles()) {
    File f = new File(ds.getBasedir(), fnm);
    nf++;
    try {
    BufferedReader br = new BufferedReader(new
    FileReader(f));
    while(br.readLine() != null) {
    nl++;
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    System.out.println(label + ":");
    if(cf) {
    System.out.println(" Number of files: " + nf);
    }
    if(cl) {
    System.out.println(" Number of lines: " + nl);
    }
    }
    /**
    * Specify label to use.
    * @param label label
    */
    public void setLabel(String label) {
    this.label = label;
    }
    /**
    * Specify whether to count files.
    * @param cf true=count false=not count
    */
    public void setCountfiles(boolean cf) {
    this.cf = cf;
    }
    /**
    * Specify whether to count lines.
    * @param cl true=count false=not count
    */
    public void setCountlines(boolean cl) {
    this.cl = cl;
    }
    /**
    * Specify a file set to process.
    * @param fs file set
    */
    public void addFileSet(FileSet fs) {
    allfs.add(fs);
    }
    }

    example:

    <target name="count">
    <taskdef name="javacodestats" classpath="${anttaskslib}"
    classname="dk.vajhoej.anttasks.codestats.JavaCodeStatsTask"/>
    <javacodestats label="Library" countfiles="yes" countlines="yes">
    <fileset dir="src" includes="dk/vajhoej/record/*.java"/>
    </javacodestats>
    <javacodestats label="Unit test" countfiles="yes" countlines="yes">
    <fileset dir="src" includes="dk/vajhoej/record/test/*.java"/>
    </javacodestats>
    </target>
     
    Arne Vajhøj, Nov 18, 2012
    #4
  5. Hi Robert!

    On 11/18/2012 05:24 PM, Robert Klemme wrote:
    > On 11/18/2012 04:43 PM, Laura Schmidt wrote:


    >> On the commandline I would do this like this:
    >> find src -name *.java | wc -l

    > No, you wouldn't.


    Sorry, this was a copy/paste error.
    My ant task approach cats all the files:

    <target name="loc">
    <exec executable="find">
    <arg line="src -name *.java -exec cat {} ;"/>
    </exec>
    </target>

    My problem with this approach and also with your approaches is to fit
    the piped command line into the ant tag, e. g. to integrate the "| wc
    -l" into the target tag above.

    Laura
     
    Laura Schmidt, Nov 19, 2012
    #5
  6. Laura Schmidt

    Lew Guest

    Laura Schmidt wrote:
    > Sorry, this was a copy/paste error.
    > My ant task approach cats all the files:


    You could exec "wc -l $(find src -name *.java)".

    Not sure what the attraction is of "cat" when "wc"
    takes files as arguments.

    $ wc --help
    Usage: wc [OPTION]... [FILE]...
    or: wc [OPTION]... --files0-from=F
    Print newline, word, and byte counts for each FILE, and a total line if
    more than one FILE is specified. With no FILE, or when FILE is -,
    read standard input. A word is a non-zero-length sequence of characters
    delimited by white space.
    The options below may be used to select which counts are printed, always in
    the following order: newline, word, character, byte, maximum line length.
    -c, --bytes print the byte counts
    -m, --chars print the character counts
    -l, --lines print the newline counts
    --files0-from=F read input from the files specified by
    NUL-terminated names in file F;
    If F is - then read names from standard input
    -L, --max-line-length print the length of the longest line
    -w, --words print the word counts
    --help display this help and exit
    --version output version information and exit

    --
    Lew
     
    Lew, Nov 19, 2012
    #6
  7. On Monday, November 19, 2012 5:35:05 AM UTC+1, Lew wrote:

    > You could exec "wc -l $(find src -name *.java)".


    That's error prone in light of file and directory names with spaces and line breaks (not too common, I know). In this case

    find src -name \*.java -exec wc -l {} +

    is better.

    > Not sure what the attraction is of "cat" when "wc"
    > takes files as arguments.


    One does not have to do the totaling manually. Also "wc" will only output a single numeric value which is convenient if you want to further process it.

    Kind regards

    robert
     
    Robert Klemme, Nov 19, 2012
    #7
  8. Laura Schmidt

    Jeff Higgins Guest

    On 11/18/2012 10:43 AM, Laura Schmidt wrote:
    > Hello,
    >
    > I want to find out the total lines of code of a project using an ant task.
    >

    First hit for "ant task word count":
    <http://antcount.sourceforge.net/>
     
    Jeff Higgins, Nov 19, 2012
    #8
  9. Laura Schmidt

    Roedy Green Guest

    On Sun, 18 Nov 2012 11:19:18 -0500, "John B. Matthews"
    <> wrote, quoted or indirectly quoted someone who
    said :

    ><http://ant.apache.org/manual/Tasks/exec.html>


    e.g.
    <!-- J E T -->
    <!-- Requires Excelsior JET native Java compiler jc.exe on the path
    -->
    <!-- See http://mindprod.com/jgloss/jet.html for details -->
    <target name="jet" depends="prejet" unless="jet.uptodate">
    <echo message=" ::: bio ::: jet compiling." />
    <exec executable="jc.exe" dir="com/mindprod/bio">
    <arg value="-DECOR=" />
    <arg value="bio.jar" />
    </exec>
    <!-- copy exe to E:/sys -->
    <copy file="com/mindprod/bio/bio.exe" todir="E:/sys" />
    </target>
    I would send the output to files one per module, then consolidate them
    later.


    --
    Roedy Green Canadian Mind Products http://mindprod.com
    Students who hire or con others to do their homework are as foolish
    as couch potatoes who hire others to go to the gym for them.
     
    Roedy Green, Nov 20, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor
    Replies:
    0
    Views:
    8,849
    Victor
    Sep 1, 2004
  2. krabhi
    Replies:
    1
    Views:
    9,323
    Marco Meschieri
    Aug 9, 2006
  3. teggy
    Replies:
    0
    Views:
    822
    teggy
    May 29, 2007
  4. Mike
    Replies:
    1
    Views:
    1,950
    GArlington
    May 12, 2008
  5. PerlFAQ Server
    Replies:
    0
    Views:
    271
    PerlFAQ Server
    Feb 2, 2011
Loading...

Share This Page