# WARファイルをTomcatにデプロイする方法

## 1。概要

Apache Tomcatは、Javaコミュニティで最も人気のあるWebサーバーの1つです。これは、WAR拡張機能を備えたWebアーカイブを提供できるサーブレットコンテナとして出荷されます

コンテナを再起動せずに、新しいWebアプリケーションをデプロイしたり、既存のWebアプリケーションをアンデプロイしたりできる管理ダッシュボード提供します。これは、実稼働環境で特に役立ちます。

この記事では、Tomcatの概要を簡単に説明してから、WARファイルをデプロイするためのさまざまなアプローチについて説明します。

## 2.Tomcatの構造

### 2.1。環境変数

$CATALINA_HOME この変数は、サーバーがインストールされているディレクトリを指します。$ CATALINA_BASE

この変数は、Tomcatの特定のインスタンスのディレクトリを指します。複数のインスタンスがインストールされている場合があります。この変数が明示的に設定されていない場合は、$CATALINA_HOMEと同じ値が割り当てられます。 Webアプリケーションは、$ CATALINA_HOME \ webappsディレクトリにデプロイされます。

### 2.2。用語

ドキュメントルート。Webアプリケーションの最上位ディレクトリを指します。ここには、JSPファイル、HTMLページ、Javaクラス、画像など、すべてのアプリケーションリソースがあります。

コンテキストパス。サーバーのアドレスを基準にして、Webアプリケーションの名前を表す場所を参照します。

たとえば、Webアプリケーションが$CATALINA_HOME \ webapps \ myappディレクトリに配置されている場合、URL // localhost / myappによってアクセスされ、そのコンテキストパスは/ myappになります。 WAR。Webアプリケーションのディレクトリ階層をZIP形式でパッケージ化したファイルの拡張子であり、WebArchiveの略です。Java Webアプリケーションは通常、デプロイメント用のWARファイルとしてパッケージ化されています。これらのファイルは、コマンドラインまたはEclipseなどのIDEを使用して作成できます。 WARファイルをデプロイした後、Tomcatはそれを解凍し、すべてのプロジェクトファイルをプロジェクトにちなんで名付けられた新しいディレクトリのwebappsディレクトリに保存します。 ## 3.Tomcatのセットアップ Tomcat Apache Webサーバーは、Webサイトからダウンロードできるフリーソフトウェアです。ユーザーのマシンで使用可能なJDKがあり、JAVA_HOME環境変数が正しく設定されている必要があります ### 3.1。Tomcatを起動します$ CATALINA_HOME \ bin \ startupにある起動スクリプトを実行するだけで、Tomcatサーバーを起動できます。そこでの.bat.shをすべてのインストールでは。

WindowsベースまたはUnixベースのオペレーティングシステムを使用しているかどうかに応じて、適切なオプションを選択してください。

### 3.2。役割の構成

ダッシュボードにアクセスするには、管理者ユーザーにmanager-guiロールが必要です。後で、Mavenを使用してWARファイルをデプロイする必要があります。このためには、manager-scriptロールも必要です。

$CATALINA_HOME \ conf \ tomcat-usersでこれらの変更を行いましょう： さまざまなTomcatの役割の詳細については、この公式リンクをたどってください。 ### 3.3。ディレクトリのアクセス許可を設定する 最後に、Tomcatインストールディレクトリに読み取り/書き込み権限があることを確認します。 ### 3.4。テストインストール Tomcatが正しくセットアップされていることをテストするには、起動スクリプト（startup.bat / startup.sh）を実行します。コンソールにエラーが表示されない場合は、// localhost：8080にアクセスして再確認できます。 Tomcatのランディングページが表示されている場合は、サーバーが正しくインストールされています。 ### 3.5。ポートの競合を解決する デフォルトでは、Tomcatはポート8080で接続をリッスンするように設定されています。このポートにすでにバインドされている別のアプリケーションがある場合は、スタートアップコンソールから通知されます。 ポートを変更するには、$ CATALINA_HOME \ conf \ server.xmlにあるサーバー構成ファイルserver.xmlを編集しますデフォルトでは、コネクタの構成は次のとおりです。

たとえば、ポートを8081に変更する場合は、次のようにコネクタのポート属性を変更する必要があります。

Sometimes, the port we have chosen is not open by default, in this case, we will need to open this port with the appropriate commands in the Unix kernel or creating the appropriate firewall rules in Windows, how this is done is beyond the scope of this article.

## 4. Deploy From Maven

If we want to use Maven for deploying our web archives, we must configure Tomcat as a server in Maven's settings.xml file.

There are two locations where the settings.xml file may be found:

• The Maven install: ${maven.home}/conf/settings.xml • A user’s install:${user.home}/.m2/settings.xml

Once you have found it add Tomcat as follows:

 TomcatServer admin password 

We will now need to create a basic web application from Maven to test the deployment. Let's navigate to where we would like to create the application.

Run this command on the console to create a new Java web application:

mvn archetype:generate -DgroupId=com.baeldung -DartifactId=tomcat-war-deployment -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

This will create a complete web application in the directory tomcat-war-deployment which, if we deploy now and access via the browser, prints hello world!.

But before we do that we need to make one change to enable Maven deployment. So head over to the pom.xml and add this plugin:

 org.apache.tomcat.maven tomcat7-maven-plugin 2.2  //localhost:8080/manager/text TomcatServer /myapp  

Note that we are using the Tomcat 7 plugin because it works for both versions 7 and 8 without any special changes.

The configuration url is the url to which we are sending our deployment, Tomcat will know what to do with it. The server element is the name of the server instance that Maven recognizes. Finally, the path element defines the context path of our deployment.

This means that if our deployment succeeds, we will access the web application by hitting //localhost:8080/myapp.

Now we can run the following commands from Maven.

To deploy the web app:

mvn tomcat7:deploy

To undeploy it:

mvn tomcat7:undeploy

To redeploy after making changes:

mvn tomcat7:redeploy

## 5. Deploy With Cargo Plugin

Cargo is a versatile library that allows us to manipulate the various type of application containers in a standard way.

### 5.1. Cargo Deployment Setup

In this section, we will look at how to use Cargo's Maven plugin to deploy a WAR to Tomcat, in this case, we will deploy it to a version 7 instance.

To get a firm grip on the whole process, we will start from scratch by creating a new Java web application from the command line:

mvn archetype:generate -DgroupId=com.baeldung -DartifactId=cargo-deploy -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

This will create a complete Java web application in the cargo-deploy directory. If we build, deploy and load this application as is, it will print Hello World! in the browser.

Unlike the Tomcat7 Maven plugin, the Cargo Maven plugin requires that this file is present.

Since our web application does not contain any servlets, our web.xml file will be very basic. So navigate to the WEB-INF folder of our newly created project and create a web.xml file with the following content:

  cargo-deploy  index.jsp  

To enable Maven to recognize Cargo's commands without typing the fully qualified name, we need to add the Cargo Maven plugin to a plugin group in Maven's settings.xml.

As an immediate child of the root element, add this:

 org.codehaus.cargo 

### 5.2. Local Deploy

In this subsection, we will edit our pom.xml to suit our new deployment requirements.

Add the plugin as follows:

   org.codehaus.cargo cargo-maven2-plugin 1.5.0   tomcat7x installed Insert absolute path to tomcat 7 installation   existing Insert absolute path to tomcat 7 installation      

The latest version, at the time of writing, is 1.5.0. However, the latest version can always be found here.

Notice that we explicitly define the packaging as a WAR, without this, our build will fail. In the plugins section, we then add the cargo maven2 plugin. Additionally, we add a configuration section where we tell Maven that we are using a Tomcat container and also an existing installation.

By setting the container type to installed, we tell Maven that we have an instance installed on the machine and we provide the absolute URL to this installation.

By setting the configuration type to existing, we tell Tomcat that we have an existing setup that we are using and no further configuration is required.

The alternative would be to tell cargo to download and setup the version specified by providing a URL. However, our focus is on WAR deployment.

It's worth noting that whether we are using Maven 2.x or Maven 3.x, the cargo maven2 plugin works for both.

We can now install our application by executing:

mvn install

and deploying it by doing:

mvn cargo:deploy

If all goes well we should be able to run our web application by loading //localhost:8080/cargo-deploy.

### 5.3. Remote Deploy

To do a remote deploy, we only need to change the configuration section of our pom.xml. Remote deploy means that we do not have a local installation of Tomcat but have access to the manager dashboard on a remote server.

So let's change the pom.xml so that the configuration section looks like this:

  tomcat8x remote   runtime  admin admin //localhost:8080/manager/text    

This time, we change the container type from installed to remote and the configuration type from existing to runtime. Finally, we add authentication and remote URL properties to the configuration.

Ensure that the roles and users are already present in $CATALINA_HOME/conf/tomcat-users.xml just as before. If you are editing the same project for remote deployment, first un-deploy the existing WAR: mvn cargo:undeploy clean the project: mvn clean install it: mvn install finally, deploy it: mvn cargo:deploy That's it. ## 6. Deploy From Eclipse Eclipse allows us to embed servers to add web project deployment in the normal workflow without navigating away from the IDE. ### 6.1. Embed Tomcat in Eclipse We can embed an installation into eclipse by selecting the window menu item from taskbar and then preferences from the drop down. We will find a tree grid of preference items on the left panel of the window that appears. We can then navigate to eclipse -> servers or just type servers in the search bar. We then select the installation directory, if not already open for us, and choose the Tomcat version we downloaded. On the right-hand-side of the panel, a configuration page will appear where we select the Enable option to activate this server version and browse to the installation folder. We apply changes, and the next time we open the servers view from Eclipse's windows -> show view submenu, the newly configured server will be present and we can start, stop and deploy applications to it. ### 6.2. Deploy Web Application in Embedded Tomcat To deploy a web application to Tomcat, it must exist in our workspace. Open the servers view from window -> show view and look for servers. When open, we can just right click on the server we configured and select add deployment from the context menu that appears. From the New Deployment dialog box that appears, open the project drop down and select the web project. There is a Deploy Type section beneath the Project combo box when we select Exploded Archive(development mode), our changes in the application will be synced live without having to redeploy, this is the best option during development as it is very efficient. Selecting Packaged Archive(production mode) will require us to redeploy every time we make changes and see them in the browser. This is best only for production, but still, Eclipse makes it equally easy. ### 6.3. Deploy Web Application in External Location We usually choose to deploy a WAR through Eclipse to make debugging easier. There may come a time when we want it deployed to a location other than those used by Eclipse's embedded servers. The most common instance is where our production server is online, and we want to update the web application. We can bypass this procedure by deploying in production mode and noting the Deploy Location in the New Deployment dialog box and picking the WAR from there. During deployment, instead of selecting an embedded server, we can select the option from the servers view alongside the list of embedded servers. We navigate to the webapps directory of an external Tomcat installation. ## 7. Deploy From IntelliJ IDEA To deploy a web application to Tomcat, it must exist and have already been downloaded and installed. ### 7.1. Local Configuration Open the Run menu and click the Edit Configurations options. In the panel on the left search for Tomcat Server, if it is not there click the + sign in the menu, search for Tomcat and select Local. In the name field put Tomcat 7/8 (depending on your version). Click the Configure… button and in Tomcat Home field navigate to the home location of your installation and select it. Optionally, set the Startup page to be //localhost:8080/ and HTTP port: 8080, change the port as appropriate. Go to the Deployment tab and click on the + symbol, select artifact you want to add to the server and click OK ### 7.2. Remote Configuration Follow the same instructions as for local Tomcat configurations, but in the server tab, you must enter the remote location of the installation. ## 8. Deploy by Copying Archive We have seen how to export a WAR from Eclipse. One of the things we can do is to deploy it by simply dropping it into the$CATALINA_HOME\webapps directory of any Tomcat instance. If the instance is running, the deployment will start instantly as Tomcat unpacks the archive and configures its context path.

If the instance is not running, then the server will deploy the project the next time it is started.

## 9. Deploy From Tomcat Manager

Assuming we already have our WAR file to hand and would like to deploy it using the management dashboard. You can access the manager dashboard by visiting: //localhost:8080/manager.

The dashboard has five different sections: Manager, Applications, Deploy, Diagnostics, and Server Information. If you go to the Deploy section, you will find two subsections.

### 9.1. Deploy Directory or WAR File Located on Server

If the WAR file is located on the server where the Tomcat instance is running, then we can fill the required Context Path field preceded by a forward slash “/”.

Let's say we would like our web application to be accessed from the browser with the URL //localhost:8080/myapp, then our context path field will have /myapp.

We skip the XML Configuration file URL field and head over to the WAR or Directory URL field. Here we enter the absolute URL to the Web ARchive file as it appears on our server. Let's say our file's location is C:/apps/myapp.war, then we enter this location. Don't forget the WAR extension.

After that, we can click deploy button. The page will reload, and we should see the message:

OK - Deployed application at context path /myapp

at the top of the page.

Additionally, our application should also appear in the Applications section of the page.

### 9.2. WAR File to Deploy

Just click the choose file button, navigate to the location of the WAR file and select it, then click the deploy button.

どちらの状況でも、すべてがうまくいけば、Tomcatコンソールは展開が成功したことを次のようなメッセージで通知します。

INFO: Deployment of web application archive \path\to\deployed_war has finished in 4,833 ms

## 10.結論

この記事では、TomcatサーバーへのWARのデプロイに焦点を当てました。