Deploying Web Applications in Jetty – 在Jetty中部署Web应用

最后修改: 2016年 9月 22日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we will do a quick overview of the Jetty web server and then cover various approaches to deploying a WAR file.

在这篇文章中,我们将对Jetty网络服务器做一个简单的概述,然后介绍部署WAR文件的各种方法。

Jetty is an open source Java HTTP web server and a servlet container. Jetty is more commonly used in machine to machine communication in the Java ecosystem.

Jetty是一个开源的Java HTTP网络服务器和一个Servlet容器。在Java生态系统中,Jetty更常用于机器对机器的通信。

2. Project Setup

2.项目设置

The latest version of Jetty can always be downloaded by following this link. We will create a very basic Java web application from the command line with Maven, which we will use for our examples.

最新版本的Jetty可以一直按照这个链接下载。我们将用Maven在命令行中创建一个非常基本的Java网络应用程序,我们将用它来做例子。

In this article, we are using Jetty 9.x, the latest version at the moment.

在这篇文章中,我们使用的是Jetty 9.x,目前最新的版本。

Let’s head over to our console, navigate to our location of choice and run the following command:

让我们到我们的控制台,导航到我们选择的位置并运行以下命令。

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

This command will create a complete Java web app inside a new jetty-app folder in our current location. It is just one of many ways of creating a Java application with Maven and it suits our purpose.

该命令将在我们当前位置的一个新jetty-app文件夹中创建一个完整的Java网络应用。这只是用Maven创建Java应用的众多方法之一,它适合我们的目的。

Since we are going to be dealing with WAR files, let’s navigate to the project root and build it:

由于我们将与WAR文件打交道,让我们导航到项目根部并构建它。

cd jetty-app

Building with Maven:

用Maven构建。

mvn package

Then jetty-app.war will be created at location jetty-app/target/jetty-app.war.

然后jetty-app.war将在jetty-app/target/jetty-app.war位置被创建。

3. Jetty Structure

3.栈桥结构

Context path. Refers to the location which is relative to the server’s address and represents the name of the web application.

上下文路径。指的是相对于服务器地址的位置,代表网络应用的名称。

For example, if our web application is put under the $JETTY_HOME\webapps\myapp directory, it will be accessed by the URL http://localhost/myapp, and its context path will be /myapp.

例如,如果我们的网络应用被放在$JETTY_HOME\webapps\myapp目录下,它将通过URL http://localhost/myapp被访问,其上下文路径将是/myapp

WAR. Is the extension of a file that packages a web application directory hierarchy in ZIP format and is short for Web Archive. Java web applications are usually packaged as WAR files for deployment. WAR files can be created on the command line or with an IDE like Eclipse.

WAR。是以ZIP格式打包网络应用程序目录层次的文件的扩展名,是Web Archive的简称。Java网络应用程序通常被打包成WAR文件进行部署。WAR文件可以在命令行上创建,也可以用Eclipse等IDE创建。

4. Deploying by Copying WAR

4.通过复制WAR进行部署

The easiest way to deploy a web application to Jetty server is probably by copying the WAR file into the $JETTY_HOME/webapps directory.

将Web应用程序部署到Jetty服务器的最简单方法可能是将WAR文件复制到$JETTY_HOME/webapps目录。

After copying, we can start the server by navigating to $JETTY_HOME and running the command:

复制后,我们可以通过导航到$JETTY_HOME并运行命令来启动服务器。

java -jar start.jar

Jetty will scan its $JETTY_HOME/webapps directory at startup for web applications to deploy. Our new app will be deployed at /jetty-app context.

Jetty将在启动时扫描其$JETTY_HOME/webapps目录,以寻找要部署的Web应用。我们的新应用将被部署在/jetty-app上下文。

When we load the URL http://localhost:8080/jetty-app from the browser, we should see our app running with Hello world! printed to the screen.

当我们从浏览器加载URL http://localhost:8080/jetty-app时,我们应该看到我们的应用程序正在运行,屏幕上打印出Hello world!

5. Deploying Using Context File

5.使用上下文文件进行部署

Jetty web server offers us a way to deploy a web archive located anywhere in the file system by us creating a context file for it.

Jetty网络服务器为我们提供了一种方法,通过我们为它创建一个上下文文件,可以部署位于文件系统中任何地方的网络归档文件

This way, even if our WAR file is located on a desktop or we have chosen to keep it in jetty-app/target where Maven places the package, we can just create its context file inside $JETTY_HOME/webapps.

这样一来,即使我们的WAR文件位于桌面上,或者我们选择将其放在Maven放置软件包的jetty-app/target中,我们也可以在$JETTY_HOME/webapps中创建其上下文文件。

Let’s undeploy the jetty-app.war we just deployed by deleting it from webapps. We will then create jetty-app.xml with the following code and place it inside webapps:

让我们把刚刚部署的jetty-app.warwebapps中删除,取消部署。然后,我们将用以下代码创建jetty-app.xml,并将其放在webapps中。

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" 
  "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/jetty</Set>
    <Set name="war">absolute/path/to/jetty-app.war</Set>
</Configure>

This context file must have the same name as our WAR, with XML file extension. Notice that we have set the contextPath attribute to /jetty. This means that we will access our web app from the URL http://localhost:8080/jetty.

这个上下文文件的名称必须与我们的WAR相同,扩展名为XML文件。请注意,我们已经将contextPath属性设置为/jetty。这意味着我们将从URLhttp://localhost:8080/jetty访问我们的Web应用程序。

This ability to customize the context path is one of the great advantages of the context file approach of deploying WARs in Jetty as some app names may not be convenient for this purpose.

这种自定义上下文路径的能力是在Jetty中部署WAR的上下文文件方法的巨大优势之一,因为一些应用程序的名称可能不方便用于此目的。

6. Deploying With the Jetty Maven Plugin

6.使用Jetty Maven插件进行部署

6.1. Default Deployment

6.1.默认部署

The jetty Maven plugin helps us to do rapid testing and iteration while building Java web applications. To be able to deploy and run applications with it, we only need to add the plugin in pom.xml:

jetty Maven插件帮助我们在构建Java网络应用时进行快速测试和迭代。为了能够用它部署和运行应用程序,我们只需在pom.xml中添加该插件。

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.3.11.v20160721</version>
</plugin>

The latest version can be found by following this Maven link.

最新版本可以通过这个Maven链接找到。

We have to make sure that our instance of Jetty running on port 8080 is stopped before we perform the next step.

在执行下一步之前,我们必须确保运行在端口8080上的Jetty实例被停止。

To deploy our app after adding the plugin, we navigate to the root where pom.xml is located and run the following command:

要在添加插件后部署我们的应用程序,我们导航到pom.xml所在的根,并运行以下命令。

mvn jetty:run

This command creates a new jetty instance, and the plugin deploys the app to it. We can access it by loading http://localhost:8080.

该命令创建了一个新的jetty实例,该插件将应用程序部署到该实例中。我们可以通过加载http://localhost:8080.来访问它。

The jetty Maven plugin continuously scans the web project for any changes and keeps redeploying it.

jetty Maven插件会持续扫描网络项目的任何变化,并不断重新部署。

6.2. Changing the ContextPath

6.2.改变ContextPath

From the previous subsection, the app was deployed under / context. However, if we would like to deploy under a given context path such as /jetty as before, we will have to configure the plugin differently.

从上一小节来看,该应用程序被部署在/上下文下。然而,如果我们想在给定的上下文路径下部署,如/jetty,我们将不得不以不同的方式配置该插件。

We will change our plugin declaration to the following XML:

我们将把我们的插件声明改为以下XML。

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.3.11.v20160721</version>
    <configuration>
        <webApp>
            <contextPath>/jetty</contextPath>
        </webApp>
    </configuration>
</plugin>

Notice how we have added a configuration block to further customize our deployment. Several configuration options exist to be placed inside this block depending on what one wants.

注意到我们如何添加了一个配置块来进一步定制我们的部署。有几个配置选项可以放在这个块里,这取决于人们想要什么。

After these changes, we can re-run the plugin as before and access our app through http://localhost:8080/jetty.

做完这些改动后,我们可以像以前一样重新运行该插件,并通过http://localhost:8080/jetty访问我们的应用程序。

6.3. Changing the Port

6.3.改变端口

A scenario one may face is a port in use exception. May be we have a jetty instance running on port 8080 for production, but we are still in development phase and want to benefit from the ease of iteration that comes with deploying using the Maven plugin.

我们可能面临的情况是端口使用中的异常。可能我们有一个运行在8080端口上的jetty实例用于生产,但我们仍处于开发阶段,希望受益于使用Maven插件部署时的简易迭代。

In such cases, we have to run our test server on a different port. Let’s change the plugin configuration to the following XML:

在这种情况下,我们必须在一个不同的端口上运行我们的测试服务器。让我们把插件的配置改为以下的XML。

<configuration>
    <webApp>
        <contextPath>/jetty</contextPath>
    </webApp>
    <httpConnector>
        <port>8888</port>
    </httpConnector>
</configuration>

When we re-run our Maven plugin, we will be able to access our app from http://localhost:8888/jetty.

当我们重新运行Maven插件时,我们将能够从http://localhost:8888/jetty访问我们的应用程序。

It is worth noting that with the jetty Maven plugin, we do not need to have an instance of jetty installed and running. Rather, it creates its own jetty instance.

值得注意的是,使用jetty Maven插件,我们不需要安装和运行jetty的实例。相反,它可以创建自己的jetty实例。

7. Deploying With Jetty Runner

7.使用Jetty Runner进行部署

Just like jetty Maven plugin, the jetty-runner offers a fast and easy way to deploy and run our web app. With jetty-runner, we also don’t need to install and run a separate instance of a jetty server.

就像jetty Maven插件一样,jetty-runner为我们部署和运行Web应用提供了一种快速而简便的方法。有了jetty-runner,我们也不需要单独安装和运行一个jetty服务器实例。

7.1. Jetty Runner Setup

7.1.Jetty Runner设置

To use jetty-runner in rapid deployment and running of our web apps, we can download the latest version by following this Maven link.

为了在快速部署和运行我们的Web应用时使用jetty-runner,我们可以通过以下Maven链接下载最新版本。

With jetty-runner, we only need to place its downloaded jar anywhere we please and be ready with the file system path to our web archives.

有了jetty-runner,我们只需要把下载的jar放在任何地方,并准备好文件系统路径到我们的网络档案。

We can pass in configuration parameters from the command line as well as deploy numerous applications at different contexts and bound to different ports with just one command.

我们可以从命令行中传递配置参数,也可以在不同的背景下部署众多的应用程序,只需一个命令就可以将其绑定到不同的端口。

I have placed my jetty-runner jar in the same hierarchy as the jetty-app directory. That is the directory containing our web application.

我把jetty-runner jar放在与jetty-app目录相同的层次结构中。这是包含我们的Web应用程序的目录。

7.2. Basic Deployment

7.2.基本部署

Let’s deploy our WAR using jetty-runner:

让我们使用jetty-runner部署我们的WAR。

java -jar jetty-runner-9.4.0.M1.jar jetty-app/target/jetty-app.war

This command, just like the case of the Maven plugin, creates a jetty instance and deploys the provided WAR to it. The WAR path can be an absolute or a relative path.

该命令与Maven插件一样,创建一个jetty实例,并将提供的WAR部署到该实例中。WAR路径可以是绝对路径,也可以是相对路径。

We can load this application using http://localhost:8080.

我们可以使用http://localhost:8080加载这个应用程序。

7.3. Deploy With Context Path

7.3.使用上下文路径进行部署

To deploy under /jetty context as before:

像以前一样在/jetty上下文下进行部署。

java -jar jetty-runner-9.4.0.M1.jar --path /jetty jetty-app/target/jetty-app.war

Accessible via http://localhost:8080/jetty.

可通过http://localhost:8080/jetty进入。

7.4. Deploy on Given Port

7.4.在指定的端口上进行部署

To deploy on a given port number:

要在一个给定的端口号上进行部署。

java -jar jetty-runner-9.4.0.M1.jar --port 9090 jetty-app/target/jetty-app.war

Accessible via http://localhost:9090.

可通过http://localhost:9090进入。

7.5. Deploy Multiple WARs

7.5.部署多个WAR

To deploy several WARs with the same command, we use the –path argument to make each unique:

为了用同一命令部署多个WAR,我们使用-path参数,使每个WAR都是独一无二的。

java -jar jetty-runner --path /one one.war --path /two two.war

We would then access one.war via http://localhost:8080/one and two.war via http://localhost:8080/two.

然后我们将通过http://localhost:8080/one访问one.war,通过http://localhost:8080/two访问two.war

8. Deploy With Cargo Maven Plugin

8.使用Cargo Maven插件进行部署

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

Cargo是一个多功能的库,它允许我们以标准方式操纵各种类型的应用程序容器。

8.1. Cargo Deployment Setup

8.1.货物部署设置

In this section, we will look at how to use Cargo’s Maven plugin to deploy a WAR to Jetty, in this case we will deploy a WAR to a Jetty 9.x instance.

本节将介绍如何使用Cargo的Maven插件将WAR部署到Jetty,在本例中我们将把WAR部署到Jetty 9.x实例上。

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:

为了牢固掌握整个过程,我们将从头开始,从命令行中创建一个新的Java网络应用程序。

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.

这将在cargo-deploy目录下创建一个完整的Java Web应用程序。如果我们按原样构建、部署和加载这个应用程序,它将在浏览器中打印Hello World!

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 if it was not auto created yet with the following content:

由于我们的Web应用程序不包含任何Servlet,我们的web.xml文件将是非常基本的。因此,导航到我们新创建的项目的WEB-INF文件夹,并创建一个web.xml(如果它还没有自动创建),内容如下。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://Java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://Java.sun.com/xml/ns/javaee 
      http://Java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

    <display-name>cargo-deploy</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

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

为了让Maven在不输入全称的情况下识别cargo的命令,我们需要在Maven的settings.xml的插件组中添加cargo Maven插件。

As an immediate child of the root <settings></settings> element, add this:

作为根<settings></settings>元素的一个直接子元素,添加这个。

<pluginGroups>
    <pluginGroup>org.codehaus.cargo</pluginGroup>
</pluginGroups>

8.2. Local Deploying

8.2.本地部署

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

在本小节中,我们将编辑我们的pom.xml以适应我们新的部署要求。

Add the plugin as follows:

添加该插件的方法如下。

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
            <version>1.5.0</version>
            <configuration>
                <container>
                    <containerId>jetty9x</containerId>
                    <type>installed</type>
                    <home>Insert absolute path to jetty 9 installation</home>
                </container>
                <configuration>
                    <type>existing</type>
                    <home>Insert absolute path to jetty 9 installation</home>
                </configuration>
            </configuration>
       </plugin>
    </plugins>
</build>

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.

请注意,我们明确地将包装定义为WAR,不这样做,我们的构建就会失败。在插件部分,我们又添加了cargo maven2插件。

The latest version at the time of writing is 1.5.0. However, the latest version can always be found here. Additionally, we add a configuration section where we tell Maven that we are using Jetty container and also an existing Jetty installation.

在撰写本文时,最新版本是1.5.0。不过,最新版本可以随时找到这里。此外,我们还添加了一个配置部分,告诉Maven我们正在使用Jetty容器,以及现有的Jetty安装.

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

通过将容器类型设置为installed,我们告诉Maven,我们在机器上安装了一个Jetty实例,并提供了该安装的绝对URL。

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

通过将配置类型设置为existing,我们告诉Maven,我们有一个正在使用的现有设置,不需要进一步配置。

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

另一种方法是告诉cargo下载并安装通过提供一个URL指定的Jetty版本。然而,我们的重点是WAR部署

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

值得注意的是,无论我们使用的是Maven 2.x还是Maven 3.x,cargo maven2插件都适用于两者。

We can now install our application by executing:

现在我们可以通过执行以下命令来安装我们的应用程序。

mvn install

and deploy it by running:

并通过运行来部署它。

mvn cargo:deploy

If all goes well in the Maven and Jetty console, then we should be able to run our web application by loading http://localhost:8080/cargo-deploy.

如果Maven和Jetty控制台一切顺利,那么我们应该能够通过加载http://localhost:8080/cargo-deploy.来运行我们的网络应用。

If we check the $JETTY_HOME/webapps folder, we will find a deployment descriptor file or what we earlier on called context file called cargo-deploy.xml created by cargo.

如果我们检查$JETTY_HOME/webapps文件夹,我们会发现一个部署描述符文件,或者我们前面所说的上下文文件,叫做cargo-deploy.xml,由cargo创建。

8.3. Remote Deploying

8.3.远程部署

By default, Jetty does not come with possibilities for remote deployment. In order to add such a support to Jetty, Cargo uses the Jetty remote deployer Web application.

默认情况下,Jetty并不具备远程部署的可能性。为了给Jetty增加这样的支持,Cargo使用了Jetty远程部署器Web应用程序。

What this means is that we have to download a web application WAR pre-created by the Cargo developers, deploy this WAR to the target jetty container.

这意味着我们必须下载一个由Cargo开发者预先创建的Web应用WAR,并将这个WAR部署到目标jetty容器中。

Every time we want to deploy to this remote server using cargo Maven plugin, it will send an HTTP request to the deployer application on the remote server with our WAR for deployment.

每次我们想使用货物Maven插件部署到该远程服务器时,它都会向远程服务器上的部署器应用程序发送HTTP请求,并附上我们的WAR进行部署。

This remote deployer can be found here. Head over to the tools section and download the cargo-jetty-7-and-onwards-deployer WAR.

这个远程部署器可以在这里找到。前往tools部分,下载cargo-jetty-7 and-onwards-deployer WAR。

Security considerations

安全考虑

We must set up a security realm in jetty before this can work, for authentication purposes. Create a file called realm.properties in $JETTY_HOME/etc directory of the remote jetty server. The file content is:

我们必须在jetty中设置一个安全境界,才能工作,以达到认证的目的。在远程jetty服务器的$JETTY_HOME/etc目录下创建一个名为realm.properties的文件。文件的内容是。

admin:password,manager

The admin is the user name by which the client can access the secured apps, password is the password and manager is the role the clients must possess before being granted access.

admin是客户端可以访问安全应用程序的用户名,password是密码,manager是客户端在被授权访问前必须拥有的角色。

We must also declare our security requirements in the deployer application. We will unpack the WAR we downloaded from the jetty downloads page, make some changes and pack it back into a WAR.

我们还必须在部署器应用程序中声明我们的安全要求。我们将解压我们从jetty下载的WAR,做一些修改,并将其打包成WAR。

After unpacking, head over to WEB-INF/web.xml and uncomment the XML code with the Uncomment in order to activate security comment. Or place the following code there:

解压后,前往WEB-INF/web.xml,用Uncomment in order to activate security注释取消XML代码。或者把下面的代码放在那里。

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Jetty Remote Deployer</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>manager</role-name>
    </auth-constraint>
</security-constraint>

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>Test Realm</realm-name>
</login-config>

Deploying the deployer

展开部署器

We can now pack the app back into a WAR and copy it to any location on the remote server. We will then deploy it to Jetty.

现在我们可以将应用程序打包成WAR,并将其复制到远程服务器的任何位置。然后,我们将把它部署到Jetty。

During deployment, it is best to use a deployment descriptor file so that we can create a securityHandler and pass to it a loginService. All secured applications must have a login service, or else jetty will fail to deploy them.

在部署过程中,最好使用部署描述符文件,这样我们就可以创建一个securityHandler并向其传递一个loginService。所有安全的应用程序都必须有一个登录服务,否则jetty将无法部署它们。

Now, let us create a context file in $JETTY_HOME/webapps of the remote jetty instance, remember the rules of naming the context file. Make it the same name as the WAR:

现在,让我们在远程jetty实例的$JETTY_HOME/webapps创建一个上下文文件,记住上下文文件的命名规则。让它与WAR的名字相同。

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" 
  "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/deployer</Set>
    <Set name="war">absolute/path/to/cargo-jetty-deployer.war</Set>
    <Get name="securityHandler">
        <Set name="loginService">
            <New class="org.eclipse.jetty.security.HashLoginService">
                <Set name="name">Test Realm</Set>
                <Set name="config"><SystemProperty name="jetty.home" 
                   default="."/>/etc/realm.properties</Set>
            </New>
        </Set>
    </Get>
</Configure>

Start the remote jetty server and if everything goes well, we should be able to load http://localhost:8080/cargo-jetty-deployer. We should then be able to see something like:

启动远程jetty服务器,如果一切顺利,我们应该能够加载http://localhost:8080/cargo-jetty-deployer。然后我们应该能够看到类似的东西。

HTTP ERROR 400

Problem accessing /cargo-jetty-deployer/. Reason:

    Command / is unknown

Deploying WAR to remote Jetty

将WAR部署到远程Jetty

To do a remote deploy we only need to change our configuration section of pom.xml. Remote deploy means that we do not have a local installation of Jetty but have authenticated access to the deployer app running on the remote server.

要进行远程部署,我们只需要改变pom.xml的配置部分。远程部署意味着我们没有Jetty的本地安装,但有对运行在远程服务器上的部署器应用的认证访问。

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

所以让我们改变pom.xml,使配置部分看起来像这样。

<configuration>
    <container>
        <containerId>jetty9x</containerId>
        <type>remote</type>
    </container>
    <configuration>          
        <type>runtime</type>
        <properties>
      <cargo.hostname>127.0.0.1</cargo.hostname>
            <cargo.servlet.port>8080</cargo.servlet.port>
            <cargo.remote.username>admin</cargo.remote.username>
            <cargo.remote.password>password</cargo.remote.password>
        </properties>
    </configuration>
</configuration>

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

这一次,我们把容器类型从installed改为remote,配置类型从existing改为runtime。最后,我们将主机名、端口和认证属性添加到配置中。

clean the project:

清洁项目。

mvn clean

install it:

安装它。

mvn install

finally, deploy it:

最后,部署它。

mvn cargo:deploy

That’s it.

就这样了。

9. Deploying from Eclipse

9.从Eclipse部署

Eclipse allows us to embed servers in order to add web project deployment in the normal workflow without navigating away from the IDE.

Eclipse允许我们嵌入服务器,以便在正常的工作流程中增加网络项目的部署,而不需要从IDE中导航出来。

9.1. Embedding Jetty in Eclipse

9.1.在Eclipse中嵌入Jetty

We can embed a Jetty installation into eclipse by selecting window item from taskbar and then preferences from the drop down menu.

我们可以通过从任务栏选择window项,然后从下拉菜单中选择preferences,将Jetty安装嵌入eclipse。

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.

我们会发现在出现的窗口的左边面板上有一个树状的偏好项目的网格。然后我们可以导航到eclipse -> servers或者直接在搜索栏中输入services。

We then select Jetty directory if not already open for us and choose the Jetty version we downloaded.

然后我们选择Jetty目录,如果还没有为我们打开,选择我们下载的Jetty版本。

On the right side of the panel will appear a configuration page where we select the enable option to activate this Jetty version and browse to the installation folder.

在面板的右侧将出现一个配置页面,我们选择启用选项来激活这个Jetty版本,并浏览到安装文件夹。

From the screen shots, jetty 7.x will be replaced by the version of jetty we configured.

从屏幕截图来看,jetty 7.x将被我们配置的jetty版本所取代。

Capture-3

 

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.

我们应用变化,下次我们从eclipse的windows->显示视图子菜单中打开服务器视图时,新配置的服务器将出现,我们可以启动、停止和部署应用程序到它。

9.2. Deploying Web Application in Embedded Jetty

9.2.在嵌入式Jetty中部署Web应用程序

To deploy a web application to the embedded Jetty instance, it must exist in our workspace.

要将一个Web应用程序部署到嵌入式Jetty实例,它必须存在于我们的工作空间中。

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.

从窗口->显示视图打开servers视图,寻找服务器。打开后,我们可以直接右击我们配置的服务器,并从出现的上下文菜单中选择添加部署

Capture-4

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.

项目组合框下面有一个部署类型部分,当我们选择爆炸存档(开发模式)时,我们在应用程序中的变化将被实时同步,而不必重新部署,这是开发期间的最佳选择,因为它非常高效。

Capture-5

 

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.

选择Packaged Archive(production mode)将要求我们在每次进行修改并在浏览器中看到它们时都要重新部署。这只对生产来说是最好的,但仍然,Eclipse使它同样简单。

9.3. Deploying Web Application in External Location

9.3.在外部位置部署Web应用程序

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.

我们通常选择通过Eclipse来部署WAR,以使调试更容易。可能有一天,我们想把它部署到Eclipse的嵌入式服务器所使用的位置以外的地方。

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.

我们可以绕过这个程序,在生产模式下进行部署,并在新部署对话框中注意到部署位置,然后从那里挑选WAR。

During deployment, instead of selecting an embedded server, we can select the <Externally Launched> option from the servers view alongside the list of embedded servers. We navigate to the $JETTY_HOME/webapps directory of an external Jetty installation.

在部署过程中,我们可以从servers视图中选择<Externally Launched>选项,而不是选择一个嵌入式服务器,与嵌入式服务器列表并列。我们导航到外部Jetty安装的$JETTY_HOME/webapps目录。

10. Deploying from IntelliJ IDEA

10.从IntelliJ IDEA进行部署

To deploy a web application to Jetty, it must exist and have already been downloaded and installed.

要在Jetty上部署一个Web应用程序,它必须存在并且已经被下载和安装。

10.1. Local Configuration

10.1.本地配置

Open the Run menu and click the Edit Configurations options.

打开Run菜单,点击Edit Configurations选项。

In the panel on the left search for Jetty Server, if it is not there click the + sign in the menu, search for Jetty and select Local. In the name field put Jetty 9.

在左边的面板上搜索Jetty Server,如果它不在那里,点击菜单中的+号,搜索Jetty并选择Local。在名称字段中输入Jetty 9

Click the Configure… button and in Jetty Home field navigate to the home location of your installation and select it.

点击Configure…按钮,在Jetty Home字段中导航到你的安装主页位置并选择它。

Optionally, set the Startup page to be http://localhost:8080/ and HTTP port: 8080, change the port as appropriate.

可选择将启动页面设置为http://localhost:8080/ HTTP端口。8080,根据情况改变端口。

Go to the Deployment tab and click on the + symbol, select artifact you want to add to the server and click OK

进入部署标签,点击+符号,选择你想添加到服务器的工件,然后点击确定

10.2. Remote Configuration

10.2.远程配置

Follow the same instructions as for local Jetty configurations but in the server tab, you must enter the remote location of the installation.

遵循与本地Jetty配置相同的说明,但在服务器标签中,你必须输入安装的远程位置。

11. Conclusion

11.结论

In this article, we have covered extensively the various ways of deploying a WAR file in Jetty web server.

在这篇文章中,我们已经广泛地介绍了在Jetty web服务器中部署WAR文件的各种方法。