2023 maven的安装以及配置 亲测有效~

Maven 是一个用于构建和管理 Java 项目的强大工具。它提供了一种标准化的项目结构、依赖管理、构建过程定义等功能,使得项目的开发、构建和部署变得更加简单和高效。

以下是 Maven 的一些主要特点和功能:

  1. 依赖管理:Maven 提供了依赖管理功能,可以通过简单的配置文件(pom.xml)声明项目所需的依赖项。Maven 负责自动下载这些依赖项,并将其添加到项目的类路径中。这样,开发人员不需要手动管理和维护外部库文件,简化了项目的配置和协作。

  2. 项目结构标准化:Maven 规定了一种标准的项目结构,使得团队成员能够更容易地理解和协作。项目结构包括源代码目录、资源目录、测试目录等。此外,Maven 还提供了一组约定的构建生命周期和构建阶段,例如编译、测试、打包和部署,使整个构建过程更加统一和可控。

  3. 构建过程管理:Maven 使用插件机制来扩展构建过程。通过配置插件,可以执行各种任务,如编译源代码、运行测试、生成文档、打包应用程序等。Maven 提供了许多常用插件,也允许开发人员自定义和集成其他插件。

  4. 自动化构建:Maven 的构建过程是自动化的,无需手动执行命令。通过运行 Maven 命令(如 mvn clean install),Maven 将按照预定义的构建生命周期和阶段进行逐步构建,并处理依赖关系、编译代码、运行测试等。

  5. 中央仓库:Maven 提供了一个中央仓库,其中包含大量常用的开源 Java 库和框架。当声明项目依赖时,Maven 可以自动从中央仓库下载所需的库文件。

总而言之,Maven 是一个功能丰富且广泛使用的构建工具,它简化了 Java 项目的管理和构建过程。通过依赖管理、标准化的项目结构、构建过程管理和自动化构建等特性,Maven 提高了开发效率、降低了配置成本,并促进了团队协作。

现在我们就来安装maven以及环境配置、

第一步:首先我们先进入官网Welcome to The Apache Software Foundation! 

找到下面maven

进入download进项下载

 下载完之后是这样子的

 把maven解压到指定的位置,因为我之前已经配置mavan所以我就拿我之前的maven版本进行演示

 点击maven文件,选中路径,复制下来

进入环境变量

  • 点击新建,然后按照图片上面,首先设置变量名MAVEN_HOME,变量值是就是我们刚才复制的maven的文件路径

找到path,点击编辑,然后点击新建

 

我之前是已经配置好了所以我就没有配置了,你们按照上面的来就行

%MAVEN_HOME%\bin 是一个表示环境变量的占位符。在此上下文中,%MAVEN_HOME% 表示 Maven 安装目录的路径。

具体来说,%MAVEN_HOME% 是一个指向 Maven 安装目录的环境变量,它是通过操作系统来设置的。该环境变量提供了 Maven 的安装路径,以便系统能够找到并执行 Maven 相关的命令和工具。

bin里面就是maven的一个环境,就像我们平时配置JDK一样。

我们的maven环境配置好了,但是我们还不知道到底配置成功没有,所以我们需要进行测试一下。

首先 win + r 输入 cmd

输入mvn

如果出现这个页面说明配置成功

 可以通过一下命令查看maven的版本

mvn -version

可以看到,我的版本是3.9.2的

接下面就是配置maven的镜像仓库,找到刚才maven安装的位置,然后进入

 

 选中settings.xml文件,进行一个设置,图片上面选中的地方表示我们以后我们下载jar包的时候jar包存放的位置,如果没有设置的话,那么就会默认放到C盘

 

可以看到我这个文件里面放了很多jar包,这些都是在idea中下载的依赖

下面就是我的setttings文件的设置,需要的可以直接复制

<?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">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>D:\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>alimaven</id>
          <mirrorOf>central</mirrorOf>
          <name>aliyun maven</name>
          <url>https://maven.aliyun.com/nexus/content/groups/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
     | 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 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>
    -->
    <!-- jdk配置 --> 
<profile>
      <id>jdk-17</id>
      <activation>
	    <activeByDefault>true</activeByDefault>
        <jdk>17</jdk>
      </activation>

      <properties>
        <maven.compiler.source>17</maven.compiler.source>
		<maven.compiler.target>17</maven.compiler.target>
		<maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
      </properties>
      
</profile>

  </profiles>

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

简单讲下:

这个表示镜像的仓库,没有配置的话就会默认使用国外的,下载jar包的速度就会比较忙,那么我选择的是阿里云的镜像仓库,属于国内的,下载速度很快。

maven也需要指定jdk的一个版本,我的是jdk17的,我个人比较喜欢用这个,很多人也喜欢用jdk8,那么只需要改下版本即可。

 接下来就是我们进入idea进行maven的一个简单使用,创建一个新的项目,语言选中java,构建系统选中maven。

点击创建,我的idea的版本是2023年最近版本的,所以会和以前的有些不一样,而且我使用了ui的皮肤,看个人习惯吧

进入设置,配置一下maven

设置好之后点击应用,然后接下来就是下载maven的插件,点击右上角的maven,点击刷新,就会自动下载maven需要的插件。

下载完成之后,接下来我们演示第一个maven项目的demo,选中pom.xml文件。

然后我们进入maven的坐标库https://mvnrepository.com/

 

 

 

鼠标单击这个依赖坐标,就会自动复制,然后我们把这个粘贴到pom文件中,放入dependencies中上面会出现一个刷新的东西,点击刷新就是对依赖进项的一个下载,如果没有出现这个刷新的东西,那么也可以选中又上角maven进行刷新。

进入test进行一个测试

运行成功说明maven没有问题

注意事项,我的maven版本是3.9.2的,需要用到这个版本的话,idea版本也必须是高版本的,不然不兼容,会导致插件下载报错,所以我建议大家还是用maven 3.6的版本,比较稳定。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值