Maven Web 应用

本章节我们将学习如何使用版本控制系统 Maven 来管理一个基于 web 的项目,如何创建、构建、部署已经运行一个 web 应用。

创建 Web 应用

我们可以使用 maven-archetype-webapp 插件来创建一个简单的 Java web 应用。

打开命令控制台,进入到 /workspace/java 文件夹,然后执行以下的 mvn 命令:

$ mvn archetype:generate -DgroupId=com.companyname.automobile -DartifactId=trucks -DarchetypeArtifactId=maven-archetype-webapp  -DinteractiveMode=false

执行完后 Maven 将开始处理,并且创建完整的于Java Web 项目的目录结构。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.1:generate (default-cli) @ standalone-pom ---
[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: basedir, Value: /workspace/java
[INFO] Parameter: package, Value: com.companyname.automobile
[INFO] Parameter: groupId, Value: com.companyname.automobile
[INFO] Parameter: artifactId, Value: trucks
[INFO] Parameter: packageName, Value: com.companyname.automobile
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/liuhanzeng/workspace/java/trucks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.962 s
[INFO] Finished at: 2022-03-04T17:02:31+08:00
[INFO] ------------------------------------------------------------------------

执行完后,我们可以在 /workspace/java 文件夹下看到 trucks 项目,查看项目的目录结构:

Maven Web应用

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 文件代码如下:

<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.companyname.automobile</groupId>
   <artifactId>trucks</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>trucks Maven Webapp</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <finalName>trucks</finalName>
   </build>
</project>

接下来我们打开 /workspace/java/trucks/src/main/webapp 文件夹,可以看到一个已经创建好的 index.jsp 文件,代码如下:

<html>
   <body>
      <h2>Hello World!</h2>
   </body>
</html>

构建 Web 应用

打开命令控制台,进入 /workspace/java/trucks 目录,然后执行下面的以下 mvn 命令:

$ mvn clean package

Maven 将开始构建项目:

[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< com.companyname.automobile:trucks >------------------
[INFO] Building trucks Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ trucks ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ trucks ---
[WARNING] Using platform encoding (UTF-8 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) @ trucks ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ trucks ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /workspace/java/trucks/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ trucks ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ trucks ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ trucks ---
...
[INFO] Packaging webapp
[INFO] Assembling webapp [trucks] in [/workspace/java/trucks/target/trucks]
[INFO] Processing war project
[INFO] Copying webapp resources [/workspace/java/trucks/src/main/webapp]
[INFO] Webapp assembled in [21 msecs]
[INFO] Building war: /workspace/java/trucks/target/trucks.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.056 s
[INFO] Finished at: 2022-03-04T17:08:01+08:00
[INFO] ------------------------------------------------------------------------

部署 Web 应用

打开 /workspace/java/trucks/target 文件夹,找到 trucks.war 文件,并复制到你的 web 服务器的 web 应用目录,然后重启 web 服务器。

测试 Web 应用

访问以下 URL 运行 web 应用:

http://localhost:8080/trucks_war/

Maven Web应用验证结果

查看笔记

扫码一下
查看教程更方便