2

I want to use one of Ant's task (scp) from inside my code. Is there any way to do it?

Should I simply reference one of Ant's library and call the API directly from my code?

jahroy
  • 22,322
  • 9
  • 59
  • 108
bcbishop
  • 2,193
  • 3
  • 20
  • 23

4 Answers4

5

Yes, you can invoke Ant tasks quite easily from your code.

Here's an example of how to extend an Ant Task:

import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.taskdefs.Copy;

public class MyCopyTask extends Copy {
    public MyCopyTask() {
        setProject(new Project());
        getProject().init();
        setTaskName("MyCopy");
        setTaskType("MyCopy");
        setOwningTarget(new Target());
    }
}

Here's how to use it in your code:

MyCopyTask copier = new MyCopyTask();
copier.setFile(srcFile);
copier.setTofile(toFile);
copier.execute();

Here's some more info:

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • what makes this better than a simple system call? – amphibient Dec 12 '12 at 04:01
  • 1
    @foampile - To quote the link: it's cross-platform, robust, and benefits from community support. Also, just like with any library, the Ant Tasks include tons of options that you can use in stead of implementing them on your own. For example, with the copy task you can easily specify whether or not to overwrite an existing file by simply setting a boolean field. Further, there are lots of things that can go wrong when you make a "_simple_" system call. I'd rather let code from Apache deal with some of those errors. I shutter to think of all the things that can go wrong when executing scp!! – jahroy Dec 12 '12 at 04:34
  • Here is a link to the source code for the Copy Task... As you can see, there are _TONS_ of options that you can take advantage of (I would hate to implement all this myself): http://www.docjar.com/html/api/org/apache/tools/ant/taskdefs/Copy.java.html – jahroy Dec 12 '12 at 04:40
  • After looking at your answer, it looks like you're suggesting using a simple system call to invoke an Ant task. So... To be fair, that does take advantage of the things I've mentioned. I still think it's more versatile to create utility routines by extending Ant Tasks than it is to rely on having Ant Tasks pre-defined somewhere that you invoke with a system call. Maybe I'm misunderstanding your answer... – jahroy Dec 12 '12 at 04:44
  • i like the way you do it, it is cleaner albeit longer. it all depends on the application and how much of its core functionality it is. if he is just trying to quickly automate a task that he doesn't anticipate will need to run on another OS, then my approach is reasonable. if he is implementing, say a web console where users will have an interface to ant jobs, i.e. ant call is core app functionality, then maybe use what you did. – amphibient Dec 12 '12 at 15:26
0

This would certainly work to include the Ant task inside your Java code. We have done this before as a last resort. One gotcha is you will likely be setting some Ant specific properties (e.g. setProject) to get the task to actually work. This can make it a bit awkward to use. In addition, you will likely need, as you mention, the Ant runtime libraries to get this to run.

My first recommendation would be to see if there is a way to call the actual SCP code you want directly without the Ant overhead. The second alternative would be to look for a Java SCP library or find out what the Ant scp task is using. See scp via java for some more examples. The final recommendation would be to include the Ant task as described above.

Community
  • 1
  • 1
tjg184
  • 4,508
  • 1
  • 27
  • 54
0

I think invoking a system call would be simpler:

try {
            Runtime.getRuntime().exec("ant scpTask");
        } catch (IOException e) {
            e.printStackTrace();
        }
amphibient
  • 29,770
  • 54
  • 146
  • 240
0

I think Runtime.getRuntime().exec("ant deploy"); depends on your OS, if you just run it in one fixed OS, it is really a simpler and easy way to get what you want. :)

feikiss
  • 344
  • 4
  • 14