使用Maven構建Web項目的目錄結構

來源:互聯網
上載者:User

標籤: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代碼

  1. —war/ 
  2.     + META-INF/ 
  3.     + WEB-INF/ 
  4.     |  + classes/ 
  5.     |  |  + ServletA.class 
  6.     |  |  + config.properties 
  7.     |  |  + ... 
  8.     |  + web.xml 
  9.     + img/ 
  10.     + css/ 
  11.     + js/ 
  12.     + index.html 
  13.     + 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代碼

  1. <project>
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.gqshao.myapp</groupId>
  4. <artifactId>project-a</artifactId>
  5. <version>1.0-SNAPSHOT</version>
  6. <packaging>war</packaging>
  7. <name>project-a</name>
  8. </project>

    Web項目的類及資源檔同一般JAR項目一樣,預設位置都是src/main/java和src/main/resources

    Web項目比較特殊的地方在於:它還有一個Web資來源目錄,其預設位置是src/main/webapp/。一個典型Web項目的Maven目錄結構如下:

File-system代碼

  1. + project 
  2.     + pom.xml 
  3.     + src 
  4.     |  + main/ 
  5.     |  |  + java/ 
  6.     |  |  |  + ServletA.class 
  7.     |  |  |  ... 
  8.     |  |  + ... 
  9.     |  + resources/ 
  10.     |  |  + config.properties 
  11.     |  +  webapp/ 
  12.     |  |  + WEB-INF/ 
  13.     |  |  |  + web.xml/ 
  14.     |  |  + img/ 
  15.     |  |  + css/ 
  16.     |  |  + js/ 
  17.     |  |  + index.html/ 
  18.     |  |  + sample.jsp/ 
  19.     |  + test/ 
  20.     |  |  + java/ 
  21.     |  + resources 

    在src/main/webapp/目錄下,必須包含一個子目錄WEB-INF,該子目錄還必須要包含web.xml檔案

2. web項目的POM

Xml代碼

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.gqshao.myapp</groupId>
  5. <artifactId>learn-maven</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>myapp.learn_maven Maven Webapp</name>
  9. <properties>
  10. <springframework.version>3.2.2.RELEASE</springframework.version>
  11. <junit.version>4.7</junit.version>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>3.8.1</version>
  18. <scope>test</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>javax.servlet</groupId>
  22. <artifactId>servlet-api</artifactId>
  23. <version>2.4</version>
  24. <scope>provided</scope>
  25. </dependency>
  26. <dependency>
  27. <groupId>javax.servlet.jsp</groupId>
  28. <artifactId>jsp-api</artifactId>
  29. <version>2.0</version>
  30. <scope>provided</scope>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <resources>
  35. <resource>
  36. <directory>src/main/resources</directory>
  37. <filtering>true</filtering>
  38. </resource>
  39. </resources>
  40. <finalName>test</finalName>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.mortbay.jetty</groupId>
  44. <artifactId>jetty-maven-plugin</artifactId>
  45. <version>8.1.10.v20130312</version>
  46. <configuration>
  47. <scanIntervalSeconds>10</scanIntervalSeconds>
  48. <webAppConfig>
  49. <contextPath>/test</contextPath>
  50. </webAppConfig>
  51. </configuration>
  52. </plugin>
  53. </plugins>
  54. </build>
  55. </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代碼

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.mortbay.jetty</groupId>
  5. <artifactId>jetty-maven-plugin</artifactId>
  6. <version>8.1.10.v20130312</version>
  7. <configuration>
  8. <scanIntervalSeconds>10</scanIntervalSeconds>
  9. <webAppConfig>
  10. <contextPath>/test</contextPath>
  11. </webAppConfig>
  12. </configuration>
  13. </plugin>
  14. </plugins>
  15. </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代碼

  1. <settings>
  2. <pluginGroups>
  3. <pluginGroup>org.mortbay.jetty</pluginGroup>
  4. <!-- 8、9 -->
  5. <pluginGroup>org.eclipse.jetty</pluginGroup>
  6. </pluginGroups>
  7. </settings>

    如果希望使用其它連接埠,可以添加jerry.port參數;跳過測試為maven.test.skip

Dos代碼

  1. 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代碼

  1. <plugin> 
  2.     <groupId>org.codehaus.cargo</groupId> 
  3.     <artifactId>cargo-maven2-plugin</artifactId> 
  4.     <version>1.4.0</version> 
  5.     <configuration> 
  6.         <container> 
  7.             <containerId>tomcat7x</containerId> 
  8.             <home>D:\myspaces\worktools\apache-tomcat-7.0.37</home> 
  9.         </container> 
  10.         <configuration> 
  11.             <!-- standalone模式部署應用至本地Web容器 --> 
  12.             <!--  
  13.             <type>standalone</type>  
  14.             <home>${project.build.directory}\tomcat7x</home>  
  15.             --> 
  16.             <!--  cargo.servlet.port屬性修改監聽連接埠 --> 
  17.             <!--                           
  18.             <properties> 
  19.                 <cargo.servlet.port>80<cargo.servlet.port> 
  20.             </properties> 
  21.             -->           
  22.             <!-- existing模式部署應用至本地Web容器 --> 
  23.             <type>existing</type>  
  24.             <home>D:\myspaces\WorkTools\apache-tomcat-7.0.37</home>  
  25.         </configuration> 
  26.     </configuration> 
  27. </plugin> 

    在setting.xml中添加<pluginGroup>org.codehaus.cargo</pluginGroup>

    運行命令:mvn cargo:start

(2)部署至遠程Web容器

    首先修改tomcat\conf\tomcat-users.xml中添加使用者

Xml代碼

  1. <role rolename="manager-gui"/>
  2. <user username="tomcat" password="tomcat" roles="manager-gui"/>

    POM配置如下

Xml代碼

  1. <plugin>
  2. <groupId>org.codehaus.cargo</groupId>
  3. <artifactId>cargo-maven2-plugin</artifactId>
  4. <version>1.4.0</version>
  5. <configuration>
  6. <container>
  7. <containerId>tomcat7x</containerId>
  8. <type>remote</type>
  9. </container>
  10. <configuration>
  11. <type>runtime</type>
  12. <properties>
  13. <cargo.remote.username>tomcat</cargo.remote.username>
  14. <cargo.remote.password>tomcat</cargo.remote.password>
  15. <cargo.remote.manager.url>http://localhost/manager</cargo.remote.manager.url>
  16. </properties>
  17. </configuration>
  18. </configuration>
  19. </plugin>

    (1)遠程部署container元素的type子項目的值必須為remote。否則預設的值為installed,並尋找對應的容器安裝目錄或者安裝包,對於遠程部署來說安裝目錄或者安裝包是不需要的。

    (2)configuration的type子項目值為runtime,表示既不使用獨立的容器配置,也不使用本地現有的容器配置,而是依賴於一個已啟動並執行容易;properties聲明一些容器熱部署的相關配置。

    (3)運行命令 mvn cargo:redeploy

使用Maven構建Web項目的目錄結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.