您的位置:首页 > 其它

使用Maven构建Web项目的目录结构

2015-12-03 17:41 465 查看
转载自:http://www.myext.cn/webkf/a_11929.html

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: