- Maven教程
- Maven - 主页
- Maven - 概述
- Maven - 环境设置
- Maven-POM
- Maven - 构建生命周期
- Maven - 构建配置文件
- Maven - 存储库
- Maven - 插件
- Maven - 创建项目
- Maven - 构建和测试项目
- Maven - 外部依赖项
- Maven - 项目文档
- Maven - 项目模板
- Maven - 快照
- Maven - 构建自动化
- Maven - 管理依赖关系
- Maven - 部署自动化
- Maven - Web 应用程序
- Maven - Eclipse IDE
- Maven - NetBeans
- Maven-IntelliJ IDEA
- Maven 有用资源
- Maven - 问题与解答
- Maven - 快速指南
- Maven - 有用的资源
- Maven - 讨论
Maven - Web 应用程序
本章教您如何使用Maven管理基于 Web 的项目。在这里您将学习如何创建/构建/部署和运行 Web 应用程序。
创建网络应用程序
要创建一个简单的 Java Web 应用程序,我们将使用maven-archetype-webapp插件。那么,让我们打开命令控制台,进入C:\MVN目录并执行以下mvn命令。
C:\MVN>mvn archetype:generate -DgroupId = com.companyname.automobile -DartifactId = trucks -DarchetypeArtifactId = maven-archetype-webapp -DinteractiveMode = false
Maven 将开始处理并创建完整的基于 Web 的 java 应用程序项目结构,如下所示 -
C:\MVN>mvn archetype:generate -DgroupId=com.companyname.automobile -DartifactId=trucks -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false [INFO] Scanning for projects... [INFO] [INFO] ------------------< org.apache.maven:standalone-pom >------------------- [INFO] Building Maven Stub Project (No POM) 1 [INFO] --------------------------------[ pom ]--------------------------------- ... [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: basedir, Value: C:\MVN [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: C:\MVN\trucks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.381 s [INFO] Finished at: 2021-12-13T19:00:13+05:30 [INFO] ------------------------------------------------------------------------ C:\MVN>
现在转到 C:/MVN 目录。您将看到一个已创建的 Java 应用程序项目,名为 Trucks(在artifactId 中指定),如以下快照中所指定。以下目录结构通常用于 Web 应用程序 -
Maven 使用标准目录布局。使用上面的例子,我们可以理解以下关键概念 -
先生。 | 文件夹结构和说明 |
---|---|
1 | 卡车 包含 src 文件夹和 pom.xml。 |
2 | src/主/网络应用程序 包含index.jsp和WEB-INF文件夹。 |
3 | src/main/webapp/WEB-INF 包含 web.xml |
4 | src/主/资源 它包含图像/属性文件。 |
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>
如果您观察,您会发现 Maven 还创建了一个示例 JSP 源文件。
使用以下代码打开C:\ > MVN > Trucks > src > main > webapp >文件夹以查看 index.jsp -
<html> <body> <h2>Hello World!</h2> </body> </html>
构建网络应用程序
我们打开命令控制台,进入C:\MVN\trucks目录并执行以下mvn命令。
C:\MVN\trucks>mvn clean package
Maven 将开始构建项目。
C:\MVN\trucks>mvn clean package [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] Deleting C:\MVN\trucks\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ trucks --- [WARNING] Using platform encoding (Cp1252 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 (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\MVN\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 --- WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/Users/intel/.m2/repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar) to field java.util.Properties.defaults WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release [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 [50 msecs] [INFO] Building war: C:\MVN\trucks\target\trucks.war [INFO] WEB-INF\web.xml already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.494 s [INFO] Finished at: 2021-12-13T19:02:15+05:30 [INFO] ------------------------------------------------------------------------ C:\MVN\trucks>
部署网络应用程序
现在将C:\ > MVN > Trucks > target >文件夹中创建的Trucks.war复制到您的网络服务器 webapp 目录并重新启动网络服务器。
测试网络应用程序
使用 URL 运行 Web 应用程序:http://<server-name>:<port-number>/trucks/index.jsp。
验证输出。