【MAC】【JAVA】【maven】【全方位配置】【这一篇绝对够用】

本文详细描述了在Mac上安装Maven的推荐方法,包括离线安装、settings.xml配置、环境变量设置以及IDEA中全局和新项目配置的步骤,确保Maven环境的正确搭建和IDEA的无缝集成。
摘要由CSDN通过智能技术生成

目标

  • 安装maven
  • 完成maven配置
  • idea中完成maven配置
  • 完成测试

吐槽
网上的东西是真的离谱…没有一个是写全篇的,然后问题也是分散的…

操作步骤

1、安装 jdk

mac电脑下,直接yum装进可以了 ->无需多说

2、安装 maven

🤯 【不推荐】 安装方式一:brew安装

1、brew 不去识别系统中的jdk,会重新安装openjdk
2、brew 安装的openjdk,安装后,在idea中也要重新配置
总结:我觉得很麻烦,而且还占存储,不划算

✅ 【推荐】安装方式二:离线安装
1、下载地址:https://maven.apache.org/docs/history.html
不推荐指定那个版本,可以自己去选择,选择的时候,要注意版本:
maven下载历史版本
2、下载好后,自行解压
在文件的位置,右键打开终端,然后输入tar -zxvf ./xxx,进行解压,再把下载的包给删了
解压压缩包
3、创建统一的java维护仓库
这个随便,我是根目录新建的,然后可以按照我这种的创建一个,一个是maven、一个是maven下载仓库的位置
在这里插入图片描述
4、配置 settings.xml 【没耐心看的,可以直接去复制完整配置】
需要配置的内容

1、仓库位置
2、代理配置
3、jdk版本配置

settings.xml 文件位置在这个地方:
在这里插入图片描述
maven仓库位置:
在这里插入图片描述
【tips】你就找标签加s的上方再添加内容,绝对不错
代理配置:
在这里插入图片描述
jdk配置:
在这里插入图片描述
完整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.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">
    <!-- java库下载位置 -->
    <localRepository>/Users/wuyihe/code/java/maven-repository/</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>

    <!-- TODO Since when can proxies be selected as depicted? -->
    <!-- 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>central</mirrorOf>
            <name>aliyun maven</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 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>
            <id>jdk-11</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>11</jdk>
            </activation>
            <properties>
                <maven.compiler.source>11</maven.compiler.source>
                <maven.compiler.target>11</maven.compiler.target>
                <maven.compiler.release>11</maven.compiler.release>
            </properties>
        </profile>
    </profiles>

</settings>

环境变量配置

1、先用sudo vim ~/.bash_profile,进入修改页面
2、英文状态下ESC、再输入:wq

export MA_HOME=/Users/wuyihe/code/java/maven
export PATH=$PATH:$MA_HOME/bin

这一步不用source ~/.bash_profile,因此时说单个终端生效,未全局生效
3、全局命令配置
我的maczsh,需修改位置~/.zshrc
同上操作,先打开文件,再复制,再保存

if [ -f ~/.bash_profile ]; then
   source ~/.bash_profile
fi

在这里插入图片描述
4、再输入source ~/.zshrc,让其生效
5、打开终端测试
在这里插入图片描述
6、maven 配置完成后的测试命令
mvn help:system
我就不重复输入了,最后下载的包内容:出现在我配置的路径下了,符合预期
在这里插入图片描述

配置 IDEA中的maven

【Tips】

1、idea中分为系统配置和创建新文件时配置
2、系统的配置内容,不会对创建文件时生效

IDEAmaven全局配置

在这里插入图片描述
进入后,直接搜索maven
点击文件小图标,把自己配置的放进去就可以
在这里插入图片描述
IDEA中创建新项目时的maven配置
剩余操作和上面一致
在这里插入图片描述
开始测试:
创建新项目
在这里插入图片描述
使用maven模式创建
自己再填写内容,就可以了~~
在这里插入图片描述
注意:第一次的创建过程中,因为要下载`maven·的各个阶段的执行工具,需要等待会
在这里插入图片描述
到此整个过程就结束了

总结

1、在安装maven的可以自己选择的去安装;
2、settings.xml配置;
3、系统环境变量进行配置;
4、IDEA配置


1、上述内容,全为我实操结果,若有错误,请指正,谢谢;
2、如若要转发,请贴上来源,谢谢;

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值