AspiringGeek
AspiringGeek

Reputation: 95

How can I deploy a web application to tomcat application server via Terminal command

I am currently testing a large number of webservices. I would like to deploy and undeploy to tomcat application server via terminal command as fast as I can. Using the HTML GUI would not be reasonable for the large number of webservices that I need to deploy. Can anyone assist me, in how to deploy via a terminal command?

Furthermore, I am writing a ash script that automates the deployment process, so perhaps if someone can give me some some direction it would be great.

Ideally, I am looking to do something like this on the command line:

TOMCAT --parameter Specify path to WAR file --parameter2 --specify some sort of config file

Upvotes: 2

Views: 7006

Answers (3)

cosbor11
cosbor11

Reputation: 16024

First you need to make sure that tomcat-user.xml is configured with the correct users and roles. The minimum role configuration is "admin,manager-script":

  1. Find out where tomcat is installed. You can do this in bash: catalina version
  2. Navigate to the CATALINA_HOME config directory which is displayed in the output from the previous command.
cd /usr/local/Cellar/tomcat/8.0.22/libexec/conf
  • Note: In my example, I used homebrew to install tomcat 8, replace this path with whatever is displayed in your command line output.
  1. Verify that the server.xml contains a UserDatabase resource (if it doesn't add it):
  <GlobalNamingResources>
      <Resource name="UserDatabase" auth="Container"
          type="org.apache.catalina.UserDatabase"
          description="User database that can be updated and saved"
          factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources> 

By default tomcat uses an in-memory database to store users and roles. This is configured in the conf/server.xml. And delegates user & role declaration to the conf/tomcat-users.xml file. See: http://tomcat.apache.org/tomcat-8.0-doc/realm-howto.html#UserDatabaseRealm for more information.

  1. Then verify that config/user-tomcat.xml exists and looks like this:
  <tomcat-users xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd">
     <role rolename="admin"/>
     <role rolename="manager-script"/>
     <user username="admin" roles="admin,manager-script" password="admin" />
  </tomcat-users>

Now, you're ready to deploy a war!

Here are two ways to do that...

Using wget:

wget is a nifty tool that lets you do http requests via the command line. I recommend installing it using a package manager like homebrew, otherwise, you can install using this wget install guide.

  1. First restart your server to pick up any configuration changes that you may have done. catalina stop, then catalina start, will do the trick from bash.
    *Note: When using wget to start deploy a war to tomcat, tomcat needs to be started first.
  2. Now execute the following command:
wget --http-user=admin --http-password=admin  "http://localhost:8080/manager/text/deploy?war=file:/Users/yourusername/app/target/APP-1.0.0.war&path=/app"

Notes:

  • Wait a few seconds to let the http request to send and fully deploy the war. (sometimes this takes a while).

  • You may be tempted to reference the war file using the home directory shortcut like this file:~/app/target/APP-1.0.0.war, but that won't work.

  • To undeploy the war simply replace deploy with undeploy in the wget command.

Using the Tomcat Maven Plugin:

If you have the source code, you can build and deploy the war yourself very easily with the tomcat7-maven-plugin. Note: At the time I wrote this there was no tomcat8-maven-plugin; the tomcat-7-maven-plugin works just fine for tomcat 8.

  1. Add the plugin to the pom.xml:
<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>${version.tomcat.maven.plugin}</version>
  <configuration>
    <path>/${project.build.finalName}</path>
    <configurationDir>${env.CATALINA_HOME}</configurationDir>
    <additionalConfigFilesDir>${env.CATALINA_HOME}/conf</additionalConfigFilesDir>
    <uriEncoding>${project.build.sourceEncoding}</uriEncoding>
  </configuration>
</plugin>
  1. Navigate to your project root, where the pom.xml is, and run the following command: mvn tomcat7:run

Upvotes: 3

anubhava
anubhava

Reputation: 785128

To deploy a WAR from the command line you can use wget (or curl)

wget "http://localhost:8080/manager/text/deploy?war=file:/path/to/MyWar.war&path=/MyApp" -O -

Upvotes: 2

Christopher Schultz
Christopher Schultz

Reputation: 20862

Take a look at Tomcat's manager webapp. You can use the "text" interface to do things from the command-line. Tomcat even comes with some Apache Ant tasks that can deploy, undeploy, etc. for you.

Upvotes: 1

Related Questions