前面讲解了使用maven创建java模块工程,本节讲述使用maven创建web工程项目。
1,使用eclipse快速向导,新建maven的web工程,如下:
2,浏览新建的web工程,发明目录结构缺失,不符合maven的默认结构,故要补充添加缺失的目录。但在实际应用的过程中发现,src/main/java目录添加不进到工程中,
此时我们再浏览工程的classpath,发现新建项目的时候,这些缺失目录已经在classpath中,故想要添加缺失目录到工程,需要把项目工程切换到Navigator视图。
快速向导生成的默认目录:
下面发现缺失的目录已经存在classpath中
补充完目录后结构(可能修改后不能立马出现,这时需要项目鼠标右键->maven->update project)
3,打包项目
项目工程鼠标右键->Run AS -> Maven build - > 输入"clean install", 执行命令成功后,如下,target目录下多一个war包,控制台输出打包成功。
4, 项目部署,我们这里使用jetty服务插件,作为服务容器。在pom.xml修改如下:
<build>
<finalName>websample</finalName>
<!-- 添加jetty插件作为服务容器 -->
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
</plugin>
</plugins>
</build>
在浏览器输入地址http://localhost:8080/ ,访问发现部署成功
5, 需要在打包完成后,自动运行jetty服务,需要修改pom.xml文件,如下:
<build>
<finalName>websample</finalName>
<!-- 添加jetty插件作为服务容器 -->
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<!-- 打包完后自动运行jetty服务 -->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
项目工程鼠标右键->Run AS -> Maven build - > 输入"clean package", 执行命令成功后,控制台输出打包成功同时jetty服务启动成功。
再次访问地址http://localhost:8080/ ,访问发现部署也成功,
控制台输出:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building websample Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ websample ---
[INFO] Deleting E:\maven\demo\maven10-wk\websample\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ websample ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ websample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ websample ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ websample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ websample ---
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ websample ---
[INFO] Packaging webapp
[INFO] Assembling webapp [websample] in [E:\maven\demo\maven10-wk\websample\target\websample]
[INFO] Processing war project
[INFO] Copying webapp resources [E:\maven\demo\maven10-wk\websample\src\main\webapp]
[INFO] Webapp assembled in [27 msecs]
[INFO] Building war: E:\maven\demo\maven10-wk\websample\target\websample.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO]
[INFO] >>> jetty-maven-plugin:8.1.16.v20140903:run (default) > test-compile @ websample >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ websample ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ websample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ websample ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ websample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< jetty-maven-plugin:8.1.16.v20140903:run (default) < test-compile @ websample <<<
[INFO]
[INFO] --- jetty-maven-plugin:8.1.16.v20140903:run (default) @ websample ---
[INFO] Configuring Jetty for project: websample Maven Webapp
[INFO] webAppSourceDirectory not set. Defaulting to E:\maven\demo\maven10-wk\websample\src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = E:\maven\demo\maven10-wk\websample\target\classes
[INFO] Context path = /
[INFO] Tmp directory = E:\maven\demo\maven10-wk\websample\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = file:/E:/maven/demo/maven10-wk/websample/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = E:\maven\demo\maven10-wk\websample\src\main\webapp
[INFO] jetty-8.1.16.v20140903
[INFO] No Transaction manager found - if your webapp requires one, please configure one.
[WARNING] !RequestLog
[INFO] Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
由以上步骤,成功创建、打包、部署maven下的web项目