Maven简介
传统项目分析
- jar包不统一,jar包不兼容
- 工程升级维护过程繁琐
- …
Maven是什么
-
项目管理工具,将项目开发和管理过程抽象成一个项目对象模型(POM)
-
POM:(Project Object Model):项目对象模型
-
- pom.xml---------->pom<-------------->依赖管理------------>本地仓库------------>私服--------------->中央仓库
- (构 建 生 命 周 期)
- 插件 插件 插件 插件
- jar包 源代码 帮助文档 war包 xml
Maven下载
-
https://maven.apache.org/download.cgi
-
link下的第一个是源码,解压第二个即可
-
配置环境变量
-
新建MAVEN_HOME
-
配置path变量新建:%MAVEN_HOME%\bin
-
测试cmd
-
C:\Users\WU>mvn [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.049 s [INFO] Finished at: 2022-04-18T17:10:20+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [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/NoGoalSpecifiedException
-
Maven重要概念
坐标:
-
对每个资源的标记,描述仓库中资源的位置
-
重要组成:
-
group:定义当前Maven项目隶属于组织名称(通常是域名反写,eg:org.mybatis)组织ID
-
artifactld:定义当前Maven项目名称(通常是模块名称列如CRM,SMS)项目ID
-
Version:定义项目版本号
-
packaging:定义打包方式
eg:在https://mvnrepository.com/找到的一个jar包 <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12-beta-3</version> <scope>test</scope> </dependency>
-
-
使用唯一标识,唯一性定位资源位置,通过该标识可以将资源的识别与下载工作交给机器完成
本地仓库配置(下到哪)
- 我们cmd命令mvn时,在C盘的users中(用户)会出现.m2文件夹,里面存放的就是maven的下载数据等,查看C:\apache-maven-3.8.5\conf中的settings.xml文件,发现以下指令:
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.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>
- 其中的Default: ${user.home}/.m2/repository表示如果没找到/path/to/local/repo路径,则放置在user的.m2文件夹下,据此我们在下面插入一行:
localRepository>C:\maven\repository</localRepository>
中间填上数据下载位置(自定义)*注:该地址与用户setting有关
远程仓库的配置(从哪下)
-
Maven默认连接的仓库位置
-
我们找到C:\apache-maven-3.8.5\lib中的任意一个jar文件,右击用winRAR打开,点到上一层目录lib里,然后点击查找,查找文件名pom* . *,可以找到一个和其他格式不一样的org\apache\maven\model\pom-4.0.0.xml,定位他的位置,打开这个文件,我们可以找到
-
<repositories> <repository> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
-
central仓库的信息,地址为国外 https://repo.maven.apache.org/maven2,所以我们要配置国内镜像站
-
-
配置Maven中央仓库国内镜像站
-
进入Maven的settings.xml,找到以下
-
<mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> </mirrors>
-
在模板下面插入
-
<mirror> <id>huaweicloud</id><!-- 此镜像的唯一标识符,用来区分不同的miorror元素 --> <mirrorOf>central</mirrorOf><!-- 对哪种仓库进行镜像,简单说就是代替哪个仓库(central仓库)--> <name>Nexus huawei</name><!-- 镜像名称 --> <url>https://mirrors.huaweicloud.com/repository/maven/</url><!--镜像URL地址--> </mirror>
-
结果eg:
-
<mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>huaweicloud</id><!-- 此镜像的唯一标识符,用来区分不同的miorror元素 --> <mirrorOf>central</mirrorOf><!-- 对哪种仓库进行镜像,简单说就是代替哪个仓库(central仓库)--> <name>Nexus huawei</name><!-- 镜像名称 --> <url>https://mirrors.huaweicloud.com/repository/maven/</url><!--镜像URL地址--> </mirror> </mirrors>
-
*这里我用的是华为的镜像网址
-
*目前我的配置是全局设置(全局setting)
用户setting的设置:
- 在你自定义下载位置时建的文件夹C:\maven\(在repository的同一层)复制一个setting文件(尽量保证二者一样,局部setting会覆盖全局setting)