在 Eclipse 中使用 Maven 構(gòu)建 Web 項(xiàng)目

在 Eclipse 中使用 Maven 創(chuàng)建 Web 項(xiàng)目

1. 建立 Maven Project

選擇 File→New→Other,在 New 對(duì)話框中選擇 Maven→Maven Project→Next。

newmaven.png

2. 選擇項(xiàng)目路徑

勾選 Use default Workspace location(使用默認(rèn)工作空間)→Next。

newmaven2.png

3. 選擇項(xiàng)目類型

在 Artifact Id 中選擇 maven-archetype-webapp。如果是 Spring Boot 項(xiàng)目,可以選擇 maven-archetype-quickstart。

newmaven3.png

4. 輸入 Group ID、Artifact ID 以及 Package

newmaven4.png

注意:Group Id 一般寫(xiě)大項(xiàng)目名稱,Artifact Id 是子項(xiàng)目名稱,Package 是默認(rèn)的包名,不寫(xiě)也可以。

剛建立好后的文件結(jié)構(gòu)如下圖所示。

newmaven5.png

如果這里顯示的內(nèi)容多,一般是 Filters 設(shè)置的問(wèn)題;或者 Perspective 為 JavaEE 模式,將其改成 Java 模式就可以了。

5. 配置項(xiàng)目

在選擇 maven_archetype_web 原型后,如上圖所示,項(xiàng)目中默認(rèn)只有 src/main/resources 這個(gè) Source Floder,還需要添加 src/main/java、src/test/java 和src/test/resources 這三個(gè) Source Floder。右鍵項(xiàng)目根目錄點(diǎn)擊 New→Source Folder,建出這三個(gè)文件夾。

newmaven6.png

可能只有 src/test/resources 的 Source Folder 可以創(chuàng)建,

newmaven72.png

而添加 src/main/java、src/test/java 的 Source Floder 時(shí)可能會(huì)報(bào) The folder is already a source folder 的錯(cuò)誤。如下圖所示。

newmaven75.png

分析原因:右鍵項(xiàng)目→Properties→Java Build Path→Source 標(biāo)簽,會(huì)看到 src/main/java, src/test/java 已存在,但是后面的括號(hào)中的內(nèi)容為 missing。所以只需要?jiǎng)?chuàng)建目錄,Source Floder 應(yīng)該就會(huì)出現(xiàn)了。

newmaven76.png

解決辦法:使用 Navigator 視圖(如果 Package Explorer 標(biāo)簽旁邊沒(méi)有Navigator標(biāo)簽,那么 Preferences→Show View→Navigator 即可顯示 Navigator 標(biāo)簽)直接建立文件夾。比如創(chuàng)建 src/main/java:選擇 Navigator 視圖→右鍵目錄樹(shù)中的 src/main 目錄→New→Folder(在現(xiàn)有的 src/main 目錄下直接創(chuàng)建 java 子目錄),如下圖所示。

newmaven7.png
newmaven77.png

最終在 Package Explorer 視圖中要有前面說(shuō)過(guò)的四個(gè) Source Folder,如下圖所示。

newmaven78.png

6. 更改編譯輸出文件的存放目錄

右鍵項(xiàng)目→Build Path→Configure Build Path→Source 標(biāo)簽,

newmaven74.png

Source 標(biāo)簽中應(yīng)該有下面 4 個(gè)已經(jīng)創(chuàng)建的文件夾:

  • src/main/java
  • src/main/resources
  • src/test/java
  • src/test/resources

勾選上最下面的 Allow output folders for source folders 復(fù)選框來(lái)允許為 Source Folder 創(chuàng)建輸出文件夾。然后雙擊每個(gè) Source Folder 文件夾下的 Output folder(有的 Source Folder 使用最下方的 Browse... 按鈕左邊的 Default output folder 作為默認(rèn)的編譯輸出文件夾),按如下規(guī)則選擇輸出目錄:

  • src/main/java 和 src/main/resources,選擇 target/classes;
  • src/test/java 和 src/test/resources,選擇 target/test-classes;
newmaven79.png

注意:target/classes 和 target/test-classes 目錄存放著編譯輸出的“.class”文件即相關(guān)配置文件,這些“.class”文件和配置文件也是按照相應(yīng)的包結(jié)構(gòu)存放的。

如果需要更改文件夾顯示的順序:通過(guò) Order and Export 標(biāo)簽進(jìn)行排序即可。

更改 JRE 版本:點(diǎn)擊 Libraries 標(biāo)簽→雙擊 JRE System Library,選擇合適的(Maven 構(gòu)建 Web 工程需要使用 JDK 安裝目錄下私有 JRE,即“Jdk_Installed_Directory/jre”,而不能是公共 JRE,關(guān)于私有 JRE 和公共 JRE 的區(qū)別請(qǐng)參考 Oracle 官網(wǎng)上關(guān)于 Private Versus Public JRE 的介紹)、較新的 JRE版本。

newmaven710.png

關(guān)于 JRE 的設(shè)置,可通過(guò)點(diǎn)擊 Installed JREs...按鈕→通過(guò) Add.. 按鈕(或 Edit... 按鈕)來(lái)添加系統(tǒng)中已安裝(或編輯已添加)的 JRE,可以指定系統(tǒng)中已安裝 JRE的路徑(JRE home)和 JRE 的別名(JRE name),當(dāng)然也可以復(fù)制(通過(guò) Duplicate... 按鈕)和刪除(通過(guò) Remove... 按鈕)已添加的 JRE。如下圖所示。

newmaven711.png

7. 把項(xiàng)目變成 Dynamic Web 項(xiàng)目

如果是 Spring Boot 項(xiàng)目,可以省略該該步驟。

右鍵項(xiàng)目,選擇 Properties→Project Facets,點(diǎn)擊 Convert to faceted form... 按鈕。取消 Dynamic Web Module 復(fù)選框的勾選(否則更改其版本時(shí)下面會(huì)提示 Cannot change version of project facet Dynamic Web Module to X.x 的錯(cuò)誤),將其版本改為 3.0(需要 1.6 及以上的 JDK),然后將Java的版本改為 1.8,點(diǎn)擊 Apply 按鈕。

newmaven713.png

再勾選 Dynamic Web Module 復(fù)選框,點(diǎn)擊下方彈出的 Further configuration available... 臨時(shí)按鈕,在彈出的對(duì)話框中將 Content directory 改為src/main/webapp,同時(shí)勾選 Generate web.xml deployment descriptor 復(fù)選框來(lái)生成 web.xml 配置文件,依次點(diǎn)擊 OK、Apply 和 OK 按鈕。如下圖所示。

newmaven714.png

如果提示錯(cuò)誤,可能需要在 Java Compiler 設(shè)置 Compiler compliance level 為 1.6 或更高,或者需要在此窗口的 Java 的 Version 改成 1.6(Dynamic Web Module 為 3.0 時(shí))或更高。

8. 設(shè)置部署程序集(Web Deployment Assembly)

再次打開(kāi)當(dāng)前項(xiàng)目的 Properties 窗口,點(diǎn)擊左側(cè)列表中 Deployment Assembly,此處顯示的列表是,部署項(xiàng)目時(shí),文件發(fā)布的路徑。如下圖所示。

newmaven715.png

刪除名稱中含有 test 的文件夾,因?yàn)?test 是測(cè)試使用,并不需要部署。然后依次點(diǎn)擊 Apply 和 OK 按鈕。置完成效果如下圖所示。

newmaven716.png

9. 構(gòu)建框架:在 pom.xml 中添加所需要的 jar 包(依賴和插件)

雙擊項(xiàng)目中的 pom.xml 文件,默認(rèn)使用 Maven POM Editor 打開(kāi) pom.xml 文件,選擇 Dependencies 標(biāo)簽,在 Dependencies 欄目點(diǎn)擊 Add 按鈕,首先彈出一個(gè)搜索按鈕,例如輸入 javax.servlet,就會(huì)自動(dòng)搜索關(guān)于 servlet 相關(guān)的 jar 包,可以很方便的地選擇使用比較多的版本的jar包,還可以通過(guò) Scope 來(lái)指定 jar 包的作用范圍。如下圖所示。

newmaven718.png

需要添加的其他 jar 包有:junit、jstl。或者點(diǎn)擊 pom.xml 標(biāo)簽直接編輯 pom.xml 文件,這樣可以直接粘貼從 Maven 倉(cāng)庫(kù)(通過(guò) Maven 倉(cāng)庫(kù),我們可以搜索各種版本的jar包)中復(fù)制過(guò)來(lái) dependency 內(nèi)容。這里給出我們的 pom.xml 內(nèi)容:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.rosydawn.maven.demo</groupId>
  <artifactId>maven-web-demo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>maven-web-demo Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>maven-web-demo</finalName>
  </build>
</project>

10. 運(yùn)行

通過(guò) Tomcat 插件運(yùn)行

右鍵項(xiàng)目→ Run As → Maven install → 右鍵項(xiàng)目 → Run As → Package,生成完后 Run As → Run on Servers 即可。

也可以使用 Tomcat 的 Maven 插件,在 pom.xml 文件中添加該插件后,即可通過(guò)該插件運(yùn)行 Web 項(xiàng)目。可通過(guò)在 pom.xml 文件的 <bulid> 元素中添加以下內(nèi)容來(lái)添加該插件。如以下代碼所示:

  <build>
    <finalName>maven-web-demo</finalName>
    <plugins>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
                <port>9999</port><!-- 定義程序運(yùn)行端口 -->
                <path>/</path><!-- 定義程序映射目錄 -->
                <urlEncoding>utf8</urlEncoding><!--解決url亂碼 -->
          </configuration>
        </plugin>
      </plugins>
  </build>

添加完該插件后,通過(guò)右鍵項(xiàng)目→ Run As → Maven build...,如下圖所示。

newmaven8.png

然后在彈出的對(duì)話框的 Goals 欄中填寫(xiě) tomcat7:run,Name 欄為默認(rèn)的運(yùn)行配置名稱。后續(xù)再運(yùn)行該項(xiàng)目時(shí),可在接選中項(xiàng)目后,如下圖所示,

newmaven9.png

點(diǎn)擊工具欄運(yùn)行按鈕,選擇初次運(yùn)行該項(xiàng)目時(shí)默認(rèn)運(yùn)行配置名稱(即 maven-web-demo)即可運(yùn)行該項(xiàng)目了。

通過(guò) Jetty 插件運(yùn)行

Tomcat 的 Maven 插件只支持到 Tomcat 7,從 2013 年到現(xiàn)在(2021 年)一直沒(méi)有再更新過(guò),不建議使用。可以考慮使用 Jetty 的 Maven 插件,如下所示。

  <build>
    <finalName>maven-web-demo</finalName>
    <plugins>
        <!--Jetty 9.x插件。You should use Maven 3.3+ for this plugin-->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.25.v20180606</version>
                <!--<version>9.3.24.v20180605</version>-->
                <!--<version>9.4.11.v20180605</version>-->
                <configuration>
                    <!--可選。httpConnector元素用于配置connector-->
                    <!--如果沒(méi)有指定該元素,則會(huì)默認(rèn)創(chuàng)建一個(gè)監(jiān)聽(tīng)8080端口的ServerConnector。也可以通過(guò)標(biāo)準(zhǔn)的jetty xml配置文件
                    來(lái)設(shè)置connector,然后在jettyXml元素中引入這些配置文件-->
                    <httpConnector>
                        <!--port元素指定connector的監(jiān)聽(tīng)端口。默認(rèn)為8080-->
                        <port>8080</port>

                        <!--host元素指定connector監(jiān)聽(tīng)的接口。默認(rèn)監(jiān)聽(tīng)所有接口-->
                        <!--<host>localhost</host>-->

                        <!--name元素指定connector名稱。在配置特定connector的響應(yīng)上下文時(shí)有用-->
                        <!--<name>jetty-connector</name>-->

                        <!--idleTimeout元素指定連接的最大空閑時(shí)間-->
                        <!--<idleTimeout>1800</idleTimeout>-->

                        <!--soLinger元素設(shè)置socket linger時(shí)間-->
                        <!--<soLinger>0</soLinger>-->
                    </httpConnector>

                    <!--可選。jettyXml元素指定逗號(hào)分隔的jetty xml文件列表,用于配置其他jetty信息,功能強(qiáng)大-->
                    <!--jetty-context.xml文件配置忽略代碼校驗(yàn),以免jetty啟動(dòng)很慢-->
                    <jettyXml>${project.basedir}/src/main/webapp/WEB-INF/jetty-context.xml</jettyXml>

                    <!--可選。scanIntervalSeconds元素設(shè)置檢查是否發(fā)生變更以及如果有變更就自動(dòng)熱部署的時(shí)間間隔-->
                    <!-- if you save changes and recompile your class files, Jetty redeploys your webapp, and you can
                    instantly test the changes you just made-->
                    <!--默認(rèn)為0,表示不進(jìn)行變更檢查和自動(dòng)熱部署-->
                    <scanIntervalSeconds>5</scanIntervalSeconds>

                    <!--可選。reload元素設(shè)置內(nèi)容改變后是否自動(dòng)重新加載和部署。默認(rèn)為automatic,結(jié)合scanIntervalSeconds元素的非零值使用。-->
                    <!--設(shè)置為manual將忽略scanIntervalSeconds元素的設(shè)置-->
                    <!--<reload>manual</reload>-->

                    <!--可選。dumpOnStart元素設(shè)置在啟動(dòng)時(shí)是否清空容器(dump out the server structure)。默認(rèn)為false-->
                    <!--<dumpOnStart>true</dumpOnStart>-->

                    <!--可選。loginServices元素可配置多個(gè)org.eclipse.jetty.security.LoginService實(shí)現(xiàn)。注意,并沒(méi)有默認(rèn)
                    的realm,如果你在web.xml中用到了一個(gè)realm,可以在這里指定相關(guān)的realm。也可以通過(guò)標(biāo)準(zhǔn)的jetty xml配置文件
                    來(lái)設(shè)置connector,然后在jettyXml元素中引入這些配置文件-->
                    <!--<loginServices>
                        <loginService implementation="org.eclipse.jetty.security.HashLoginService">
                            <name>Test Realm</name>
                            <config>${basedir}/src/etc/realm.properties</config>
                        </loginService>
                    </loginServices>-->

                    <!--可選。requestLog元素指定一個(gè)org.eclipse.jetty.server.RequestLog實(shí)現(xiàn)類。還可以有其他三種方式來(lái)配置:
                    jetty xml文件和jettyXml元素,context xml文件和contextXml元素,webApp元素-->
                    <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">
                        <filename>target/access-yyyy_mm_dd.log</filename>
                        <filenameDateFormat>yyyy_MM_dd</filenameDateFormat>
                        <logDateFormat>yyyy-MM-dd HH:mm:ss</logDateFormat>
                        <logTimeZone>GMT+8:00</logTimeZone>
                        <append>true</append>
                        <logServer>true</logServer>
                        <retainDays>120</retainDays>
                        <logCookies>true</logCookies>
                    </requestLog>

                    <!--自Jetty 9.3.1版本開(kāi)始可選。配置一個(gè)org.eclipse.jetty.server.Server實(shí)例。
                    一般不用設(shè)置該元素,尤其是使用了jettyXml元素的情況下(在jetty xml中就可以配置Server)。
                    如果同時(shí)配置了兩者,那么jetty xml文件將會(huì)覆蓋pom.xml配置-->
                    <!--<server></server>-->

                    <!--可選。stopPort元素指定接收stop命令的端口。與jetty:stop或jetty:run-forked目標(biāo)一起使用時(shí)有用-->
                    <stopPort>9966</stopPort>
                    <!--可選。stopKey元素和stopPort元素配合使用,用于指定發(fā)送到關(guān)閉端口的關(guān)閉命令的字符串,從而來(lái)驗(yàn)證stop命令。 -->
                    <stopKey>shutdown</stopKey>

                    <!--可選。systemProperties元素用于配置該插件運(yùn)行時(shí)的系統(tǒng)屬性-->
                    <!--<systemProperties>
                        <systemProperty>
                            <name>foo_prop</name>
                            <value>bar_value</value>
                        </systemProperty>
                    </systemProperties>-->

                    <!--可選。systemPropertiesFile指定指定一個(gè)包含該插件運(yùn)行時(shí)的系統(tǒng)屬性的文件。
                    這里設(shè)置的系統(tǒng)屬性默認(rèn)不會(huì)覆蓋命令行、JVM和systemProperties元素指定的系統(tǒng)屬性,也可以設(shè)置為強(qiáng)制覆蓋-->
                    <!--<systemPropertiesFile></systemPropertiesFile>-->

                    <!--可選。skip元素用于設(shè)置是否執(zhí)行該插件,默認(rèn)為false。與在命令行設(shè)置-Djetty.skip系統(tǒng)屬性有相同效果。
                    在集成測(cè)試中想要跳過(guò)測(cè)試時(shí)這樣配置jetty很有用-->
                    <!--<skip>true</skip>-->

                    <!--默認(rèn)為false。如果為true,則會(huì)將<scope>provided</scope>類型的依賴添加到容器的類路徑中,一般都會(huì)使用容
                    器已經(jīng)提供的依賴,所以,一般都不用配置該元素。除非容器提供的依賴達(dá)不到使用要求-->
                    <!--<useProvidedScope>true</useProvidedScope>-->

                    <!--可選。excludedGoals元素指定不執(zhí)行的Jetty插件的目標(biāo)列表-->
                    <!--<excludedGoals>run,run-exploded</excludedGoals>-->

                    <!--忽略代碼校驗(yàn),以免 Jetty 啟動(dòng)很慢。-->
                    <contextXml>${project.basedir}/src/main/webapp/WEB-INF/jetty-context.xml</contextXml>

                    <!-- 指定系統(tǒng)屬性。這里配置的屬性不會(huì)覆蓋命令行和JVM指定的屬性,僅用于補(bǔ)充兩者配置的屬性。 -->
                    <!--<systemProperties>
                        <systemProperty>
                            <name>foo_prop</name>
                            <value>bar_value</value>
                        </systemProperty>
                    </systemProperties>-->
                    <!-- 也可以選擇使用屬性文件類配置系統(tǒng)屬性。這里配置的屬性不會(huì)覆蓋命令行、JVM和systemProperties指定的屬性,
                    僅用于補(bǔ)充三者配置的屬性。-->
                    <!--<systemPropertiesFile>${project.basedir}/my.props</systemPropertiesFile>-->

                    <webApp>
                        <!-- 設(shè)置項(xiàng)目的ContextPath,默認(rèn)為“/${project.artifactId}”。 -->
                        <contextPath>/${project.artifactId}</contextPath>
                        <!-- Jetty提供靜態(tài)資源的多個(gè)目錄,用來(lái)替換baseResource元素。 -->
                        <resourceBases>
                            <resourceBase>${project.basedir}/src/main/webapp</resourceBase>
                            <!--<resourceBase>${project.basedir}/commons</resourceBase>-->
                        </resourceBases>
                        <!-- 指定一個(gè)jetty-env.xml文件的路徑,在該文件中可使用JNDI。
                         確定該文件打包進(jìn)classpath中,如果把該文件放在測(cè)試目錄就不行。-->
                        <jettyEnvXml>${project.basedir}/src/main/webapp/WEB-INF/jetty-env.xml</jettyEnvXml>
                    </webApp>
                </configuration>
            </plugin>

            <!--Jetty 7.x or Jetty 8.x插件。You will need to use maven 3 from jetty-7.5.3 release going forwards-->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.16.v20140903</version>
                <configuration>
                    <!--可選。connectors元素指定一個(gè)org.eclipse.jetty.server.Connector對(duì)象(jetty的端口監(jiān)聽(tīng)器)列表。如果沒(méi)有指定,
                    則會(huì)默認(rèn)在8080端口上配置一個(gè)NIO org.eclipse.jetty.server.nio.SelectChannelConnector對(duì)象。也可以在標(biāo)準(zhǔn)
                    jetty xml文件里配置connectors,然后在jettyXml元素中引用該配置文件-->
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <!-- 可通過(guò)命令行指令“mvn -Djetty.port=9999 jetty:run” 來(lái)設(shè)置jetty.port系統(tǒng)屬性更改默認(rèn)端口。 -->
                            <port>8080</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>9090</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>

                    <!--可選。jettyXml元素指定逗號(hào)分隔的jetty xml文件列表,用于配置其他jetty信息,功能強(qiáng)大-->
                    <!--jetty-context.xml文件配置忽略代碼校驗(yàn),以免jetty啟動(dòng)很慢-->
                    <jettyXml>${project.basedir}/src/main/webapp/WEB-INF/jetty-context.xml</jettyXml>

                    <!--可選。scanIntervalSeconds元素設(shè)置檢查是否發(fā)生變更以及如果有變更就自動(dòng)熱部署的時(shí)間間隔-->
                    <!-- if you save changes and recompile your class files(you can use IDE auto build function),
                    Jetty redeploys your webapp, and instantly test the changes you just made-->
                    <!--默認(rèn)為0,表示不進(jìn)行變更檢查和自動(dòng)熱部署-->
                    <scanIntervalSeconds>5</scanIntervalSeconds>

                    <!--可選。systemProperties元素用于配置該插件運(yùn)行時(shí)的系統(tǒng)屬性-->
                    <!--<systemProperties>
                        <systemProperty>
                            <name>foo_prop</name>
                            <value>bar_value</value>
                        </systemProperty>
                    </systemProperties>-->
                    <!--可選。systemPropertiesFile指定指定一個(gè)包含該插件運(yùn)行時(shí)的系統(tǒng)屬性的文件。這里設(shè)置的系統(tǒng)屬性默認(rèn)不會(huì)覆蓋命令
                    行、JVM和systemProperties元素指定的系統(tǒng)屬性,僅用于補(bǔ)充三者配置的屬性,也可以設(shè)置為強(qiáng)制覆蓋-->
                    <!--<systemPropertiesFile>${basedir}/my.props</systemPropertiesFile>-->

                    <!--可選。skip元素用于設(shè)置是否執(zhí)行該插件,默認(rèn)為false。與在命令行設(shè)置-Djetty.skip系統(tǒng)屬性有相同效果。
                    這在集成測(cè)試中配置jetty跳過(guò)測(cè)試時(shí)很有用-->
                    <!--<skip>true</skip>-->

                    <!--可選。loginServices元素可配置多個(gè)org.eclipse.jetty.security.LoginService實(shí)現(xiàn)。注意,并沒(méi)有默認(rèn)
                    的realm,如果你在web.xml中用到了一個(gè)realm,可以在這里指定相關(guān)的realm。也可以通過(guò)標(biāo)準(zhǔn)的jetty xml配置文件
                    來(lái)設(shè)置connector,然后在jettyXml元素中引入這些配置文件-->
                    <!--<loginServices>
                        <loginService implementation="org.eclipse.jetty.security.HashLoginService">
                            <name>Test Realm</name>
                            <config>${basedir}/src/etc/realm.properties</config>
                        </loginService>
                    </loginServices>-->

                    <!--可選。requestLog元素指定一個(gè)org.eclipse.jetty.server.RequestLog實(shí)現(xiàn)類。還可以有其他三種方式來(lái)配置:
                    jetty xml文件和jettyXml元素,context xml文件和contextXml元素,webApp元素-->
                    <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">
                        <filename>target/access-yyyy_mm_dd.log</filename>
                        <filenameDateFormat>yyyy_MM_dd</filenameDateFormat>
                        <logDateFormat>yyyy-MM-dd HH:mm:ss</logDateFormat>
                        <logTimeZone>GMT+8:00</logTimeZone>
                        <append>true</append>
                        <logServer>true</logServer>
                        <retainDays>120</retainDays>
                        <logCookies>true</logCookies>
                    </requestLog>

                    <!--可選。stopPort元素指定接收stop命令的端口。與jetty:stop或jetty:run-forked目標(biāo)一起使用時(shí)有用-->
                    <stopPort>9966</stopPort>
                    <!--可選。stopKey元素和stopPort元素配合使用,用于指定發(fā)送到關(guān)閉端口的關(guān)閉命令的字符串,從而來(lái)驗(yàn)證stop命令。 -->
                    <stopKey>shutdown</stopKey>

                    <!--可選。scanIntervalSeconds元素設(shè)置檢查是否發(fā)生變更以及如果有變更就自動(dòng)熱部署的時(shí)間間隔-->
                    <!-- if you save changes and recompile your class files, Jetty redeploys your webapp, and you can
                    instantly test the changes you just made-->
                    <!--默認(rèn)為0,表示不進(jìn)行變更檢查和自動(dòng)熱部署-->
                    <scanIntervalSeconds>5</scanIntervalSeconds>

                    <!--可選。reload元素設(shè)置內(nèi)容改變后是否自動(dòng)重新加載和部署。默認(rèn)為automatic,結(jié)合scanIntervalSeconds元素的非零值使用。-->
                    <!--設(shè)置為manual將忽略scanIntervalSeconds元素的設(shè)置-->
                    <!--<reload>manual</reload>-->

                    <!--忽略代碼校驗(yàn),以免 Jetty 啟動(dòng)很慢。-->
                    <contextXml>${project.basedir}/src/main/webapp/WEB-INF/jetty-context.xml</contextXml>
                    <!--<dumpOnStart>true</dumpOnStart>-->

                    <!--可選。systemProperties元素用于配置該插件運(yùn)行時(shí)的系統(tǒng)屬性-->
                    <!--<systemProperties>
                        <systemProperty>
                            <name>foo_prop</name>
                            <value>bar_value</value>
                        </systemProperty>
                    </systemProperties>-->
                    <!--可選。systemPropertiesFile指定指定一個(gè)包含該插件運(yùn)行時(shí)的系統(tǒng)屬性的文件。這里設(shè)置的系統(tǒng)屬性默認(rèn)不會(huì)覆蓋命令
                    行、JVM和systemProperties元素指定的系統(tǒng)屬性,僅用于補(bǔ)充三者配置的屬性,也可以設(shè)置為強(qiáng)制覆蓋-->
                    <!--<systemPropertiesFile>${basedir}/my.props</systemPropertiesFile>-->

                    <!--Configuring Your WebApp-->
                    <!--webApp元素配置一個(gè)org.eclipse.jetty.webapp.WebAppContext的擴(kuò)展,其任何setter方法都可用來(lái)配置Web應(yīng)用-->
                    <webApp>
                        <!-- set the context path for your webapp,default is “/${project.artifactId}”-->
                        <contextPath>/${project.artifactId}</contextPath>

                        <!-- Optional. Location of a jetty-env.xml file, which allows you to make JNDI bindings that
                        satisfies <env-entry>, <resource-env-ref> and <resource-ref> linkages in the web.xml. Note that
                        these can also be made in a <jettyXml> file if you want them to apply to more than one webapp.
                        確保該文件打包進(jìn)classpath中,如果把該文件放在測(cè)試目錄就不行。-->
                        <jettyEnvXml>${project.basedir}/src/main/webapp/WEB-INF/jetty-env.xml</jettyEnvXml>

                        <!--The path to a dir that Jetty can use to expand or copy jars and jsp compiles when your
                        webapp is running. The default is ${project.build.outputDirectory}/tmp-->
                        <!--<tempDirectory>${project.build.outputDirectory}/tmp</tempDirectory>-->

                        <!--the path from which Jetty will serve static resources. Defaults to src/main/webapp.-->
                        <!--<baseResource>${project.basedir}/src/main/webapp</baseResource>-->

                        <!-- use instead of <baseResource> if you have multiple dirs from which you want to serve static
                        content. This is an array of dir names.-->
                        <resourceBases>
                            <resourceBase>${project.basedir}/src/main/webapp</resourceBase>
                            <!--<resourceBase>${project.basedir}/commons</resourceBase>-->
                        </resourceBases>

                        <!--The path to the web.xml file for your webapp-->
                        <!--<descriptor>${project.basedir}/src/main/webapp/WEB-INF/web.xml</descriptor>-->

                        <!--The path to a webdefault.xml file that will be applied to your webapp before the web.xml.
                        If none is supplied, Jetty uses a default one baked into the jetty-webapp.jar.-->
                        <!--<defaultsDescriptor></defaultsDescriptor>-->

                        <!--The path to a web.xml file that will be applied after your web.xml is read.
                        You can use this to replace or add configuration.-->
                        <!--<overrideDescriptor></overrideDescriptor>-->

                        <!--Optional. Defaults to "true". Controls whether any overlaid wars are added before or after
                        the original base resource(s) of the webapp.-->
                        <!--<baseAppFirst>true</baseAppFirst>-->

                        <!--Since jetty-8.1.x. Optional. A pattern to match against the urls of the jars on the
                        container path to select those that should be examined for things like META-INF fragments,
                        resources, tlds and class inheritance hierarchy-->
                        <!--<containerIncludeJarPattern></containerIncludeJarPattern>-->

                        <!--Since jetty-8.1.6. Optional. A pattern to match against the urls of the jars considered to
                        be on the webapp's WEB-INF classpath to select those that should be examined for META-INF
                        fragments, resources, tlds, and class inheritance hierarchy. The default is to potentially
                        examine them all, subject to any ordering exclusions configured in web.xml.-->
                        <!--<webInfIncludeJarPattern></webInfIncludeJarPattern>-->
                    </webApp>

                    <!--The location of the built WAR file(jetty:run-war) or the exploded WAR(jetty:run-exploded).
                    This defaults to ${project.build.directory}/${project.build.finalName}[.war]-->
                    <war>${basedir}/target/myApp</war>
                    <!--<war>${basedir}/target/myApp.war</war>-->

                    <!--Optional. The path to a context xml file that is applied to your webapp AFTER the <webApp>.-->
                    <!--<contextXml></contextXml>-->
                </configuration>
            </plugin>
      </plugins>
  </build>

通過(guò) Spring Boot 運(yùn)行

如果你使用 Spring Boot 開(kāi)發(fā),那么就不需要使用對(duì)應(yīng)的運(yùn)行插件了。因?yàn)?Spring Boot 的 spring-boot-starter-web 依賴自帶較新版本的內(nèi)嵌 Tomcat,也可以緩存內(nèi)嵌的 Jetty、Undertower 等 Web 容器。

<dependencies>
    <!-- ... -->
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>x.x.x.RELEASE</version>
    </dependency>
    <!-- ... -->
</dependencies>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評(píng)論 6 546
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,814評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 178,980評(píng)論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 64,064評(píng)論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,779評(píng)論 6 414
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 56,109評(píng)論 1 330
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評(píng)論 3 450
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 43,287評(píng)論 0 291
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,799評(píng)論 1 338
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,515評(píng)論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,750評(píng)論 1 375
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評(píng)論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,933評(píng)論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 35,327評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 36,667評(píng)論 1 296
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,492評(píng)論 3 400
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,703評(píng)論 2 380

推薦閱讀更多精彩內(nèi)容