文章目录
一、关于Maven
1.Maven的概念
Maven 是⼀个项⽬构建⼯具,创建的项⽬只要遵循 Maven 规范(称为Maven项⽬),即可使⽤ Maven 来进⾏:管理 jar 包、编译项⽬,打包项⽬等功能。
为什么学习 Servlet 之前要学 Maven?
因为 Servlet 是框架,要使⽤ Maven 进⾏ jar 包管理和项⽬打包和发布。
2.Maven的作用
- 导⼊外部 jar 包
- 打包项⽬
- 发布项⽬
二、Maven的配置及使用
Maven ⽆需安装,因为 Idea 已经⾃带了,打开 Idea 的设置页面settings,在搜索框中输入“Maven”就能找到 Maven,如下图所示:
1.Maven 依赖管理流程
Maven 项⽬中可以引⼊依赖包(引⼊外部框架的 jar 包),引⼊后,加载依赖包的⽅式为在 Maven 仓库 中搜索。
Maven仓库可以理解为存放依赖包的仓库,分为本地仓库和远程仓库两种。
本地仓库地址在 Idea 中可以找到,如下图所示:
2.Maven的配置
Maven的数据源默认是国外的数据源,所以下载 jar 会很慢,且经常出差,所以⼀定要配置本机的 Maven 源 为国内源,它的配置⽅法如下:
1.找到maven的settings.xml配置文件
默认情况下电脑上有这个文件。
如果没有,解决方法如下:
复制settings.xml文件名,这里不建议手写,因为手写出错的可能性更大,会导致后面下载jar包失败。
【注】:配置文件目录是不能出现中文的。 如果出现中文字符,需要在Users的下一级目录新建xx\.m2文件,一定要改成英文文件名。
在配置文件目录的.m2文件中新建settings.xml文件
将下列代码复制到settings.xml文件中。
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.conf}/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->
<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
<!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command l