Maven 初探

1. Maven 安装

  1. 进入 maven 官网,点击 download

在这里插入图片描述

  1. 单击下载

    在这里插入图片描述

  2. 解压文件到本地(注意路径不要有中文、空格)

    在这里插入图片描述

  3. 配置环境变量

    • MAVEN_HOME:解压后的文件的根目录,如 D:\maven\apache-maven-3.6.3
    • Path:添加 MAVEN_HOME\bin

    命令行输入 mvn -v,出现下图则说明安装成功

在这里插入图片描述

2. 配置本地仓库、镜像和 IDEA

  1. 新建文件夹作为本地仓库(注意不要带中文、空格)

    在这里插入图片描述

  2. 修改 maven 配置文件

    打开 maven 目录下的配置文件

    在这里插入图片描述

    找到并修改<localRepository>,最初是注释掉的,取消注释就可以,改为本地仓库的目录

    在这里插入图片描述

  3. 添加中央仓库

    一般使用阿里云镜像,在配置文件中添加添加

    <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
    

在这里插入图片描述

  1. 把刚刚修改的 maven 下的 conf/settings.xml 拷贝至你自己的本地仓库下一份,就是刚才修改的 <localRepository> 中的路径

在这里插入图片描述

  1. 在 IDEA 配置本地 maven

    在这里插入图片描述

3. 标准目录结构

项目根目录
  |--src                   
  |   |--main
  |   |   |--java           # 项目源代码
  |   |   |--resources      # 项目资源文件,如配置文件
  |   |   |--webapp         # web 工程的主目录
  |   |        |--WEB-INF
  |   |             |--web.xml
  |   |--test
  |       |--java           # 单元测试类
  |       |--resources      # 测试资源文件
  |--target                   # 代码编译后的保存位置
  |--pom.xml                  # maven 项目核心配置文件

4. Maven 常用命令

4.1 清理

mvn clean

删除根目录下 target 目录

4.2 编译

mvn compile

将项目中 .java 文件编译成 .class 文件

4.3 单元测试

mvn test

将项目根目录下 test/java 文件夹里的单元测试类都执行

4.4 打包

mvn package

根据项目不同自动选择打 war 包 还是 jar 包

4.5 安装

mvn install

将项目打包到本地仓库

5. Maven 生命周期

在 maven 中存在 “三套” 生命周期,每一套生命周期相互独立,互不影响

  • 清理生命周期:clean

  • 默认生命周期:compile、test、package、install、deploy

  • 站点声明周期:site

6. 依赖范围

A 依赖 B,需要在 A 的 pom.xml 文件中添加 B 的坐标,添加坐标时需要指定依赖范围,以来范围包括:

依赖范围对于编译classpath有效对于测试classpath有效对于运行时classpath有效例子
compilespring-core
test--junit
provided-servlet-api
runtime-jdbc驱动
system-本地仓库之外的类库,不推荐

7. Maven 创建普通项目

  1. 选择 maven 模板

    在这里插入图片描述

  2. 填写项目信息,完成创建

    在这里插入图片描述

  3. 当前创建完项目结构如下所示

    在这里插入图片描述

8. Maven 创建 java web 项目

  1. 选择模板,此时要勾选从模板创建

    在这里插入图片描述

  2. 填写项目信息

    在这里插入图片描述

  3. 配置本地 maven

    在这里插入图片描述

  4. 完成创建

    创建完成的项目结构如下

    在这里插入图片描述

  5. 完善标准 maven java web 目结构

    在这里插入图片描述

  6. 添加 Tomcat 服务器

在这里插入图片描述

  1. 配置 Tomcat 服务器

    在这里插入图片描述

  2. 解决警告问题

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

  3. 启动 Tomcat

    在这里插入图片描述

9. pom文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- Maven头文件 -->
<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">
    <!-- Maven 版本号 -->
    <modelVersion>4.0.0</modelVersion>
    <!-- 配置的GAV -->
    <groupId>com.ice</groupId>
    <artifactId>demo01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--
      项目的打包方式
      jar:java 应用
      war:java web 应用
     -->
    <packaging>war</packaging>

    <name>demo01 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.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <!-- 项目依赖 -->
    <dependencies>
        <!-- 具体依赖的jar包配置文件 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- 项目构建用的东西 -->
    <build>
        <finalName>demo01</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中配置resources,来防止我们资源导出失败的问题-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.properties</exclude>
                    <exclude>**/*.xml</exclude>
                </excludes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

10. web.xml

IDEA 会自动创建一个 web.xml 文件,但是最好替换成 Tomcat 里的 web.xml 内容

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">

    <display-name>Welcome to Tomcat</display-name>
    <description>
        Welcome to Tomcat
    </description>

</web-app>

11. Maven 仓库的使用

地址:https://mvnrepository.com/

在这里插入图片描述

搜索需要的 jar 包名称,选择使用版本即可获取 pom 文件以来的写法,粘贴到 pom 文件里即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值