使用maven构建web项目实例

来源:互联网 发布:网易imap服务器和端口 编辑:程序博客网 时间:2024/05/16 05:34
用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> 
]<plugin>  <groupId>org.mortbay.jetty</groupId>  <artifactId>maven-jetty-plugin</artifactId>  <version>6.1.26</version>  <configuration>  <scanIntervalSeconds>10</scanIntervalSeconds>  <webAppConfig>  <contextPath>/test</contextPath>  <!-- http://host:port/test/ -->  </webAppConfig>  </configuration>    </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> 
<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.juvenxu.mvnbook.account</groupId><artifactId>maven-web-demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><build><plugins><plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId><version>6.1.26</version><configuration><scanIntervalSeconds>10</scanIntervalSeconds><webAppConfig><contextPath>/test</contextPath>  <!-- http://host:port/test/ --></webAppConfig></configuration></plugin></plugins></build><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.4</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api </artifactId><version>2.0</version><scope>provided</scope></dependency><dependency><groupId>  org.springframework </groupId><artifactId>spring-web</artifactId><version>2.5.6</version></dependency></dependencies></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>