How can I Using Ant Tasks Outside of Ant?
1 posts in topic
Flat View  Flat View

Posted By:   Anonymous
Posted On:   Sunday, July 13, 2003 08:18 PM

I already read about [Using Ant Tasks Outside of Ant] topic under http://ant.apache.org/manual/ but I dont know the way to do. Please tell me the way to do that. Thanks so much.

Re: How can I Using Ant Tasks Outside of Ant?

Posted By:   Anonymous  
Posted On:   Friday, July 18, 2003 04:19 AM

Some weeks ago I wrote a little java class for accessing the task. Maybe you find a start here:

import org.apache.tools.ant.taskdefs.FixCRLF;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.PatternSet;
import java.io.File;

public class CLIFixCrLf {

private static boolean debug = false;

// Parameters:
// -sendto let the program set user.dir
// -debug create debug statements
public static void main(String[] args) {
// Where is this program started
String curDir = System.getProperty("user.dir");

// create the instance
FixCRLF fix = new FixCRLF();

// Sets the Ant-Project
Project prj = new Project();
prj.addBuildListener(new org.apache.tools.ant.DefaultLogger());

task specific settings
// Sets CRLF-Mode (windows)
FixCRLF.CrLf crlf = new FixCRLF.CrLf();

// helper class for getting the relative path from absolute paths.
// E.g.: c: emp> CLIFixCrLf c: emp estfile.txt
// wouldn´t work because filesets doesn´t support absolute filenames.
// So we must put c: emp estfile.txt to testfile.txt
FileUtils fu = FileUtils.newFileUtils();
curDir = fu.normalize(curDir).toString();

// Should I set curDir for my own?
// Scenario: on windows make a "SendTo" for the file. No usable curDir is set.
// So I use the directory of the files itself.
boolean mustSetCurDir = false;
String aFile = "";
for (int i=0; i if ("-sendto".equals(args[i])) {
mustSetCurDir = true;
} else if ("-debug".equals(args[i])) {
debug = true;
} else {
aFile = args[i];

for (int i=0; i log(" - " + args[i]);
log("Must set curDir: " + mustSetCurDir);

if (mustSetCurDir) {
curDir = (new File(aFile)).getParent().toString();
log("new curDir="+curDir);

// Sets the files for convertion
for (int i=0; i PatternSet.NameEntry ne = fix.createInclude();

// normalize() only supports absolute filenames
String path =
( (new java.io.File(args[i])).isAbsolute())
? fu.normalize(args[i]).toString()
: args[i];

// thats the path we can use
String relativePath =
(path.indexOf(curDir) > -1)
? path.substring(curDir.length()+1)
: path;

if ( (new File(relativePath)).isDirectory() ) {
// for a directory include all files
ne.setName(relativePath + "/**/*.*" );
} else {

// the actual directory is the source directory for the implicit fileset
fix.setSrcdir(new File(curDir));

// make the corrections

if (out!=null) try {out.close(); } catch (Exception e) {}

// Logging without any System.out
private static java.io.FileWriter out = null;
private static String br = System.getProperty("line.separator");
private static void log(String msg) {
if (debug) {
try {
if (out==null) out = new java.io.FileWriter("c:/temp/out.txt");
} catch (Exception e) {

About | Sitemap | Contact