利用Maven工程命令行学习实操<第三课>(一)

简介: 利用Maven工程命令行学习实操<第三课>(一)

第一部分:创建Maven中的Web工程

1 Web工程的目录如图所示:

2 说明

使用 mvn archetype:generate 命令生成 Web 工程时,需要使用一个专门的 archetype。这个专门生成 Web 工程骨架的 archetype 可以参照官网看到它的用法:

参数 archetypeGroupId、archetypeArtifactId、archetypeVersion 用来指定现在使用的 maven-archetype-webapp 的坐标

3 操作的命令行命令是

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4

4  win+r输入Cmd enter 切换到运行项目的路径执行上面的代码

Microsoft Windows [版本 10.0.22000.856]
(c) Microsoft Corporation。保留所有权利。
C:\Users\MZFAITHDREAM>d:
'd:' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
C:\Users\MZFAITHDREAM>d:
D:\>cd Maven_WoekSpaces\SpaceVideo
D:\Maven_WoekSpaces\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.8 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 11 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':

5 web项目构建运行成功的cmd命令窗口

Define value for property 'groupId': com.web.maven
Define value for property 'artifactId': pros-web01-maven
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.web.maven: :
Confirm properties configuration:
groupId: com.web.maven
artifactId: pros-web01-maven
version: 1.0-SNAPSHOT
package: com.web.maven
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-webapp:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.web.maven
[INFO] Parameter: artifactId, Value: pros-web01-maven
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.web.maven
[INFO] Parameter: packageInPathFormat, Value: com/web/maven
[INFO] Parameter: package, Value: com.web.maven
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.web.maven
[INFO] Parameter: artifactId, Value: pros-web01-maven
[INFO] Project created from Archetype in dir: D:\Maven_WoekSpaces\SpaceVideo\pros-web01-maven
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:30 min
[INFO] Finished at: 2022-09-01T09:56:24+08:00
[INFO] -------------------------------------

6  观察下面的代码

Define value for property 'groupId': com.web.maven
Define value for property 'artifactId': pros-web01-maven
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.web.maven: :

7 生成的pom.xml 解析pom.xml文件 注释在下面有介绍在这里不在说明

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<!--Maven的坐标 -->
  <groupId>com.web.maven</groupId>
  <artifactId>pros-web01-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
<!-- -->
<!--打包的方式是War包 -->
  <packaging>war</packaging>
  <name>pros-web01-maven Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
<!--源码的字符集 -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<!-- 配置对Java工程maventest的依赖 -->
<!-- 具体的配置方式:在dependency标签内使用坐标实现依赖 -->
<dependency>
  <groupId>com.efg.maven</groupId>
  <artifactId>maventest</artifactId>
  <version>1.0-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>
  </dependencies>
  <build>
    <finalName>pros-web01-maven</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

8 在自己创建maven-web项目中创建下面项目的信息

①在 main 目录下创建 java 目录

②在 java 目录下创建 Servlet 类所在的包的目录

③在包下创建 Servlet 类

9 创建Servicet类

package com.web.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");
}
}

10 注册Service

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
 <servlet>
  <servlet-name>helloServlet</servlet-name>
  <servlet-class>com.web.maven.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>helloServlet</servlet-name>
  <url-pattern>/helloServlet</url-pattern>
  </servlet-mapping>
</web-app>

11  在 index.jsp 页面编写超链接

<html>
<body>
<h2>Hello World!</h2>
<a href="helloServlet">Access Servlet</a>
</body>
</html>

12 编译程序

此时直接执行 mvn compile 命令出错:
DANGER
程序包 javax.servlet.http 不存在
程序包 javax.servlet 不存在
找不到符号
符号: 类 HttpServlet
……
上面的错误信息说明:我们的 Web 工程用到了 HttpServlet 这个类,而 HttpServlet 这个类属于 servlet-api.jar 这个 jar 包。此时我们说,Web 工程需要依赖 servlet-api.jar 包。

13  配置对 servlet-api.jar 包的依赖

对于不知道详细信息的依赖可以到https://mvnrepository.com/网站查询。使用关键词搜索,然后在搜索结果列表中选择适合的使用。

 

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

14 运行 mvn package 命令,生成 war 包的位置如下图所示:

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<!--Maven的坐标 -->
  <groupId>com.web.maven</groupId>
  <artifactId>pros-web01-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
<!-- -->
<!--打包的方式是War包 -->
  <packaging>war</packaging>
  <name>pros-web01-maven Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
<!--源码的字符集 -->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<!-- 配置对Java工程maventest的依赖 -->
<!-- 具体的配置方式:在dependency标签内使用坐标实现依赖 -->
<dependency>
  <groupId>com.efg.maven</groupId>
  <artifactId>maventest</artifactId>
  <version>1.0-SNAPSHOT</version>
    <scope>compile</scope>
</dependency>
  </dependencies>
  <build>
    <finalName>pros-web01-maven</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
D:\Maven_WoekSpaces\SpaceVideo\pros-web01-maven>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.web.maven:pros-web01-maven >-------------------
[INFO] Building pros-web01-maven Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[WARNING] The POM for com.efg.maven:maventest:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.405 s
[INFO] Finished at: 2022-09-01T22:02:53+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project pros-web01-maven: Could not resolve dependencies for project com.web.maven:pros-web01-maven:war:1.0-SNAPSHOT: Could not find artifact com.efg.maven:maventest:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
相关文章
|
7月前
|
Java 应用服务中间件 Apache
Maven程序 tomcat插件安装与web工程启动
Maven程序 tomcat插件安装与web工程启动
82 0
|
4月前
|
Java 应用服务中间件 测试技术
Maven学习笔记(一):Maven基础(基于命令行的学习和应用)
Maven 是一款 Java 项目构建工具,主要用于管理 jar 包及其依赖关系。 本文主要了解Maven基础知识及基础应用,旨在为之后的进一步学习奠定基础。 内容上几近全为学习《尚硅谷2022版Maven教程》整理所得。 仅供参考。
320 81
Maven学习笔记(一):Maven基础(基于命令行的学习和应用)
|
5月前
|
前端开发 Java 编译器
【前端学java】如何从前端视角快速学习Maven
【8月更文挑战第12天】如何从前端视角快速学习Maven
77 2
【前端学java】如何从前端视角快速学习Maven
|
6月前
|
Java Maven
maven 工程pom依赖优化及常用命令
maven 工程pom依赖优化及常用命令
90 0
|
7月前
|
Java Maven
Maven命令行参数详解
Maven命令行参数详解
252 0
|
7月前
|
SpringCloudAlibaba Java Maven
关于拆分boot工程的踩坑之maven多模块间的依赖关系处理
关于拆分boot工程的踩坑之maven多模块间的依赖关系处理
|
7月前
|
缓存 Java Maven
IDEA如何把MAVEN项目打包成jar包并且用命令行启动
IDEA如何把MAVEN项目打包成jar包并且用命令行启动
102 0
|
7月前
|
Java 应用服务中间件 API
eclipse创建 maven-web工程
eclipse创建 maven-web工程
|
2月前
|
XML Java 测试技术
从零开始学 Maven:简化 Java 项目的构建与管理
Maven 是一个由 Apache 软件基金会开发的项目管理和构建自动化工具。它主要用在 Java 项目中,但也可以用于其他类型的项目。
80 1
从零开始学 Maven:简化 Java 项目的构建与管理