2

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

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

2

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 - 

3 Comments

Thanks for that. I forgot to mention that I am running Tomcat via the executable jar. I found instructions here: tomcat.apache.org/maven-plugin-trunk/executable-war-jar.html. The next thing I'm wondering about is how would I create parameters for my specific web service. In my case, I am deploying the webservice to a database and I would like the user to specify credentials when running the webservice. How would I set up those parameters? I'm thinking somewhere along the lines of: java -jar tomcat.war.jar --xmlconfigFileforMyapp /path/to/config/ --user foo --password random
deploying the webservice to a database sorry I don't get that.
Note that this solution only works if you have your WAR file on the Tomcat server already. There are ways to upload the WAR file at the point of deployment, which is quite convenient.
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.