• Maven通过命令创建Web工程操作实例


    目标:使用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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    生成目录:
    在这里插入图片描述
    生成的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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    生成文件成功。
    在这里插入图片描述

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    生成WAR包:
    在这里插入图片描述

    9.将WAR包或解压包放在Tomcat的运行目录下运行测试结果
    在这里插入图片描述
    运行tomcat后打开页面:
    在这里插入图片描述
    点击Access Servlet
    在这里插入图片描述

    至此,这个实验成功完成!

  • 相关阅读:
    供电企业的福音,远程抄表系统无线解决方案,让工程师告别奔波
    Go语言-华山论剑
    【ROS2原理8】节点到参与者的重映射
    拓端tecdat|GIS遥感数据可视化评估:印度河流域上部的积雪面积变化
    武汉凯迪正大—全自动变比测试仪
    NFTScan | 09.04~09.10 NFT 市场热点汇总
    Windows 构建 Acid Game Engine 的坑
    读书记:认知觉醒(三)元认知、自控力
    自学Python 46 日期和时间函数(一)
    docker 安装 RabbitMQ & 安装 rabbitmq_delayed_message_exchange插件
  • 原文地址:https://blog.csdn.net/weixin_42844704/article/details/127506015