Can you call an Ant target from a JAVA class?

Discussion in 'Java' started by hust6, Jul 6, 2005.

  1. hust6

    hust6 Guest

    I need help in calling an Ant target from within a JAVA class.

    If anyone knows how to do this, I would appreciate the help.

    Thanks!

    hust6
    hust6, Jul 6, 2005
    #1
    1. Advertising

  2. hust6

    Roland Guest

    On 6-7-2005 17:58, hust6 wrote:

    > I need help in calling an Ant target from within a JAVA class.
    >
    > If anyone knows how to do this, I would appreciate the help.
    >
    > Thanks!
    >
    > hust6
    >


    Use the static method 'start' in org.apache.tools.ant.Main


    String[] target = {"compile"};
    Properties extraUserProps = ... ; // can be null
    ClassLoader loader = ... ; // can be null
    org.apache.tools.ant.Main.start(target, extraUserProps, loader);


    See Ant's API doc. This should be available in your Ant installation
    directory, e.g. apache-ant-1.6.5/docs/manual/api/index.html
    --
    Regards,

    Roland de Ruiter
    ` ___ ___
    `/__/ w_/ /__/
    / \ /_/ / \
    Roland, Jul 6, 2005
    #2
    1. Advertising

  3. hust6

    hust6 Guest

    I didn't mention that I am trying to run an ant task in a build.xml
    file. Does this change it? I can't figure out exactly how to
    implement the code you offered. Say I am trying to run an ant target
    from C:\test\build.xml, and the target is as follows:

    <target name="test1">
    <echo> ${arg1}</echo>
    </target>

    How would I make a call to this target (including specifying "arg1")?

    Again, thank you very much for the help, I appreciate it.

    hust6
    hust6, Jul 6, 2005
    #3
  4. hust6

    Roland Guest

    On 6-7-2005 22:14, hust6 wrote:

    > I didn't mention that I am trying to run an ant task in a build.xml
    > file. Does this change it? I can't figure out exactly how to
    > implement the code you offered. Say I am trying to run an ant target
    > from C:\test\build.xml, and the target is as follows:
    >
    > <target name="test1">
    > <echo> ${arg1}</echo>
    > </target>
    >
    > How would I make a call to this target (including specifying "arg1")?
    >
    > Again, thank you very much for the help, I appreciate it.
    >
    > hust6
    >


    Use the Properties parameter of the start method:

    String[] args = {"test1"};
    Properties userProps = new Properties();
    userProps.setProperty("arg1", "WhatEver");
    userProps.setProperty("arg2", "Something");
    ClassLoader loader = null; // or a specific classloader
    org.apache.tools.ant.Main.start(args, userProps, loader);


    And if you need to specify a specific build file, you can use the following:

    File buildFile = new File("path/to/some/build.xml");
    String[] args = {"-buildfile", buildFile.toString(), "test1"};

    I.e. the args passed to start method are like those you would pass on
    the commandline:
    ant -buildfile path/to/some/build.xml test1

    --
    Regards,

    Roland de Ruiter
    ` ___ ___
    `/__/ w_/ /__/
    / \ /_/ / \
    Roland, Jul 6, 2005
    #4
  5. hust6

    hust6 Guest

    Thanks for the help, Roland, it works perfectly!
    hust6, Jul 7, 2005
    #5
    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. Michael Kleen

    how to call a ant job from java class

    Michael Kleen, Jun 28, 2003, in forum: Java
    Replies:
    0
    Views:
    1,092
    Michael Kleen
    Jun 28, 2003
  2. Chris Bedford
    Replies:
    6
    Views:
    2,237
  3. Don
    Replies:
    0
    Views:
    4,346
  4. Thomas Stien
    Replies:
    0
    Views:
    467
    Thomas Stien
    Oct 18, 2004
  5. mohit
    Replies:
    0
    Views:
    553
    mohit
    Jan 23, 2008
Loading...

Share This Page