Search code examples
javajenkinstomcat

How to deploy war file to Tomcat from Jenkins without using plugin


I want to deploy from my Jenkins to Tomcat without using this "Deploy to container" plugin .

So I am thinking of a Tomcat REST API to deploy a war file. The Tomcat and the Jenkins are on different servers.

Maybe it is enough to just copy the file to the webapps folder ? But then I still have to deploy it meaning to start it . Right ?

Honestly I have not found any REST API to deploy to Tomcat so far. The problem is to make the .war file available .


Solution

  • From Jenkins, you can use a bash script to transfer the .war file to your Tomcat webapps directory (the code is the same if you work with a pipeline).

    First, stop the Tomcat

    ssh tomcat@tomcat '${TOMCAT_BIN_PATH}shutdown.sh'
    

    Then, copy the war to the webapps directory of your Tomcat

    scp -r -p jenkins@jenkins:path/to/war/file.war tomcat@tomcat:path/to/webapp/
    

    After that, you have to start the Tomcat and the .war will be deployed automatically

    ssh tomcat@tomcat '${TOMCAT_BIN_PATH}startup.sh'
    

    If you are using Maven, I suggest you to take a look at tomcat-maven-plugin, you can find it here. You can deploy the war file with the following command:

    mvn -B -P deploy-profile-from-pom-xml tomcat7:deploy-only
    

    An example of implementation can be found here


    UPDATE

    If you want Tomcat to delay the webapps directory checking, you can add backgroundProcessorDelay to your server.xml configuration

    <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true" backgroundProcessorDelay="15">
    

    Tomcat will only check for a new .war every 15 seconds with this code for instance.