Maven下载安装与配置、Idea配置Maven(详细版)

YDYXCODE · · 345 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
>Maven是Apache软件基金会的一个开源项目,是一款优秀的项目构建工具,它主要用于帮助开发者管理项目中jar以及jar之间的依赖关系,最终完成项目编译,测试,打包和发布等工作。 前面我们已经简单介绍了Maven的概念、特点及使用,本篇文章就来给大家出一个详细的安装和配置教程,还没有安装Maven的小伙伴要赶紧收藏起来哦! 首先给大家解释一下为什么学习Java非要学Maven不可。 ## 一、为什么要学习Maven? 大家在读这篇文章之前大部分人都已经或多或少的经历过项目,说到项目,在原生代码无框架的时候,最痛苦的一件事情就是要在项目中导入各种各样使用的jar包,jar太多就会导致项目很难管理。 需要考虑到jar包之间的版本适配的问题还有去哪找项目中使用的这么多的jar包,等等。这个时候,Maven就出现了,它轻松解决了这些问题。 说的直白一点,maven就相当于个三方安装包平台,可以通过一些特殊的安装方式,把一些三方的jar包,装到咱们自己的项目中。 好了,废话不多说下面就正式进入到Maven的下载安装。 ## 二、Maven安装以及配置 Maven下载地址:https://maven.apache.org/ ![image.png](http://static.itsharecircle.com/231214/ff1bc6d7d0ae732006c192150bc1b762.png) 打开欢迎界面点击download下载 ![image.png](http://static.itsharecircle.com/231214/620d14b64f711037fd93f611b670fdea.png) 如下图上的标记,我们可以根据自己的需要选择对应的版本来下载 ![image.png](http://static.itsharecircle.com/231214/8e5cc75e21dc7890f7d034d4a561ad50.png) 在这里我们安装的是Maven的3.6.3版本 ![image.png](http://static.itsharecircle.com/231214/64831076a0f92dfad6bd94f867f1bb1a.png) 下载好之后是一个压缩包 ![image.png](http://static.itsharecircle.com/231214/10eae1932afdfcc7ae6b1fcae9185f32.png) 在电脑中找一个地方把压缩包解压,解压后 ![image.png](http://static.itsharecircle.com/231214/e57cd3c144a7444b0b0ecf97f72fa805.png) 配置环境变量 ![image.png](http://static.itsharecircle.com/231214/b072b57c46b1994027a572b756774e12.png) 在这儿一定要注意配置路径 ![image.png](http://static.itsharecircle.com/231214/7ba8575f3da5164eda6b2cfe613b2287.png) 接下来我们验证一下有没有安装成功: 点击开始菜单 ->搜查框输入:cmd 回车 -> 出现Maven版本号说明安装成功 ![image.png](http://static.itsharecircle.com/231214/08aa8174ea4908bedce01d3109af31b4.png) ## 三、Maven配置本地仓库 如何将下载的 jar 文件存储到我们指定的仓库中呢? 需要在 maven 的服务器解压的文件中找到 conf 文件夹下的 settings.xml 文件进行修改,如下图所示: ![image.png](http://static.itsharecircle.com/231214/c2d3d4c0a09f4f8b88eb6d7dee827c92.png) 进入文件夹打开settings.xml文件 ![image.png](http://static.itsharecircle.com/231214/a1beb25102266c5fb1bb28e422e0f33d.png) 因为默认的远程仓库地址,是国外的地址;在国内为了提高下载速度,可在如图所示位置配置阿里云仓库。 ![image.png](http://static.itsharecircle.com/231214/6dc5ad50dae178db2d7643472c099dd5.png) ## 四、在idea工具中配置Maven 打开idea-----点击File-----点击New Projects Settings-----点击Setting for New Projects… ![image.png](http://static.itsharecircle.com/231214/0ea040c433a62c42d20f0add43c82313.png) 这样,我们的Maven就安装配置完成了。 >你还在苦恼找不到真正免费的编程学习平台吗?可以试试云端源想!课程视频、在线书籍、在线编程、实验场景模拟、一对一咨询……你想要的全部学习资源这里都有,重点是统统免费![点这里即可查看](https://ydcode.cn/memberIndex?sourceId=462) 最后,附上我们一个配置文件,里面其实很大篇幅都是注释,为我们解释标签的用途,核心是咱们配置的远程中央仓库地址;在有的公司,是有自己的远程私有仓库地址的,配置方式跟中央仓库配置基本雷同,可能有一些账号密码而已。 ``` <?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 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>aliyun</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </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> ``` 跟着教程一步一步安装下来,是不是发现也挺简单的,那么你的Maven安装成功了吗?
345 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传