zoukankan      html  css  js  c++  java
  • 使用Maven构建Web项目的目录结构

    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:myspacesworktoolsapache-tomcat-7.0.37</home> 
    9.         </container> 
    10.         <configuration> 
    11.             <!-- standalone模式部署应用至本地Web容器 --> 
    12.             <!--  
    13.             <type>standalone</type>  
    14.             <home>${project.build.directory} omcat7x</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:myspacesWorkToolsapache-tomcat-7.0.37</home>  
    25.         </configuration> 
    26.     </configuration> 
    27. </plugin> 

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

        运行命令:mvn cargo:start

    (2)部署至远程Web容器

        首先修改tomcatconf omcat-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

  • 相关阅读:
    C++学习9 this指针详解
    福建省第八届 Triangles
    UVA 11584 Partitioning by Palindromes
    POJ 2752 Seek the Name, Seek the Fame
    UVA 11437 Triangle Fun
    UVA 11488 Hyper Prefix Sets (字典树)
    HDU 2988 Dark roads(kruskal模板题)
    HDU 1385 Minimum Transport Cost
    HDU 2112 HDU Today
    HDU 1548 A strange lift(最短路&&bfs)
  • 原文地址:https://www.cnblogs.com/wych/p/4119195.html
Copyright © 2011-2022 走看看