Maven项目管理工具:Maven创建Web项目
到目前为止,我们所接触的Maven项目的打包类型只有JAR和POM,但在现今的互联网时代,我们创建的大部分应用程序都是Web应用,在Java中Web应用的打包方式是WAR。
本节我们将介绍如何使用Maven创建、构建、部署和运行Web应用程序。
创建Web应用
通过使用Maven的maven-archetype-webapp模板可以创建一个简单的Web应用。
例如,在命令行窗口执行以下命令,Maven会为我们创建一个JavaWeb应用。
mvn archetype:generate -DgroupId=net.biancheng.www -DartifactId=mavenWeb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false1复制代码类型:[java]
执行结果如下。
[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.0:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] [INFO] --- maven-archetype-plugin:3.2.0: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: D:\MavenWeb [INFO] Parameter: package, Value: net.biancheng.www [INFO] Parameter: groupId, Value: net.biancheng.www [INFO] Parameter: artifactId, Value: mavenWeb [INFO] Parameter: packageName, Value: net.biancheng.www [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: D:\MavenWeb\mavenWeb [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.780 s [INFO] Finished at: 2021-03-29T10:23:18+08:00 [INFO] ------------------------------------------------------------------------1234567891011121314151617181920212223242526272829复制代码类型:[java]
命令执行完成后,Maven为我们创建了一个名为mavenWeb的JavaWeb应用,其目录结构如图所示。
其中,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>net.biancheng.www</groupId> <artifactId>mavenWeb</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>mavenWeb 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>mavenWeb</finalName> </build> </project>123456789101112131415161718192021复制代码类型:[java]
index.jsp代码如下。
<html> <body> <h2>Hello World!</h2> </body> </html>12345复制代码类型:[java]
打包Web应用
Web应用创建完成后,接下来我们将该应用进行打包成war文件,以便将其部署到服务器中。
打开命令行窗口,跳转到mavenWeb的目录,执行以下mvn命令,对该Web应用进行打包。
mvn clean package1复制代码类型:[java]
结果如下。
[INFO] Scanning for projects... [INFO] [INFO] ---------------------< net.biancheng.www:mavenWeb >--------------------- [INFO] Building mavenWeb Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mavenWeb --- [INFO] Deleting D:\MavenWeb\mavenWeb\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenWeb --- [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) @ mavenWeb --- [INFO] No sources to compile [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenWeb --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\MavenWeb\mavenWeb\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavenWeb --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenWeb --- [INFO] No tests to run. [INFO] [INFO] --- maven-war-plugin:2.2:war (default-war) @ mavenWeb --- [INFO] Packaging webapp [INFO] Assembling webapp [mavenWeb] in [D:\MavenWeb\mavenWeb\target\mavenWeb] [INFO] Processing war project [INFO] Copying webapp resources [D:\MavenWeb\mavenWeb\src\main\webapp] [INFO] Webapp assembled in [34 msecs] [INFO] Building war: D:\MavenWeb\mavenWeb\target\mavenWeb.war [INFO] WEB-INF\web.xml already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.001 s [INFO] Finished at: 2021-03-29T12:37:37+08:00 [INFO] ------------------------------------------------------------------------12345678910111213141516171819202122232425262728293031323334353637383940复制代码类型:[java]
打包完成后,进入D:\MavenWeb\mavenWeb\target目录,可以看到该应用打包生成的war文件,如图所示。
部署Web应用
将mavenWeb的war文件复制到Tomcat服务器的webapp目录中,然后重新启动Tomcat服务器。
Tomcat启动完成后,在浏览器地址栏输入“http://localhost:8080/mavenWeb/index.jsp”,结果如图所示。
赞 (0)