Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 709119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:28:05+00:00 2026-05-14T04:28:05+00:00

I am working on an Ant target for running tests, and I need to

  • 0

I am working on an Ant target for running tests, and I need to have an app server up and running before the tests start. I’m using the waitFor task to ping a URL until it gets a response. This works, except that I need to not only make sure the URL is simply accessible, but also check that a particular status string is present. This second requirement is simple on its own, with a get or telnet task, followed by a substring search. But the ‘waitfor’ task doesn’t accept any nested elements other than standard Ant conditions, or things they can wrap (things that return booleans). I have tried loading the URL as a resource prior to running the ‘waitfor’ task with a resourceconatins condition, but that caches the response, and doesn’t refresh from the server.

Is there a way to force the URL to be loaded on each loop? Or some other way to achieve this?

Here’s what I want to achieve in pseudo-code:

<waitfor>
    <and>
        <!-- make sure the URL is accessible at all -->
        <http url="theurl.jsp"/>

        <!-- load the URL as a resource and make sure it contains a substring -->
        <!-- moving the "url" task before the waitfor works if all conditions are met the first time through, but does not refresh the URL if it has to loop over again. having it inside like this, fails because waitfor/and don't support nexted "url" tasks -->
        <url id="url.resource" url="url.jsp"/>
        <resourcecontains refid="url.resource" substring="status"/>
    </and>
    <!-- else, keep on trying until timeout -->
</waitfor>
<!-- continue on to the tests to be run -->
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:28:06+00:00Added an answer on May 14, 2026 at 4:28 am

    Usage:

       <typedef
          name="httpcontains"
          classname="HttpContains"
       />
    
      <waitfor maxwait="10" maxwaitunit="minute" checkevery="500">
        <httpcontains contains="Running" url="http://myurl"/>
      </waitfor>
    

    Code below allows for this:

    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import org.apache.tools.ant.BuildException;
    import org.apache.tools.ant.Project;
    import org.apache.tools.ant.ProjectComponent;
    import org.apache.tools.ant.taskdefs.condition.Condition;
    
    /**
     * Condition to wait for a HTTP request to succeed. Its attribute(s) are:
     *   url - the URL of the request.
     *   errorsBeginAt - number at which errors begin at; default=400.
     * @since Ant 1.5
     */
    public class HttpContains extends ProjectComponent implements Condition {
        private String spec = null;
        private String contains = null;
        /**
         * Set the url attribute
         *
         * @param url the url of the request
         */
        public void setUrl(String url) {
            spec = url;
        }
    
        public void setContains(String s){
           contains = s;
        }
    
        private int errorsBeginAt = 400;
    
        /**
         * Set the errorsBeginAt attribute
         *
         * @param errorsBeginAt number at which errors begin at, default is
         *                      400
         */
        public void setErrorsBeginAt(int errorsBeginAt) {
            this.errorsBeginAt = errorsBeginAt;
        }
    
        /**
         * @return true if the HTTP request succeeds
         * @exception BuildException if an error occurs
         */
        public boolean eval() throws BuildException {
            if (spec == null) {
                throw new BuildException("No url specified in http condition");
            }
            log("Checking for " + spec, Project.MSG_VERBOSE);
            try {
                URL url = new URL(spec);
                try {
                    URLConnection conn = url.openConnection();
                    if (conn instanceof HttpURLConnection) {
                        HttpURLConnection http = (HttpURLConnection) conn;
                        int code = http.getResponseCode();
                        log("Result code for " + spec + " was " + code,
                            Project.MSG_VERBOSE);
                        if (code > 0 && code < errorsBeginAt) 
                        {
                           if ( evalContents(url) )
                              return true;
                           else
                              return false;
    
                        } else {
                            return false;
                        }
                    }
                } catch (java.io.IOException e) {
                    return false;
                }
            } catch (MalformedURLException e) {
                throw new BuildException("Badly formed URL: " + spec, e);
            }
            return true;
        }
    
        public boolean evalContents(URL mUrl )
        {
           try
           {
    
              String contents;
    
              StringBuffer buffer = new StringBuffer();
              byte[] buf = new byte[1024];
              int amount = 0;
              InputStream input = mUrl.openStream();
    
              while ((amount = input.read(buf)) > 0) 
              {   
                 buffer.append(new String(buf, 0, amount));
              }
              input.close();
              contents = buffer.toString();
    
              log("Result code for " + contents,
                    Project.MSG_VERBOSE);
    
              if ( contents.indexOf(contains) > -1 )
              {
    
                 log("Result code for " + contents.indexOf(contains),
                       Project.MSG_VERBOSE);
                 return true;
    
              }
    
    
    
    
    
    
              return false;
           }
           catch ( Exception e )
           {
              e.printStackTrace();
              log("Result code for " + e.toString(),
                    Project.MSG_VERBOSE);
    
              return false;
           }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ant task set up like so: <target name=unit-test description=unit tests depends=compile-tests>
I've been working with JAX-WS with Weblogic Server, using their Ant tasks to build
working on an ANT build file, where I have two macros defined and need
We have tried using ant ftp tasks, but could not get it working like
I am getting below error when running a target of ANT script. Error message
I am converting ant to maven2. In build.xml, I have: <target name=clean> <delete file=${dir.dist}/${api.jarfile}
I am working with the new cfbuilder and using ANT to push my code
I can't get either the FTP nor the MySQL Task working for Ant 1.8.2
I'm working on a project that used ant . I had a target dist
I am working on a grails app and need to remove Tomcat plugin in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.