dcsimg
Calling ANT source to untar a file.
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Alan_S
Posted On:   Thursday, November 30, 2006 09:26 PM

I'm considering using the ANT source as a base for a project to deploy applications. So here is a first attempt at using org.apache.tools.ant.taskdefs.Untar to decompress a tar-gz file. It seems to call the execute method without any errors, but it doesn't actually unzip the file. In the ANT source there are calls to a log() method which I think would help debug why. Is there a way to redirect this log to stdout? Example code below. /** * decompress files from the *.tar.gz format. * @param tarFile * @param destDir */ static public void untar(String tarFile, String destDir){ try{ Project p = new Project(); p.init();    More>>

I'm considering using the ANT source as a base for a project to deploy applications. So here is a first attempt at using org.apache.tools.ant.taskdefs.Untar to decompress a tar-gz file.


It seems to call the execute method without any errors, but it doesn't actually unzip the file. In the ANT source there are calls to a log() method which I think would help debug why. Is there a way to redirect this log to stdout?


Example code below.


			
/**
* decompress files from the *.tar.gz format.
* @param tarFile
* @param destDir
*/
static public void untar(String tarFile, String destDir){

try{
Project p = new Project();
p.init();
//ToDo: how do we get the logged output from Project?

Untar untar = new Untar();
untar.setProject( p );

untar.setTaskName("AntUtils.untar");
untar.setTaskType("untar");

//double check the files are correct.
File tf = new File(tarFile);
File dd = new File(destDir);
System.out.println(tf.getAbsolutePath());
System.out.println(dd.getAbsolutePath());
if( !tf.exists() ){
System.out.println( "This paths does not exist: "+tf.getAbsolutePath() );
}//if

untar.setSrc(new File(tarFile));
untar.setDest(new File(destDir));
Untar.UntarCompressionMethod mode = new Untar.UntarCompressionMethod();
mode.setValue("gzip");
untar.setCompression( mode );
untar.execute();
}catch(BuildException be){
System.out.println("Error had: "+be);
}
}//untar
   <<Less

Re: Calling ANT source to untar a file.

Posted By:   Robert_Lybarger  
Posted On:   Friday, December 1, 2006 09:24 AM

Unrelated, you might also try calling untar task code's perform() method instead of execute() method. The reason for needing to do this sometimes isn't clearly spelled out anywhere. (Then again, few of ants peculiarities are either.)

Re: Calling ANT source to untar a file.

Posted By:   Robert_Lybarger  
Posted On:   Friday, December 1, 2006 09:22 AM

Look in the Ant API docs for better help, but you should be able to setup a "DefaultLogger" on the standard output stream and connect this logger to your dummy Project. I can't promise that's all it takes to work, but that's the direction to start looking. Alternatively, if you look at Ant's source code for the "Ant.java" (ant task code) itself, they are doing this for a sub-ant build ... annoyingly they hard-wire sub-builds to DefaultLogger (instead of whatever was on your command line) which I had to override in our in-house copy of Ant.

Re: Calling ANT source to untar a file.

Posted By:   Anonymous  
Posted On:   Friday, December 1, 2006 09:20 AM

In your case a look into Ant's sources would be very useful.


The log() method delegates the message to Project.log() method if there is a project reference. So you have to provide a Project implementation. Subclass the existing project and overwrite the log-methods.


Also have a look at Leafcutter

About | Sitemap | Contact