⚡别再被依赖地狱折磨!一篇看懂Maven:3分钟创建项目,10秒解决Jar包冲突,新手也能秒变架构师!
啥是Maven?。我们可以直接通过一个文件“pom.xml”,把我们需要的jar包给导进来,这些设置啊配置什么的,maven都给我们做好了,也就是“全自动导包Maven有啥用?、提高我们的开发效率,减少开发的难度主要的功能是以下两个:1.项目构建2.依赖管理问题来了:依赖又是啥?
一、啥是Maven,Maven有啥用
啥是Maven?
不讲苦涩的话,简单说:Maven就是个项目管理工具。
我们可以直接通过一个文件“pom.xml”,把我们需要的jar包给导进来,这些设置啊配置什么的,maven都给我们做好了,也就是“全自动导包”。
Maven有啥用?
它的作用就是:更简单、更方便、提高我们的开发效率,减少开发的难度
主要的功能是以下两个:
1.项目构建
2.依赖管理
问题来了:依赖又是啥?
依赖(dependency),就是我们项目中需要用到的jar包,
比如说:
我之前写过一篇JDBC的文章,就是如何导jar包什么的,哎哟我去,那可真是太麻烦了,我不仅要去依赖仓库下载jar包,我还得自己导进去,特别特别复杂;
但是如果我有maven的话,我只要知道它的mavenID那些,我把它加进pom.xml就可以直接自动导进去,减少了很多学习的成本,更快更方便更简单,提高了开发效率。
上面就成功导进去了,简直简单到没边。
如果想看一下原来没有maven要自己导包的话,多麻烦的请看我另一篇博客:
短短几行代码?怎么做到的?
其实Maven相当于一个菜鸟驿站,几行代码其实就相当于“你的快递在“xxxx-xx-xx” 请注意查收”

我们有中央仓库和本地仓库:
中央仓库是全球唯一的,由Maven公司运营,依赖都从这下载
本地仓库,是我们自己下载的依赖仓库,我们从Maven官网或者阿里下载,都会保存到自己的电脑,那要用的时候就从本地仓库拿,就不用下载了。
私服指的是一个公司一起共用的仓库

啥是依赖传递?
在早期,我们没有maven的时候,如果我们的项目A需要依赖B,可是B又依赖C,我们不仅要把B导进来,还需要导进C ,这就是依赖传递。
举个例子:假设我要追女神,我要约她吃饭,但是她一定要带她的闺蜜,不带就不去。那么我只能去求女神的闺蜜也一起过来吃饭,这就是依赖传递。
但是有了maven之后,因为maven的依赖具有传递性,我们并不需要把C也引进来,maven自动帮我一起导进去,就相当于女神不问我意见,直接把她闺蜜也带了过来~~
我们把AB和BC叫做直接依赖,AC叫做间接依赖
依赖排除
我们的项目太大了,依赖太多了,太多太杂了,我们需要把不要的依赖排除出去。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.6</version>
<!-排除依赖-->
<exclusions>
<exclusion>
<artifactId>spring-jcl</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
maven还有⼀些功能是依赖调解,可选依赖、依赖调解:
当项⽬中的依赖存在依赖冲突时,例如存在这样的依赖:
A->B->C->X(1.0)
A->D->X(2.0)
Maven会采⽤最短路径优先的原则去选择依赖,这⾥2的依赖路径更短,所以会选择X(2.0),当然我们也可以选择指定某个依赖的版本 当然也可以指定X的版本
二、Maven怎么创建
在网上,有很多教大家怎么安装下载的,还有配置,还专门注明了是2025年最新的。
哎哟我去,大清都亡了,这种技术太老了,现在Spring已经把Maven集成了,
也就是说我们创建的Spring项目已经自带Maven了。
当然,可以直接创建Maven项目,我先带大家创建一次maven项目,只需要一次就好了,以后我们直接用Spring选中maven创建Spring项目就好了。
1.打开idea ->New Project(勾选Maven)

2.点击Next -> 输入项目名,点击Finish

3.创建完之后,我们需要配置Maven仓库的
打开File -> Settings -> Maven
(每个人的路径都不一样,请按照实际来设置,主要不要有中文)

注意的是:由于依赖是需要从中央仓库下载的,所以速度很慢,我们这时候可以换成国内的镜像源。
也就是Settings.xml这个文件注释掉原本的镜像加入下面的镜像
<mirror> <id>aliyunmaven</id> <mirrorOf>central</mirrorOf> <name> 阿⾥云公共仓库 </name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror>如果你没有这个文件,那么你可以自己创建个Settings.xml文件,把下面的粘贴上去
<!-- 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.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> --> <!-- 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 line like "mvn prefix:goal". Maven will automatically add the group identifiers | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list. | --> <pluginGroups> <!-- pluginGroup | Specifies a further group identifier to use for plugin lookup. <pluginGroup>com.your.plugins</pluginGroup> --> </pluginGroups> <!-- proxies | This is a list of proxies which can be used on this machine to connect to the network. | Unless otherwise specified (by system property or command-line switch), the first proxy | specification in this list marked as active will be used. | --> <proxies> <!-- proxy | Specification for one proxy, to be used in connecting to the network. | <proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <username>proxyuser</username> <password>proxypass</password> <host>proxy.host.net</host> <port>80</port> <nonProxyHosts>local.net|some.host.com</nonProxyHosts> </proxy> --> </proxies> <!-- servers | This is a list of authentication profiles, keyed by the server-id used within the system. | Authentication profiles can be used whenever maven must make a connection to a remote server. | --> <servers> <!-- server | Specifies the authentication information to use when connecting to a particular server, identified by | a unique name within the system (referred to by the 'id' attribute below). | | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are | used together. | <server> <id>deploymentRepo</id> <username>repouser</username> <password>repopwd</password> </server> --> <!-- Another sample, using keys to authenticate. <server> <id>siteServer</id> <privateKey>/path/to/private/key</privateKey> <passphrase>optional; leave empty if not used.</passphrase> </server> --> </servers> <!-- mirrors | This is a list of mirrors to be used in downloading artifacts from remote repositories. | | It works like this: a POM may declare a repository to use in resolving certain artifacts. | However, this repository may have problems with heavy traffic at times, so people have mirrored | it to several places. | | That repository definition will have a unique id, so we can create a mirror reference for that | repository, to be used as an alternate download site. The mirror site will be the preferred | server for that repository. | --> <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>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> </mirrors> <!-- profiles | This is a list of profiles which can be activated in a variety of ways, and which can modify | the build process. Profiles provided in the settings.xml are intended to provide local machine- | specific paths and repository locations which allow the build to work in the local environment. | | For example, if you have an integration testing plugin - like cactus - that needs to know where | your Tomcat instance is installed, you can provide a variable here such that the variable is | dereferenced during the build process to configure the cactus plugin. | | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles | section of this document (settings.xml) - will be discussed later. Another way essentially | relies on the detection of a system property, either matching a particular value for the property, | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'. | Finally, the list of active profiles can be specified directly from the command line. | | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact | repositories, plugin repositories, and free-form properties to be used as configuration | variables for plugins in the POM. | | --> <profiles> <!-- profile | Specifies a set of introductions to the build process, to be activated using one or more of the | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/> | or the command line, profiles have to have an ID that is unique. | | An encouraged best practice for profile identification is to use a consistent naming convention | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc. | This will make it more intuitive to understand what the set of introduced profiles is attempting | to accomplish, particularly when you only have a list of profile id's for debug. | | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo. <profile> <id>jdk-1.4</id> <activation> <jdk>1.4</jdk> </activation> <repositories> <repository> <id>jdk14</id> <name>Repository for JDK 1.4 builds</name> <url>http://www.myhost.com/maven/jdk14</url> <layout>default</layout> <snapshotPolicy>always</snapshotPolicy> </repository> </repositories> </profile> --> <!-- | Here is another profile, activated by the system property 'target-env' with a value of 'dev', | which provides a specific path to the Tomcat instance. To use this, your plugin configuration | might hypothetically look like: | | ... | <plugin> | <groupId>org.myco.myplugins</groupId> | <artifactId>myplugin</artifactId> | | <configuration> | <tomcatLocation>${tomcatPath}</tomcatLocation> | </configuration> | </plugin> | ... | | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to | anything, you could just leave off the <value/> inside the activation-property. | <profile> <id>env-dev</id> <activation> <property> <name>target-env</name> <value>dev</value> </property> </activation> <properties> <tomcatPath>/path/to/tomcat/instance</tomcatPath> </properties> </profile> --> </profiles> <!-- activeProfiles | List of profiles that are active for all builds. | <activeProfiles> <activeProfile>alwaysActiveProfile</activeProfile> <activeProfile>anotherAlwaysActiveProfile</activeProfile> </activeProfiles> --> </settings>然后保存就好了,在设置到idea里面。
repo那个文件就是放依赖的地方,我们放到我们的电脑,这些依赖就叫本地Maven仓库。
4.然后点击apply,点击File,如图:点击Settings for New Project

5.开始下载依赖,点击idea右边的Maven,然后刷新

6.点开Build,看看下载情况
下面这样的有三个的是ok的,正在下载

如果等了很久一直是两个的,是下面这样的,是不ok的,下载不下来的

要考虑三个问题:
1.没有配置好设置,镜像那些
解决方法: 往回去看看怎么配置,还要设置
2.网络不好
解决方法:换个网络,换个时间
3.缓存问题
解决方法:打开File->Invalidate Cachs
7.下载完成

出现上面的样子,并且没报红,就算下载完成了依赖,这时打开repo文件:
Spring集成了Maven,其实可以不用创建这个Maven项目了,直接创建Spring项目选择Maven就行了,非常的方便。下一篇博客教大家如何创建Spring项目。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐






所有评论(0)