0

I have an application that runs on a Tomcat 7 server on a Windows machine. In its current stage, I have to frequently update and fix it. Whenever I need to update the application, I do all this:

  1. Build a new war file (using ant) on my Linux development computer;
  2. Go to the Windows server, stop the Tomcat service;
  3. download the file from my computer to the Windows computer, put it under webapps;
  4. Remove the old application folder under webapps;
  5. Remove the old application folder under work/Catalina/localhost (otherwise it keeps the old version cached).
  6. Restart the Tomcat service.

I am sure there is a way to do all this automatically. What is it?

Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
  • 1
    See this ANT build.xml http://stackoverflow.com/a/18460429/185565 answer. It shows how you can use webapp directly from the projects folder at development time. So much easier to see .jsp,.js,.css changes immediately. Later build war to be deployed somewhere. Deployment may use ant filecopy or ssh/ftp to copy .war to tomcat/webapps/ folder. – Whome Sep 08 '13 at 22:13

3 Answers3

2

Summary: I'd invest some time in setting up a build and deployment scheme. It's a bit of work up front, but a general solution that can grow as needed after it's set up.

You can accomplish step 1 with an Ant script. You'd first install Ant, and write a short build.xml to include something like this example. With Ant properly installed, you just change to the directory where build.xml is located and run 'ant'.

For steps 2 thru 6, since you need to clean up the target, manage services, etc., I'd look at generating MSI installers or executables. My first choice for an MSI tool is Advanced Installer. You'll see under the list of features that the freeware version allows you to control (start, stop, install, uninstall) Windows services on install and uninstall.

(Side note: we've used Advanced Installer Enterprise for four years. It's continuously improved, and an exceptionally high quality product. You won't be disappointed.)

You can control the MSI creation thru Ant as well. Here's a snip from my build.xml that invokes a couple macros to compile and deploy one of the products I maintain:

<target name="myproduct-installer" depends="unzip-myproductdocs">
    <build-ai-installer product.name="MyProduct" installer.path="setup/installs/MyProduct" project.file="MyProduct.aip" />
</target>
<target name="release-myproduct-installer">
    <release-AI-installer product.name="MyProduct" installer.path="setup/installs/MyProduct" product.path="${some-predefined-target}" />
</target>

Here are the macros used above:

<macrodef name="build-ai-installer">
    <attribute name="product.name" />
    <attribute name="installer.path" />
    <attribute name="project.file" />
    <sequential>
        <echo message="Making installer at @{installer.path}" />
        <mkdir dir="@{installer.path}/newInstall" />
        <exec dir="@{installer.path}" executable="${env.ADVANCEDINSTALLER}" failonerror="true">
            <arg line="/edit @{project.file} /SetVersion ${product.version}" />
        </exec>
        <exec dir="@{installer.path}" executable="${env.ADVANCEDINSTALLER}" failonerror="true">
            <arg line="/build @{project.file}" />
        </exec>
    </sequential>
</macrodef>

<macrodef name="release-AI-installer">
    <attribute name="product.name" />
    <attribute name="installer.path" />
    <attribute name="product.path" />
    <sequential>
        <copy todir="@{product.path}">
            <fileset dir="@{installer.path}/newInstall" />
        </copy>
    </sequential>
</macrodef>

These macros use a Windows environment variable called env.ADVANCEDINSTALLER. Simpler build setups will just set the Ant property and drop the 'env.' prefix:

<property name="ADVANCEDINSTALLER" value="path-to-AdvancedInstaller.com" />

This level of automation pays dividends as soon as it's up and running. But if it's more than you need (I wouldn't be surprised), this answer may help.

Community
  • 1
  • 1
Tim Condit
  • 138
  • 4
  • Advanced Installer looks like a fine product. The problem is, my development computer is Linux, and AI does not run on Linux (probably even with Wine: http://www.advancedinstaller.com/forums/viewtopic.php?f=1&t=9124 ) – Erel Segal-Halevi Mar 20 '12 at 07:10
2

With kwatee agile deployment (I'm the developer) you can configure an automated deployment of webapps to tomcat in just a jew minutes (see tutorial). You then trigger the deployment either on-demand via a simple web interface or automatically using the kwatee ant task or the kwatee maven plugin. Oh, it's free too.

mac
  • 5,627
  • 1
  • 18
  • 21
  • That's an excellent software, thank you! However, I didn't find a way to implement my exact use case: * The Windows server is offline - I have no ftp/telnet/ssh access. * I manually download the war to the Windows server. * The war file always has the same name, so a server-side script could potentially deploy it automatically, if only I knew how to build such scripts. – Erel Segal-Halevi Mar 20 '12 at 07:01
  • You can generate CLI off-line installers with kwatee. Once you copy the installer to your windows box, just launch it and it will do all necessary operations. You just need to modify the configuration of the tutorial to replace curl by curl.exe which you can download from curl.haxx.se. – mac Mar 22 '12 at 11:04
1

There are ways to do it.

One way is to use a continuous integration system, for example Jenkins. It would let you build the war regularly or after each commit to a source repository.

Then there is the Jenkins Deploy Plugin that can be used to deploy a war to a running Tomcat instance after it is built. It uses Tomcat's standard manager webapp, so this must be installed in Tomcat, with an user and password set up.

Note that frequent redeployments to a running Tomcat are unfortunately an easy way to find out that your webapp leaks memory on undeployment. OutOfMemory and MemoryLeakProtection articles on the Tomcat wiki are useful reads. Automated Tomcat restarts may be an useful stopgap measure.

Michał Politowski
  • 4,288
  • 3
  • 30
  • 41
  • OK, I installed Jenkins and the deploy plugin, created an empty build job with the "deploy" checkbox checked, ran the job, and nothing happened - the console output says "SUCCESS" but the new war is not deployed... – Erel Segal-Halevi Mar 25 '12 at 13:10
  • @Erel What do you mean by empty build? If the job does not build a war, how did you tell it where to take it from for deployment? Did you fill in all the settings for the deploy action (including tomcat address/username/password)? Doesn't the console say anything else? – Michał Politowski Apr 01 '12 at 22:07
  • I finally understood that I must have some build steps in order for the plugin to work. However, there is another problem - it seems that the deployment plugin just drops the war into the webapps folder, without stopping the Tomcat service. This causes just the same problem like when I do the same: http://stackoverflow.com/questions/9896102/tomcat-webapp-folder-is-ruined-when-putting-a-new-war-file/9903722#9903722 – Erel Segal-Halevi Apr 03 '12 at 05:47