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":
- Find out where tomcat is installed. You can do this in bash:
catalina version - 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.
- 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.
- 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.
- 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. - 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.
- 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>
- Navigate to your project root, where the pom.xml is, and run the following command:
mvn tomcat7:run