標籤:http io ar os 使用 sp java 檔案 資料
1.Web項目的目錄結構
基於Java的Web項目,標準的打包方式是WAR。與JAR比較,包含更多的內容,比如JSP檔案、Servlet、Java類、web.xml設定檔、依賴JAR包、靜態web資源(HTML、CSS、JavaScript)等。
一個典型的WAR檔案如下目錄結構:
File-system代碼
- —war/
- + META-INF/
- + WEB-INF/
- | + classes/
- | | + ServletA.class
- | | + config.properties
- | | + ...
- | + web.xml
- + img/
- + css/
- + js/
- + index.html
- + sample.jsp
一個WAR包下至少包含兩個子目錄:META-INF和WEB-INF,前者包含了一些打包中繼資料資訊;後者是WAR包的核心,WEB-INF下必須包含一個Web資源描述檔案web.xml,它的子目錄classes包含所有該Web項目的類,而另一個子目錄lib則包含所有該Web項目的依賴JAR包。classes和lib目錄都會在啟動並執行時候被加入到Classpath中。除此之外,WAR包中會包含很多Web資源,比如html、jsp、圖片等。
Maven對Web項目的布局結構也有一個通用的約定,Web項目必須顯示的指定打包方式為war
Xml代碼
- <project>
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.gqshao.myapp</groupId>
- <artifactId>project-a</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>project-a</name>
- </project>
Web項目的類及資源檔同一般JAR項目一樣,預設位置都是src/main/java和src/main/resources
Web項目比較特殊的地方在於:它還有一個Web資來源目錄,其預設位置是src/main/webapp/。一個典型Web項目的Maven目錄結構如下:
File-system代碼
- + project
- + pom.xml
- + src
- | + main/
- | | + java/
- | | | + ServletA.class
- | | | ...
- | | + ...
- | + resources/
- | | + config.properties
- | + webapp/
- | | + WEB-INF/
- | | | + web.xml/
- | | + img/
- | | + css/
- | | + js/
- | | + index.html/
- | | + sample.jsp/
- | + test/
- | | + java/
- | + resources
在src/main/webapp/目錄下,必須包含一個子目錄WEB-INF,該子目錄還必須要包含web.xml檔案
2. web項目的POM
Xml代碼
- <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.gqshao.myapp</groupId>
- <artifactId>learn-maven</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>myapp.learn_maven Maven Webapp</name>
- <properties>
- <springframework.version>3.2.2.RELEASE</springframework.version>
- <junit.version>4.7</junit.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.4</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.0</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- <build>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- </resource>
- </resources>
- <finalName>test</finalName>
- <plugins>
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <artifactId>jetty-maven-plugin</artifactId>
- <version>8.1.10.v20130312</version>
- <configuration>
- <scanIntervalSeconds>10</scanIntervalSeconds>
- <webAppConfig>
- <contextPath>/test</contextPath>
- </webAppConfig>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
(1)packaging元素值為war
(2)幾乎所有的Web都要依賴於servlet-api和jsp-api,但這兩個依賴範圍是provided,表示他們最終不會被打包至war檔案中,因為web容器會提供這兩個類庫。
(3)build-finalName:用來標識項目產生的主構件的名稱,改變該元素的預設值,方便部署。
3.使用jetty-maven-plugin進行測試
Web頁面測試應該僅限於頁面的層次,例如JSP、CSS、JavaScript的修改,其他代碼的修改(如資料訪問)、請編寫單元測試。
傳統的Web測試方法要求我們編譯、測試、打包及部署,jetty-maven-plugin能夠協助我們節省時間,它能夠周期性的檢查項目內容,發現變更後自動更新到內建的Jetty Web容器中,也就是幫我們省去了打包和部署的步驟。通常情況下,我們只需要在IDE中修改源碼,IDE能夠執行自動編譯,jetty-maven-plugin發現編譯後的檔案變化後,自動更新到Jetty容器,就可以直接測試Web頁面了。
jetty-maven-plugin配置
Xml代碼
- <build>
- <plugins>
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <artifactId>jetty-maven-plugin</artifactId>
- <version>8.1.10.v20130312</version>
- <configuration>
- <scanIntervalSeconds>10</scanIntervalSeconds>
- <webAppConfig>
- <contextPath>/test</contextPath>
- </webAppConfig>
- </configuration>
- </plugin>
- </plugins>
- </build>
注意:
(1)jetty-maven-plugin不是官方Maven外掛程式,需要注意groupId和artifactId;scanIntervalSeconds表示該外掛程式掃描項目變更的時間間隔,單位是秒;contextPath表示項目部署後的content path。http://hostname:port/{contextPath}/
(2)預設情況下,只有org.apache.maven.plugins和org.codehaus.mojo兩個groupId下的外掛程式才支援簡化的命令列調用,即可以運行mvn help:system。為了能在命令列直接運行mvn jetty:run,使用者需要配置setting.xml如下:
Xml代碼
- <settings>
- <pluginGroups>
- <pluginGroup>org.mortbay.jetty</pluginGroup>
- <!-- 8、9 -->
- <pluginGroup>org.eclipse.jetty</pluginGroup>
- </pluginGroups>
- </settings>
如果希望使用其它連接埠,可以添加jerry.port參數;跳過測試為maven.test.skip
Dos代碼
- mvn jetty:run -Djetty.port=80 -Dmaven:test:skip=true
4.使用Cargo實現自動化部署
Cargo是一組協助使用者操作Web容器的工具,它能夠協助使用者實現自動化部署,而且它幾乎支援所有Web容器,如Tomcat、JBoss、Jetty和Glassfish等。Cargo通過cargo-maven2-plugin提供了Maven繼承,Maven使用者可以使用該外掛程式將Web項目部署到Web容器中。
cargo-maven2-plugin和jetty-maven-plugin的功能看起來很相似,但目的不同。jetty-maven-plugin主要是協助日常的快速開發與測試,而cargo-maven2-plugin主要服務於自動化部署。
(1)部署至本地Web容器
Cargo支援兩種本地部署的方式,分別為standalone模式和existing模式。在standalone模式中,Cargo會從Web容器的安裝目錄複寫一份配置到使用者指定的目錄,然後在此基礎上部署應用,每次重新構建的時候,這個目錄都會被清空,所有配置重建;existing模式中,使用者需要指定現有的Web容器配置目錄,然後Cargo會直接使用這些配置並將應用部署到其對應的位置。
standalone/模式部署應用至本地Web容器
Java代碼
- <plugin>
- <groupId>org.codehaus.cargo</groupId>
- <artifactId>cargo-maven2-plugin</artifactId>
- <version>1.4.0</version>
- <configuration>
- <container>
- <containerId>tomcat7x</containerId>
- <home>D:\myspaces\worktools\apache-tomcat-7.0.37</home>
- </container>
- <configuration>
- <!-- standalone模式部署應用至本地Web容器 -->
- <!--
- <type>standalone</type>
- <home>${project.build.directory}\tomcat7x</home>
- -->
- <!-- cargo.servlet.port屬性修改監聽連接埠 -->
- <!--
- <properties>
- <cargo.servlet.port>80<cargo.servlet.port>
- </properties>
- -->
- <!-- existing模式部署應用至本地Web容器 -->
- <type>existing</type>
- <home>D:\myspaces\WorkTools\apache-tomcat-7.0.37</home>
- </configuration>
- </configuration>
- </plugin>
在setting.xml中添加<pluginGroup>org.codehaus.cargo</pluginGroup>
運行命令:mvn cargo:start
(2)部署至遠程Web容器
首先修改tomcat\conf\tomcat-users.xml中添加使用者
Xml代碼
- <role rolename="manager-gui"/>
- <user username="tomcat" password="tomcat" roles="manager-gui"/>
POM配置如下
Xml代碼
- <plugin>
- <groupId>org.codehaus.cargo</groupId>
- <artifactId>cargo-maven2-plugin</artifactId>
- <version>1.4.0</version>
- <configuration>
- <container>
- <containerId>tomcat7x</containerId>
- <type>remote</type>
- </container>
- <configuration>
- <type>runtime</type>
- <properties>
- <cargo.remote.username>tomcat</cargo.remote.username>
- <cargo.remote.password>tomcat</cargo.remote.password>
- <cargo.remote.manager.url>http://localhost/manager</cargo.remote.manager.url>
- </properties>
- </configuration>
- </configuration>
- </plugin>
(1)遠程部署container元素的type子項目的值必須為remote。否則預設的值為installed,並尋找對應的容器安裝目錄或者安裝包,對於遠程部署來說安裝目錄或者安裝包是不需要的。
(2)configuration的type子項目值為runtime,表示既不使用獨立的容器配置,也不使用本地現有的容器配置,而是依賴於一個已啟動並執行容易;properties聲明一些容器熱部署的相關配置。
(3)運行命令 mvn cargo:redeploy
使用Maven構建Web項目的目錄結構