469,286 Members | 2,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

Running Ant using Java..

Hi,

I have written a java code to execute ant targets via Servlet. I have given the code below.

Expand|Select|Wrap|Line Numbers
  1. import org.apache.tools.ant.*;
  2. import org.apache.catalina.ant.*;
  3. .
  4. .
  5. .
  6. .
  7. public void antTest()
  8. {
  9.     BuildLogger logger = new DefaultLogger();
  10.     logger.setMessageOutputLevel(Project.MSG_INFO);
  11.     logger.setOutputPrintStream(System.out);
  12.     logger.setErrorPrintStream(System.out);
  13.     logger.setEmacsMode(true);
  14.  
  15.     ProjectHelper ph = ProjectHelper.getProjectHelper();
  16.     Project p = new Project();
  17.     p.addBuildListener(logger);
  18.     p.init();
  19.     p.addReference("ant.projectHelper", ph);
  20.     ph.parse(p, new File(sctx.getRealPath("/")+"bin/build.xml"));
  21.     p.executeTarget("display");
  22. }
The build.xml file is as follows.

Expand|Select|Wrap|Line Numbers
  1. <project name="Web Application" default="display" basedir=".">
  2.     <property file="${user.home}/build.properties"/>
  3.  
  4.     <property file="build.properties"/>
  5.  
  6.     <target name="init">
  7.         <tstamp/>
  8.     </target>
  9.  
  10.     <target name="display" description="Deletes the Web Application's war directory and war file">
  11.         <echo message="This line is displaying from ant target ..."/>
  12.     </target>
  13.  
  14. </project>
When the method antTest() is invoked, I got the following output.

Expand|Select|Wrap|Line Numbers
  1. This line is displaying from ant target ...
When I executed the build.xml using the command prompt (i.e. using the command "ant -f build.xml display"), I got the following output.

Expand|Select|Wrap|Line Numbers
  1. Buildfile: build.xml
  2. Trying to override old definition of datatype resources
  3.  
  4. display:
  5.      [echo] This line is displaying from ant target ...
  6.  
  7. BUILD SUCCESSFUL
  8. Total time: 0 seconds
I would like to have the following.

1) I expect the output from the java code should also display the same lines as in the output from command prompt. Please let me know the way to do so.

2) I want to log the output of the ant target to a log file when executing from java.

3) Is there any other way to run ant targets as we are running from the command prompt. That is if we have a string variable having the value "ant -f build.xml display", we could have to make this as the command to execute ant target. Is this possible? If so please let me know the way to implement.
Dec 27 '08 #1
6 13807
Is anyone having solution for this??
Jan 1 '09 #2
chaarmann
785 Expert 512MB
If you want the exact output from Ant as if running it from command line, then why don't you run it from command line through java then?

Here I wrote a function where you need to pass the command and working directory only and you get back the output from this command as a string result. Then you can save this result to a log file.

In your case, call it with
Expand|Select|Wrap|Line Numbers
  1.  
  2. executeShell("ant -f build.xml display", "c:\\myAntInstallationDirectory\\bin")
  3.  
Expand|Select|Wrap|Line Numbers
  1.  
  2.  /////////////////////////////////////////////////////////////////////////////////////////// 
  3.  public static final String executeShell(String command, String dir)
  4.  throws Exception
  5.  {
  6.   Runtime runtime = Runtime.getRuntime();
  7.   String output="";
  8.   try 
  9.   {
  10.    File fileDir = new File(dir);
  11.    Process process = runtime.exec(command, null, fileDir);
  12.    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
  13.  
  14.    // Read the output
  15.    String line = null;
  16.    while ((line = in.readLine()) != null)
  17.    {
  18.     output += line + "\n";
  19.    }
  20.   }
  21.   catch (Exception exception)
  22.   {
  23.    throw new Exception("[EXCEPTION in RuntimeUtils.executeShell()] : output of shell-command '" + command + "' = '" + output + "'. Exception=" + exception);
  24.   }
  25.  
  26.   return output;
  27.  }
  28.  
Jan 9 '09 #3
Hi chaarmann,

I've tried your solution but instead I got this error:

[EXCEPTION in RuntimeUtils.executeShell()] : output of shell-command 'ant -f build.xml display' = ''. Exception=java.io.IOException: Cannot run program "ant" (in directory "X:\build.xml"): CreateProcess error=2, The system cannot find the file specified

The file is there and it can be run through command line. Any idea?

Thanks
Jun 1 '10 #4
Try below

cmd = new String[] { "sh", "-c", "PATH=/ant binaries path","ant -buildfile","Your ant build file args..."}

p = r4.exec(cmd);
Sep 4 '12 #5
for logs use the Record option to document the Target out put
Sep 4 '12 #6
<target name="ftp_record" >
<echo message="Started Recording .."/>

<record name="C:\App\logs\${ftp.user}_ftp_log.txt" action="start" append="no"/>
</target>
Sep 4 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Peter Bassett | last post: by
reply views Thread by Baddn Raj | last post: by
reply views Thread by shanmukhi | last post: by
lifeisgreat20009
4 posts views Thread by lifeisgreat20009 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.