使用maven构建web项目实例

本文介绍如何使用Maven构建Web项目,包括目录结构、配置文件、打包方式、依赖管理等核心内容。同时演示了使用Jetty插件及Cargo进行自动化部署的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用maven构建web项目,首先要知道标准的目录结构,和一般的maven项目相同,源文件存放在src/main/java中,配置文件存在src/main/resources目录下。测试的代码放在src/test/java下,
对应的资源文件放在src/test/resources目录下。除了这些目录外。web项目还有一个src/main/webapp目录,该目录必须存在,且必须有一个web.xml文件,用于对整个web项目的配置。
如maven-web-demo这个项目实例。该项目的目录结构如下图所示:


[size=medium]为了web项目的部署,该项目的打包方式必须显示声明为war方式,因为maven默认的打包方式为jar。
还有pom文件中必须引入servlet,jsp的相关jar包,scope设置为provided,表示它们最终不会打包到war项目中。因为几乎所有的web容器都提供有javax.servlet相关的jar包,如果war包中重复出现
就会出现版本冲突的错误。




为了测试web项目,可以使用jetty插件,需要在pom文件中给出相应的配置。[/size
Xml代码  收藏代码
  1. ]<plugin>  
  2.             <groupId>org.mortbay.jetty</groupId>  
  3.             <artifactId>maven-jetty-plugin</artifactId>  
  4.             <version>6.1.26</version>  
  5.             <configuration>  
  6.             <scanIntervalSeconds>10</scanIntervalSeconds>  
  7.             <webAppConfig>  
  8.             <contextPath>/test</contextPath>  <!-- http://host:port/test/ -->  
  9.             </webAppConfig>  
  10.             </configuration>  
  11.               
  12.         </plugin>  

contextPath用于配置url的路径,该实例访问的url为http://host:port/test/.
为了能在命令行上启动jetty,并部署web项目,必须配置maven的settings.xml。添加如下语句即可。
<pluginGroup>org.mortbay.jetty</pluginGroup>

在命令行下输入mvn jetty:run启动并部署web项目。然后在浏览器中即可访问。


整个maven配置文件pom.xml如下:
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.juvenxu.mvnbook.account</groupId>  
  5.     <artifactId>maven-web-demo</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>war</packaging>  
  8.     <build>  
  9.         <plugins>  
  10.             <plugin>  
  11.                 <groupId>org.mortbay.jetty</groupId>  
  12.                 <artifactId>maven-jetty-plugin</artifactId>  
  13.                 <version>6.1.26</version>  
  14.                 <configuration>  
  15.                     <scanIntervalSeconds>10</scanIntervalSeconds>  
  16.                     <webAppConfig>  
  17.                         <contextPath>/test</contextPath>  <!-- http://host:port/test/ -->  
  18.                     </webAppConfig>  
  19.                 </configuration>  
  20.   
  21.             </plugin>  
  22.         </plugins>  
  23.     </build>  
  24.     <dependencies>  
  25.         <dependency>  
  26.             <groupId>javax.servlet</groupId>  
  27.             <artifactId>servlet-api</artifactId>  
  28.             <version>2.4</version>  
  29.             <scope>provided</scope>  
  30.         </dependency>  
  31.   
  32.         <dependency>  
  33.             <groupId>javax.servlet</groupId>  
  34.             <artifactId>jsp-api </artifactId>  
  35.             <version>2.0</version>  
  36.             <scope>provided</scope>  
  37.         </dependency>  
  38.         <dependency>  
  39.             <groupId>  org.springframework </groupId>  
  40.             <artifactId>spring-web</artifactId>  
  41.             <version>2.5.6</version>  
  42.         </dependency>  
  43.   
  44.     </dependencies>  
  45. </project>  


除了用jetty测试,还可以使用cargo进行自动化部署,如部署到本地的web容器中,主要是在pom文件中进行配置。内容如下:
Xml代码  收藏代码
  1. <plugin>  
  2.                 <groupId>org.codehaus.cargo</groupId>  
  3.                 <artifactId>cargo-maven2-plugin</artifactId>  
  4.                 <version>1.0</version>  
  5.                 <configuration>  
  6.                     <container>  
  7.                         <containerId>Tomcat6x</containerId>  
  8.                         <home>D:\software installs\Tomcat6.0</home>  
  9.                     </container>  
  10.                     <configuration>  
  11.                         <type>standalone</type>  
  12.                         <home>${project.build.directory}/Tomcat6x</home>  
  13.                         <properties>  
  14.                         <cargo.servlet.port>8080</cargo.servlet.port>  
  15.                         </properties>  
  16.                     </configuration>  
  17.                 </configuration>  
  18.             </plugin> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值