Maven - Web 应用

Maven - Web 应用

此篇教程将教给你如何使用版本控制系统 Maven 来管理一个基于 web 的项目。这里,你将学到怎样创建、构建、部署已经运行一个 web 应用。

创建 Web 应用

要创建一个简单的 Java web 应用,我们将使用 maven-archetype-webapp 插件。那么,让我们打开命令控制台,去到 C:\MVN directory 目录,然后执行项目的 mvn 命令。d

C:\MVN>mvn archetype:generate

-DgroupId=com.companyname.automobile

-DartifactId=trucks

-DarchetypeArtifactId=maven-archetype-webapp

-DinteractiveMode=false

Maven 将开始处理,并且创建完整的基于 web 的 Java 应用项目的目录结构。

[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] [archetype:generate {execution: default-cli}]

[INFO] Generating project in Batch mode

[INFO] --------------------------------------------------------------------

[INFO] Using following parameters for creating project

from Old (1.x) Archetype: maven-archetype-webapp:1.0

[INFO] --------------------------------------------------------------------

[INFO] Parameter: groupId, Value: com.companyname.automobile

[INFO] Parameter: packageName, Value: com.companyname.automobile

[INFO] Parameter: package, Value: com.companyname.automobile

[INFO] Parameter: artifactId, Value: trucks

[INFO] Parameter: basedir, Value: C:\MVN

[INFO] Parameter: version, Value: 1.0-SNAPSHOT

[INFO] project created from Old (1.x) Archetype in dir: C:\MVN\trucks

[INFO] -------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] -------------------------------------------------------------------

[INFO] Total time: 16 seconds

[INFO] Finished at: Tue Jul 17 11:00:00 IST 2012

[INFO] Final Memory: 20M/89M

[INFO] -------------------------------------------------------------------

现在去到 C:/MVN 目录下, 你将看到一个创建好的名为 trucks (如 artifactId 指定的一样) 的 Java 应用项目。

Maven 使用一套标准的目录结构。借助上面的例子,我们可以理解下面几个关键概念:

文件夹结构

描述

trucks

包含 src 文件夹和 pom.xml 文件。

src/main/webapp

包含 index.jsp 文件和 WEB-INF 文件夹.

src/main/webapp/WEB-INF

包含 web.xml 文件

src/main/resources

包含图片、properties资源文件。

POM.xml 文件

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">

4.0.0

com.companyname.automobile

trucks

war

1.0-SNAPSHOT

trucks Maven Webapp

http://maven.apache.org

junit

junit

3.8.1

test

trucks

如你所见,Maven 也创建好了一个 JSP 样例源文件。

打开 C:\ > MVN > trucks > src > main > webapp > 文件夹,你将看到 index.jsp 文件。

Hello World!

构建 Web 应用

让我们打开命令控制台,去到 C:\MVN\trucks 目录,然后执行下面的 mvn 命令。

C:\MVN\trucks>mvn clean package

Maven 将开始构建项目。

[INFO] Scanning for projects...

[INFO] -------------------------------------------------------------------

[INFO] Building trucks Maven Webapp

[INFO] task-segment: [clean, package]

[INFO] -------------------------------------------------------------------

[INFO] [clean:clean {execution: default-clean}]

[INFO] [resources:resources {execution: default-resources}]

[WARNING] Using platform encoding (Cp1252 actually) to

copy filtered resources,i.e. build is platform dependent!

[INFO] Copying 0 resource

[INFO] [compiler:compile {execution: default-compile}]

[INFO] No sources to compile

[INFO] [resources:testResources {execution: default-testResources}]

[WARNING] Using platform encoding (Cp1252 actually) to

copy filtered resources,i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory

C:\MVN\trucks\src\test\resources

[INFO] [compiler:testCompile {execution: default-testCompile}]

[INFO] No sources to compile

[INFO] [surefire:test {execution: default-test}]

[INFO] No tests to run.

[INFO] [war:war {execution: default-war}]

[INFO] Packaging webapp

[INFO] Assembling webapp[trucks] in [C:\MVN\trucks\target\trucks]

[INFO] Processing war project

[INFO] Copying webapp resources[C:\MVN\trucks\src\main\webapp]

[INFO] Webapp assembled in[77 msecs]

[INFO] Building war: C:\MVN\trucks\target\trucks.war

[INFO] -------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] -------------------------------------------------------------------

[INFO] Total time: 3 seconds

[INFO] Finished at: Tue Jul 17 11:22:45 IST 2012

[INFO] Final Memory: 11M/85M

[INFO] -------------------------------------------------------------------

部署 Web 应用

现在复制 C:\ > MVN > trucks > target > 文件夹下创建好的 trucks.war 文件到你的 web 服务器的 web 应用目录,然后重启 web 服务器。

测试 Web 应用

访问下面的 URL 来运行 web 应用。 http://:/trucks/index.jsp

验证结果。