Maven 最详细 了解&安装&下载&应用

目录

一、本章内容介绍

二、 Maven简单介绍

三、如何下载配置

解压: 

 配置环境变量:

 Maven中的仓库配置:

 配置settings.xml文件:

四、Eclipse创建Maven项目

创建Maven项目以及配置:

五、了解Pom.xml


一、本章内容介绍

        在我们编程的过程中,通常会导入各类的jar包文件从而来完善我们的项目,有的项目需要导入几百个jar包甚至几千个jar包,甚至有的jar包的版本不一样,也会导致编程实现的效果不一样,那面对这样的问题?我们如何解决呢?本章所介绍的内容Maven技术就能完美的解决这些问题,更方便快捷的导入项目所需要的各类jar包,集中导入,大大节省导入jar包所消耗的时间。

二、 Maven简单介绍

        概念: Apache Maven是个项目管理和自动构建工具,基于项目对象模型(POM)的概念。        

        作用:完成项目的相关操作,如:编译,构建,单元测试,安装,网站生成和基于Maven部署项目。

        总而言之:Maven可以极大程度上的提供编程的便捷,快速的导入大量的编程所需jar包文件。

三、如何下载配置

        下载:

        官方网址: Maven – Download Apache Maven 进行下载,如图所示:

解压: 

        找到自己所需要的版本,点击既可以下载,下载完成之后找到对应的下载文件解压到自己想要放置的路径下,在该目录下再创建一个存放Maven下载jar包存放的文件:mvn_repository(名字可自定义,但最好以英文命名)

 配置环境变量:

        基于:已经安装配置好jdk的情况下

        视频配置:

Maven配置

        步骤一(配置环境变量):找到“此电脑”——鼠标右击——单击“属性”——单击“高级系统设置”——单击“高级”——单击“环境变量”——在“系统变量”的下方——“新建”——在“变量名”文本框中输入“MAVEN_HOME”——在“变量名”文本框中选择“浏览目录”——选择已经解压的文件路径“E:\tools\maven\apache-maven-3.5.0-bin\apache-maven-3.5.0-bin\apache-maven-3.5.0”也就是bin目录的上一层——单击确认

        步骤二(使用环境变量):——找到系统变量中的“Path”——进行双击——“新建”——输入“%MAVEN_HOME%\bin”——然后在移动到“%JAVA_HOME%\bin”的下方位置——然后一直单击确定

        步骤三(是否配置成功):“win+r”——调出命令框——输入“cmd”调用黑窗口——输入“mvn -version”命令——最后回车(enter)        如图所示:

         如果回车出现这种状态就说明已经配置成功了。

 Maven中的仓库配置:

        仓库概念:

        中央仓库、私服仓库:涵盖了我们使用的所有jar包

        本地仓库:只能自己访问,jar包有限

        区别:       

                中央仓库:因为是jar包源头,所以更新jar包速度快,但国内访问慢,容易使用Maven在网络不好的情况下下载jar包时,导致下载不全,从而导致出现问题。

                 私服仓库:因为依赖中央仓库,所以jar包更新相对于中央仓库慢(需要一些时间),但是访问速度快,下载jar包速度快。

        Maven中主要有三大仓库:1.中央仓库(国外网站,更新最快)2.私服仓库(常见阿里云仓库,中文形式,jar包资源来自中央仓库)3.本地仓库(已经下载好的jar包)

        中央仓库与私服仓库都需要internet,是一个网站,可以被所有人访问到,因此也被称为远程仓库,而本地仓库是只能自己访问,但不需要网络。

 配置settings.xml文件:

settings配置

        首先我们找到之前所新建的jar包存放“mvn_repository”文件夹,复制路径“E:\tools\maven\mvn_repository”——以记事本形式打开在“E:\tools\maven\apache-maven-3.5.0-bin\apache-maven-3.5.0-bin\apache-maven-3.5.0\conf”里的“settings.xml”文件——Ctrl+F查找“localRepository”配置本地仓库,如图所示:

注意:路径的斜杠需要修改成相反形式,这是标准“\”改为“/” 

        

复制内容(配置好的远程阿里云仓库):

<mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  <mirrorOf>central</mirrorOf>        
</mirror>
<mirror>  
 <id>alimaven</id>  
 <mirrorOf>central</mirrorOf>  
 <name>aliyun maven</name>  
 <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>  
</mirror> 

配置阿里云仓库,Ctrl+F查找“mirrors”私服仓库,如图所示: 

配置成功后,ctrs+s保存 

 完整的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.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>
本地配置
  -->
<localRepository>E:/tools/maven/mvn_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>

  <!-- 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>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  <mirrorOf>central</mirrorOf>        
</mirror>
<mirror>  
 <id>alimaven</id>  
 <mirrorOf>central</mirrorOf>  
 <name>aliyun maven</name>  
 <url>http://maven.aliyun.com/nexus/content/repositories/central/</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>

四、Eclipse创建Maven项目

        在创建Maven项目之前必须要连接WiFI网络(一定要有网络,并且网速可以,不能断网)因为需要使用网络从我们所配置的阿里云远程私服下载jar包到本地仓库(就是我们配置的电脑本地路径存放jar包的位置)

        以Eclipse编程工具为例:

Eclipse基础配置

        单击主界面的“window”——单击“Perferences”——在搜索栏中搜索“Maven”

 ——单击“Installations”——会有一个默认的maven地址,需要你重新配置,选中,apache-maven...——进行修改——把路径改成你存放Maven的路径(bin的上一层目录)——“Apply”

接下来——单击“User Settings”

 ——在界面User Settings(open file)文本框的右边单击“Browse...”——选中settings文件路径确定——“Apply and Close”保存并关闭

创建Maven项目以及配置:

Maven项目的创建以及调配

        打开Eclipse——“Ctrl+N”新建——在输入框中输入“Maven”——找到如下图“Maven Project”选中如图所示:

——点击“Next”下一步——到弹出如下图:

 ——继续单击“下一步”——出现如图所示:

 ——在Catalog下拉框中选中“Internal”——在下方选中“org.apache.maven.archetypes maven-archetype-webapp 1.0”——再下一步如图所示:

 在“Group Id”的文本框中输入路径名,“Artifact Id”输入项目名——单击“Finish”——查看jar包是否在下载:

 ——然后单击“Other”——出现如下图——在输入框中输入“Progress”

 ——找到并选中下方“Progress”——单击下方“Open”就会出现正在下载阿里云jar包的情况

 ——单击下划线内容——可在Console中查看详情,如图所示:

 注意(一定要有网络,并且网速可以,不要断网):

        因为但凡网络不好,jar包就会没有下载全,导致项目出现问题

        问题1:如果创建项目失败,查看自己是否连接WIFI,如果没有连接WIF你已经执行了创建项目的步骤,需要把你存放jar包的文件夹内的org文件给删除掉,重新连接互联网尝试。

        问题2:路径是否为阿里云(aliyun),如果不是,那么需要重新配置settrings.xml文件按照配置settings.xml文件执行即可

查看是否下载完成——出现如图所示:

 “No operations to display at this time.”:代表jar包已经下载完成

进行项目分层

        选中项目——鼠标右击选中“Build Path”——单击“Configure Build Path...”

 ——出现如图界面:

 ——“Apply and Close” 保存并关闭

没有配置配置web.xml(2.3)和jdk的版本(1.5):

配置web.xml和jdk的版本:

配置jdk版本1.8版本:

<!--第一步就是配置maven-compiler-plugin插件-->
<plugins>
<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
</plugins>

   将上方代码复制到pom.xml文件中的<finalName>Maven</finalName>标签的后面,Ctrl+S保存

 

在这个步骤中,可能会出现plugin标签位置出报错的情况如图所示:

解决方案:按照以上的问题1来进行处理

 配置web.xml(3.1)版本:将以下的整段代码复制到web.xml(进行内容更替)“Ctrl+S”保存

<!-- 修改web.xml由2.3至3.1 -->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>
</web-app>

将默认版本进行更改:

我们在更改Web版本为3.1版本的时候,必须要先取消掉2.3版本的勾选,然后“Apply”保存,进行版本的3.1选择,再次“Apply”保存,然后在进行勾选,会在界面的下方位置显示有如图所示:下划线文字单击

 只更改Content directory文本框内容更改为“src/main/webapp” 

 单击“OK”——进行保存并关闭“Apply and Close”

如何在Maven创建的项目中引入jar包:

Maven的jar包引入

        我们在浏览器中搜索中央仓库:https://mvnrepository.com/

        在这个网站中,我们搜索我们需要的jar包以视频中的jstl的jar包为例:

        所有的jar包都可以依此来进行引入Maven项目中

我们选中我们所需要的版本——复制马文的部分带有<dependency></dependency>标签的所有内容

<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

我们将文件复制到pom.xml文件中的<dependencys></dependencys>内,进行保存,就会进行配置(需要网络)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test.maven</groupId>
  <artifactId>Maven</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
<!--
   group id:组织id,一般就是公司域名反写
   artifact id:项目名字
   version:版本
   package:java包名

   src/main(源代码)
   src/test(测试代码)
   target
    编译后、打包后的文件
-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
  </dependencies>
  <build>
    <finalName>Maven</finalName>
    <!--第一步就是配置maven-compiler-plugin插件-->
    <plugins>
<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			</plugins>
  </build>
</project>

如果需要更替jar包的版本也非常方便,只需要将  对应jar包中<version>3.8.1</version>标签内的版本进行更改即可。

五、了解Pom.xml

   Project Object Model,项目对象模型。
   通过xml格式保存的pom.xml文件。作用类似ant的build.xml文件,功能更强大。
   该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。
   最后,根据你的项目需要更新您的pom.xml

   <dependencies>
     <!--英语解释:依赖,专业解释:坐标-->
     <!--  E:/mvn-repository/junit/junit/3.8.1.jar -->
     <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
   </dependencies>


   网址:www.mvnrepository.com
  
   在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署。目前<scope>可以使用5个值: 
    * compile,缺省值,适用于所有阶段,会随着项目一起发布。 
    * provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。 
    * runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。 
    * test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。 
    * system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache Maven是一个基于项目对象模型(POM)的项目管理工具,它可以用来构建、测试、打包和发布Java应用程序。Maven提供了一个标准化的项目结构、依赖管理和生命周期管理的框架,而且这些功能都可以通过简单的配置来实现。 对于初学者来说,Maven下载安装也许是整个工具使用过程中最麻烦的一步。在下载Maven时,我们需要知道下载的是哪个版本、所需文件的大小、下载的网站等。本文就详细介绍如何从官方网站下载Maven 3.8.5 for Windows。 首先,我们需要打开Maven官方网站 http://maven.apache.org/download.cgi。在Maven下载页面中,我们可以找到最新版本的Maven程序包和相关文件。找到"Maven 3.8.5"版本,并单击 "apache-maven-3.8.5-bin.zip" 下载链接。 下载完成后,解压文件并将解压后的文件移动到一个电脑易于访问的位置。在Windows上,建议将文件移动到C:/,这样能够方便地访问并配置环境变量。 配置Maven的环境变量需要在系统属性设置中进行。步骤如下: 1. 右键单击此电脑 /我的电脑,选择属性。 2. 选择“高级系统设置”> 环境变量。 3. 在系统变量中,找到“Path”变量,单击编辑。添加Maven bin目录的路径,比如:C:/apache-maven-3.8.5/bin。 4. 确认所有更改后,重启电脑。 完成以上步骤后,我们就可以在命令行中使用Maven了。我们可以通过提供“mvn -v”命令来验证是否正确安装Maven。 总之,下载Maven 3.8.5 for Windows需要从Maven官方网站下载文件,并按照上述步骤进行安装和配置。具体步骤简单,但务必注意细节。此外,为方便起见,建议提前了解一些Maven的基本知识,以便更好的理解和使用该工具。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值