Tuesday, April 22, 2014

Start-Up, Shutdown or Restart the Tomcat Server Using an Ant build.xml


Following Ant build script can be used to start, stop or restart your Tomcat server. First you should set the property "tomcat.home" to your Tomcat home directory. Then when you just execute the "ant" command, it will restart your running server.
Here are all the commands you can use with this script.

Start Up:    "ant tomcat-start"
Shutdown:  "ant tomcat-stop"
Restart:      "ant" or "ant tomcat-restart"

Depending on the web application you are running, you may be getting an Out of Memory PermGen space error. If so, remove the comment marks and enable jvm argument <jvmarg value="-XX:MaxPermSize=256m"/> in the script (line 16).


18 comments:

  1. Awesome post. It most helpful for me.

    ReplyDelete
  2. Thanks... Its exactly what i needed.

    ReplyDelete
  3. Hi,

    I am able to start and stop the tomcat server but my application is not getting started which is present in tomcat webapps.

    And in tomcat GUI i can see that false is getting displayed under Running.

    please help.

    ReplyDelete
    Replies
    1. Are you sure that the application doesn't have any problem? In other word, can you start the tomcat server and run the application in normal way?

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. hai bro, its working fine bro, i want to stop the tomcat services in parallel way.

    example : i have five tomcat instances i want to stop them and stop them in a way using ant parallel can we do this?

    ReplyDelete
  6. After a long R&D, i found your blog. It helped me a lot thank you.

    ReplyDelete
  7. Hi

    I am using the above code to start the tomcat through hudson job.
    The tomcat gets started , but the build process continues until i abort it using close button.

    also the tomcat stops after aborting the build.

    is it posssible to start the tomcat and keep it running and also the build job shows build successful after starting the tomcat ?

    Please help

    ReplyDelete
    Replies
    1. Yes, if you use the same script, the job will not stop. Because the tomcat sever keeps running in the same job thread. But it's possible what you want to do.
      In the above tutorial, i'm directly using bootstrap Jars to start tomcat. But You can you start scripts (startup.bat/startup.sh) files instead. Those scripts can be execute using Ant 'exec' task (https://ant.apache.org/manual/Tasks/exec.html).
      Then the Tomcat server will start in a different thread and Job will finish successfully.

      Delete
  8. Thanks for your masrvelous posting! I truly enjoyed reading it, you can be a great author.I will
    be sure to bookmark your blog and will often come back later
    on. I want to encourage you to continue your great writing, have a nice day!

    ReplyDelete
  9. Hi,
    I have done the same but, When i ran the script from Jenkins, the job is not getting success.

    [java] Apr 08, 2020 10:12:14 AM org.apache.coyote.AbstractProtocol start
    [java] INFO: Starting ProtocolHandler ["http-nio-9090"]
    [java] Apr 08, 2020 10:12:14 AM org.apache.catalina.startup.Catalina start
    [java] INFO: Server startup in [607] milliseconds
    it still in progress and not failed or success.

    ReplyDelete
    Replies
    1. It seems like the Jenkins job has been attached to the Tomcat process. According to the log messages you have posted, the Tomcat server is up and running as normal. So the Jenkins job will not be finished as long as the Tomcat is up.

      Delete