在 Maven 学习笔记(一) ,我们已经配置好Maven,并且已经能成功创建了简单的项目了,今天来创建一个web项目
到目前为止,我们所有的操作都还是在shell命令界面来做,先不涉及任何IDE工具。不过使用Maven来做这一切,一切都还是觉得很顺手,并没有什么麻烦。
(输入创建项目命令,我们不想一开始就输入一大堆参数,看了头晕,简单点mvn archetype:generate,等下再安提示回答) bencmai@benc-linux:~> mvn archetype:generate [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'archetype'. [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Default Project [INFO] task-segment: [archetype:generate] (aggregator-style) [INFO] ------------------------------------------------------------------------ [INFO] Preparing archetype:generate [INFO] No goals needed for project - skipping [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'. [INFO] Setting property: velocimacro.messages.on => 'false'. [INFO] Setting property: resource.loader => 'classpath'. [INFO] Setting property: resource.manager.logwhenfound => 'false'. [INFO] [archetype:generate] [INFO] Generating project in Interactive mode [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0) Choose archetype: 1: internal -> appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF) 2: internal -> appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate, Spring and Spring MVC) ...... 17: internal -> maven-archetype-site (A more complex site project) 18: internal -> maven-archetype-webapp (A simple Java web application) 19: internal -> jini-service-archetype (Archetype for Jini service project creation) 20: internal -> softeu-archetype-seam (JSF+Facelets+Seam Archetype) 21: internal -> softeu-archetype-seam-simple (JSF+Facelets+Seam (no persistence) Archetype) 22: internal -> softeu-archetype-jsf (JSF+Facelets Archetype) 23: internal -> jpa-maven-archetype (JPA application) ....... 40: internal -> gmaven-archetype-basic (Groovy basic archetype) 41: internal -> gmaven-archetype-mojo (Groovy mojo archetype) (选择我们要创建的项目类型编号,创建web项目,所以选择18) Choose a number: (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41) 15: : 18 [INFO] artifact org.apache.maven.archetypes:maven-archetype-webapp: checking for updates from central (输入Mavne 团体 坐标) Define value for groupId: : yotexs (输入Mavne 项目 坐标) Define value for artifactId: : yotexs (输入Mavne 项目版本 坐标) Define value for version: 1.0-SNAPSHOT: : (输入Mavne 项目包结构 坐标) Define value for package: yotexs: : com.yotexs Confirm properties configuration: groupId: yotexs artifactId: yotexs version: 1.0-SNAPSHOT package: com.yotexs (确认所有坐标信息--Maven的项目定位坐标由 团体groupId、项目 artifactId、包packageName、版本version唯一地确定一个工程,也是Maven仓库中定位所有依赖的要素) Y: : [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating OldArchetype: maven-archetype-webapp:RELEASE [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: yotexs [INFO] Parameter: packageName, Value: com.yotexs [INFO] Parameter: package, Value: com.yotexs [INFO] Parameter: artifactId, Value: yotexs [INFO] Parameter: basedir, Value: /home/bencmai [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] ********************* End of debug info from resources from generated POM *********************** [INFO] OldArchetype created in dir: /home/bencmai/yotexs [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1 minute 27 seconds [INFO] Finished at: Mon May 25 11:26:47 HKT 2009 [INFO] Final Memory: 17M/168M [INFO] ------------------------------------------------------------------------ (查看刚建立的web项目的目录结构) bencmai@benc-linux:~> tree yotexs yotexs |-- pom.xml `-- src `-- main |-- resources `-- webapp |-- WEB-INF | `-- web.xml `-- index.jsp 5 directories, 3 files bencmai@benc-linux:~> ls bin Desktop Documents example MyProgram public_html SoftWeave yotexs (进入我们刚建的yotexs web项目) bencmai@benc-linux:~> cd yotexs bencmai@benc-linux:~/yotexs> ls pom.xml src (用vim 编辑器编辑我们的 Maven 的配置文件,此文件作用就类似ant的build.xml,统管整个项目的构建配置) bencmai@benc-linux:~/yotexs> vim pom.xml
vim打开的项目默认 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>yotexs</groupId> <artifactId>yotexs</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>yotexs Maven Webapp</name> <url>http://maven.apache.org</url> <!--指定项目所依赖的库--> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.6</version> <!--指定junit的jar仅仅在测试时依赖,并不发布--> <scope>test</scope> </dependency> </dependencies> <build> <finalName>yotexs</finalName> </build> </project>
接着我们给 我们的Maven项目添加一些依赖的jar库配置,这个刚新建的Web项目的classpath中并没有任何依赖,除了juint,又因为这个项目仅仅是建立了目录结构,我们编译的时候还需要一些如Servlet API、Servlet JSP 等的jar
在<dependency>标签内增加Servlet的编译依赖
<dependencies> .... <!--Servlet API--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <!--范围指定为发布时容器提供,不打包--> <scope>provided</scope> </dependency> <!--Servlet JSP--> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <type>jar</type> <!--范围指定为发布时容器提供,不打包--> <scope>provided</scope> </dependency> .... </dependencies>
开发过程中,我们肯定会用如jetty活tomcat之类的容器发布并测试,因而我们添加build构建配置,在build里添加容器插件,我们这里使用jetty,让maven自动给我们发布运行
同样在刚才vim 打开的pom.xml <project>标签内部添加,与</dependency>这行同级并在其底下
<build> <finalName>yotexs</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> </plugin> </plugins> </build>
依赖配置完成,现在可以进行编码了
(新建java代码包目录-Maven默认会自动把src/main/java/下的目录当成java包和源码,编译时会自动对其进行编译) bencmai@benc-linux:~> mkdir -p src/main/java/com/yotexs/web bencmai@benc-linux:~>cd src/main/java/com/yotexs/web (用vim 新建并打开一个Servlet Java 源文件 ) bencmai@benc-linux:~/yotexs/src/main/java/com/yotexs/web>vim SimpleServlet.java
package org.sonatype.mavenbook.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class SimpleServlet */ public class SimpleServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("SimpleServlet 已经执行"); out.flush(); out.close(); } }
保存后回到项目根目录进行编译
bencmai@benc-linux:~/yotexs/src/main/java/com/yotexs/web>cd bencmai@benc-linux:~>cd yotexs bencmai@benc-linux:~/yotexs>mvn compile
如果是第一次编译,你会看到Maven会给你自动下载所有的依赖jar库,并添加到classpath中
编译好后,我们可以编辑web.xml,注册我们的Servlet 同样用 vim 编辑
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>SimpleServlet</servlet-name> <display-name>SimpleServlet</display-name> <description></description> <servlet-class>com.yotexs.web.SimpleServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SimpleServlet</servlet-name> <url-pattern>/simple</url-pattern> </servlet-mapping> </web-app>
好,一切准备就绪,可以跑起来了
因为我们前面在pom.xml中配置了Maven 的 jetty 插件,Maven知道如何发布和打包我们的web应用并发布到jetty容器中,如果你喜欢,还可以配置其他容器的插件,这点太方便了我们所要做的,就是启动 jetty容器就行
bencmai@benc-linux:~/yotexs>mvn jetty:run ........ [INFO] Context path = /yotexs [INFO] Tmp directory = determined at runtime [INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml [INFO] Web overrides = none [INFO] Webapp directory = /home/bencmai/yotexs/src/main/webapp [INFO] Starting jetty 7.0.0.pre5 ... 2009-05-25 12:39:00.756::INFO: jetty-7.0.0.pre5 2009-05-25 12:39:00.910::INFO: No Transaction manager found - if your webapp requires one, please configure one. 2009-05-25 12:39:00.486::INFO: Started [email protected]:8080 [INFO] Started Jetty Server
在另外一个shell标签里访问我们的servlet,呵呵,ok
bencmai@benc-linux:~/yotexs> curl http://localhost:8080/yotexs/simple SimpleServlet 已经执行
接这你还可以在 src/main/test/ 下写测试用例等,Maven 也会很聪明的自动给你完成所有测试,自要简单的 mvn test,一切ok。
比ant简单好用多了吧
下一节我们开始 更发杂点的多模块项目的构建