ant task to compute lines of code

L

Laura Schmidt

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
 
J

John B. Matthews

Laura Schmidt said:
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>
 
R

Robert Klemme

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
 
A

Arne Vajhøj

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>
 
L

Laura Schmidt

Hi Robert!

On 11/18/2012 04:43 PM, Laura Schmidt wrote:
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
 
L

Lew

Laura said:
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
 
R

Robert Klemme

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
 
R

Roedy Green


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top