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

Maven是Apache软件基金会的一个开源项目,是一款优秀的项目构建工具,它主要用于帮助开发者管理项目中jar以及jar之间的依赖关系,最终完成项目编译,测试,打包和发布等工作。前面我们已经简单介绍了Maven的概念、特点及使用,本篇文章就来给大家出一个详细的安装和配置教程,

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

打开欢迎界面点击download下载

image.png

如下图上的标记,我们可以根据自己的需要选择对应的版本来下载

image.png

在这里我们安装的是Maven的3.6.3版本

04.png

下载好之后是一个压缩包

image.png

在电脑中找一个地方把压缩包解压,解压后

05.png

配置环境变量

06.png

在这儿一定要注意配置路径

07.png

接下来我们验证一下有没有安装成功:

点击开始菜单 ->搜查框输入:cmd 回车 -> 出现Maven版本号说明安装成功

08.png

三、Maven配置本地仓库

如何将下载的 jar 文件存储到我们指定的仓库中呢?

需要在 maven 的服务器解压的文件中找到 conf 文件夹下的 settings.xml 文件进行修改,如下图所示:

09.png

进入文件夹打开settings.xml文件

10.png

因为默认的远程仓库地址,是国外的地址;在国内为了提高下载速度,可在如图所示位置配置阿里云仓库。

11.png

四、在idea工具中配置Maven

打开idea-----点击File-----点击New Projects Settings-----点击Setting for New Projects…

12.png

这样,我们的Maven就安装配置完成了。

编程学习,从云端源想开始,课程视频、在线书籍、在线编程、一对一咨询……你想要的全部学习资源这里都有,重点是统统免费!点这里即可查看

最后,附上我们一个配置文件,里面其实很大篇幅都是注释,为我们解释标签的用途,核心是咱们配置的远程中央仓库地址;在有的公司,是有自己的远程私有仓库地址的,配置方式跟中央仓库配置基本雷同,可能有一些账号密码而已。

<!--StartFragment-->

&lt;?xml version="1.0" encoding="UTF-8"?>

&lt;!--
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.
-->

&lt;!--
 | 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.
 |
 |-->
&lt;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">
    &lt;!-- localRepository
     | The path to the local repository maven will use to store artifacts.
     |
     | Default: ${user.home}/.m2/repository
    &lt;localRepository>/path/to/local/repo&lt;/localRepository>
    -->

    &lt;!-- 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
    &lt;interactiveMode>true&lt;/interactiveMode>
    -->

    &lt;!-- 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
    &lt;offline>false&lt;/offline>
    -->

    &lt;!-- 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.
     |-->
    &lt;pluginGroups>
        &lt;!-- pluginGroup
         | Specifies a further group identifier to use for plugin lookup.
        &lt;pluginGroup>com.your.plugins&lt;/pluginGroup>
        -->
    &lt;/pluginGroups>

    &lt;!-- 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.
     |-->
    &lt;proxies>
        &lt;!-- proxy
         | Specification for one proxy, to be used in connecting to the network.
         |
        &lt;proxy>
          &lt;id>optional&lt;/id>
          &lt;active>true&lt;/active>
          &lt;protocol>http&lt;/protocol>
          &lt;username>proxyuser&lt;/username>
          &lt;password>proxypass&lt;/password>
          &lt;host>proxy.host.net&lt;/host>
          &lt;port>80&lt;/port>
          &lt;nonProxyHosts>local.net|some.host.com&lt;/nonProxyHosts>
        &lt;/proxy>
        -->
    &lt;/proxies>

    &lt;!-- 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.
     |-->
    &lt;servers>

        &lt;!-- 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.
         |
        &lt;server>
          &lt;id>deploymentRepo&lt;/id>
          &lt;username>repouser&lt;/username>
          &lt;password>repopwd&lt;/password>
        &lt;/server>
        -->

        &lt;!-- Another sample, using keys to authenticate.
        &lt;server>
          &lt;id>siteServer&lt;/id>
          &lt;privateKey>/path/to/private/key&lt;/privateKey>
          &lt;passphrase>optional; leave empty if not used.&lt;/passphrase>
        &lt;/server>
        -->
    &lt;/servers>

    &lt;!-- 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.
     |-->
    &lt;mirrors>
        &lt;!-- 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.
         |
        &lt;mirror>
          &lt;id>mirrorId&lt;/id>
          &lt;mirrorOf>repositoryId&lt;/mirrorOf>
          &lt;name>Human Readable Name for this Mirror.&lt;/name>
          &lt;url>http://my.repository.com/repo/path&lt;/url>
        &lt;/mirror>
         -->
         &lt;mirror>
          &lt;id>aliyun&lt;/id>
          &lt;name>aliyun maven&lt;/name>
          &lt;url>http://maven.aliyun.com/nexus/content/groups/public/&lt;/url>
          &lt;mirrorOf>central&lt;/mirrorOf>
         &lt;/mirror>

    &lt;/mirrors>

    &lt;!-- 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.
     |
     |-->
    &lt;profiles>
        &lt;!-- 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 &lt;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.
        &lt;profile>
          &lt;id>jdk-1.4&lt;/id>

          &lt;activation>
            &lt;jdk>1.4&lt;/jdk>
          &lt;/activation>

          &lt;repositories>
            &lt;repository>
              &lt;id>jdk14&lt;/id>
              &lt;name>Repository for JDK 1.4 builds&lt;/name>
              &lt;url>http://www.myhost.com/maven/jdk14&lt;/url>
              &lt;layout>default&lt;/layout>
              &lt;snapshotPolicy>always&lt;/snapshotPolicy>
            &lt;/repository>
          &lt;/repositories>
        &lt;/profile>
        -->

        &lt;!--
         | 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:
         |
         | ...
         | &lt;plugin>
         |   &lt;groupId>org.myco.myplugins&lt;/groupId>
         |   &lt;artifactId>myplugin&lt;/artifactId>
         |
         |   &lt;configuration>
         |     &lt;tomcatLocation>${tomcatPath}&lt;/tomcatLocation>
         |   &lt;/configuration>
         | &lt;/plugin>
         | ...
         |
         | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
         |       anything, you could just leave off the &lt;value/> inside the activation-property.
         |
        &lt;profile>
          &lt;id>env-dev&lt;/id>

          &lt;activation>
            &lt;property>
              &lt;name>target-env&lt;/name>
              &lt;value>dev&lt;/value>
            &lt;/property>
          &lt;/activation>

          &lt;properties>
            &lt;tomcatPath>/path/to/tomcat/instance&lt;/tomcatPath>
          &lt;/properties>
        &lt;/profile>
        -->

    &lt;/profiles>

    &lt;!-- activeProfiles
     | List of profiles that are active for all builds.
     |
    &lt;activeProfiles>
      &lt;activeProfile>alwaysActiveProfile&lt;/activeProfile>
      &lt;activeProfile>anotherAlwaysActiveProfile&lt;/activeProfile>
    &lt;/activeProfiles>
    -->
&lt;/settings>

<!--EndFragment-->

跟着教程一步一步安装下来,是不是发现也挺简单的,那么你的Maven安装成功了吗?

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
云端源想
云端源想
@云端源想,https://ydcode.cn/