目标:使用Maven命令,创建一个web工程,该工程可以在页面上有一个连接,可以访问执行servlet。
1.创建命令:mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4
E:\Code\study\Maven\spaceVideo>mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4
[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 Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] Archetype repository not defined. Using the one from [org.apache.maven.archetypes:maven-archetype-webapp:1.0] found in catalog internal
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.4/maven-archetype-webapp-1.4.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.4/maven-archetype-webapp-1.4.pom (1.4 kB at 1.9 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bundles/1.4/maven-archetype-bundles-1.4.pom
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-bundles/1.4/maven-archetype-bundles-1.4.pom (4.5 kB at 9.5 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.4/maven-archetype-webapp-1.4.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-webapp/1.4/maven-archetype-webapp-1.4.jar (6.8 kB at 14 kB/s)
Define value for property 'groupId': com.ldsnet.maven
Define value for property 'artifactId': pro02-maven-web
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.ldsnet.maven: :
Confirm properties configuration:
groupId: com.ldsnet.maven
artifactId: pro02-maven-web
version: 1.0-SNAPSHOT
package: com.ldsnet.maven
Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-webapp:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.ldsnet.maven
[INFO] Parameter: artifactId, Value: pro02-maven-web
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.ldsnet.maven
[INFO] Parameter: packageInPathFormat, Value: com/ldsnet/maven
[INFO] Parameter: package, Value: com.ldsnet.maven
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.ldsnet.maven
[INFO] Parameter: artifactId, Value: pro02-maven-web
[INFO] Project created from Archetype in dir: E:\Code\study\Maven\spaceVideo\pro02-maven-web
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:06 min
[INFO] Finished at: 2022-10-25T06:59:08+08:00
[INFO] ------------------------------------------------------------------------
E:\Code\study\Maven\spaceVideo>
生成目录:
生成的pom文件
2.修改junit版本为4.12,并将build删除。修改后为:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.ldsnet.mavengroupId>
<artifactId>pro02-maven-webartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>pro02-maven-web Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
project>
3.补充创建目录和文件
在main目录下创建:\java\com\ldsnet\maven
创建文件:HelloServlet.java
4.编辑文件index.jsp
<html>
<body>
<h2>Hello World!</h2>
<a href="helloServlet">Access Servlet</a> //引入Servlet
</body>
</html>
5.编辑文件HttpServlet.java
package com.ldsnet.maven;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
public class HelloServlet extends HttpServlet{
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
response.getWriter().write("hello maven web");
}
}
6.在pom.xml中加入Servlet-api依赖包
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.ldsnet.mavengroupId>
<artifactId>pro02-maven-webartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>pro02-maven-web Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
dependencies>
project>
7.执行编译命令mvn compile
E:\Code\study\Maven\spaceVideo\pro02-maven-web>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.ldsnet.maven:pro02-maven-web >------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ pro02-maven-web ---
[INFO] Deleting E:\Code\study\Maven\spaceVideo\pro02-maven-web\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\Code\study\Maven\spaceVideo\pro02-maven-web\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pro02-maven-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\Code\study\Maven\spaceVideo\pro02-maven-web\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.671 s
[INFO] Finished at: 2022-10-26T06:47:27+08:00
[INFO] ------------------------------------------------------------------------
E:\Code\study\Maven\spaceVideo\pro02-maven-web>
生成文件成功。
8.执行打包操作mvn package
E:\Code\study\Maven\spaceVideo\pro02-maven-web>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.ldsnet.maven:pro02-maven-web >------------------
[INFO] Building pro02-maven-web Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ pro02-maven-web ---
[INFO] Deleting E:\Code\study\Maven\spaceVideo\pro02-maven-web\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\Code\study\Maven\spaceVideo\pro02-maven-web\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pro02-maven-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\Code\study\Maven\spaceVideo\pro02-maven-web\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pro02-maven-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\Code\study\Maven\spaceVideo\pro02-maven-web\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pro02-maven-web ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ pro02-maven-web ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ pro02-maven-web ---
Downloading from nexus-aliyun: (省略下载依赖过程)
[INFO] Packaging webapp
[INFO] Assembling webapp [pro02-maven-web] in [E:\Code\study\Maven\spaceVideo\pro02-maven-web\target\pro02-maven-web-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [E:\Code\study\Maven\spaceVideo\pro02-maven-web\src\main\webapp]
[INFO] Webapp assembled in [33 msecs]
[INFO] Building war: E:\Code\study\Maven\spaceVideo\pro02-maven-web\target\pro02-maven-web-1.0-SNAPSHOT.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.220 s
[INFO] Finished at: 2022-10-26T07:03:38+08:00
[INFO] ------------------------------------------------------------------------
E:\Code\study\Maven\spaceVideo\pro02-maven-web>
生成WAR包:
9.将WAR包或解压包放在Tomcat的运行目录下运行测试结果
运行tomcat后打开页面:
点击Access Servlet
至此,这个实验成功完成!