maven 从0-1

一、先介绍下maven(麦文)  !!!不是妈文

Maven是一种流行的项目管理工具,它主要用于构建、依赖管理和项目生命周期管理。以下是Maven的主要作用:

  1. 依赖管理:Maven可以帮助你管理项目所需的外部依赖库。通过在项目的配置文件(pom.xml)中声明依赖,Maven能够自动下载并管理这些依赖。它能够解决依赖冲突问题,并确保项目使用的依赖库的版本一致性。

  2. 构建管理:Maven提供了一种结构化的方式来定义项目的构建过程。通过配置插件和目标,你可以使用Maven执行各种构建任务,如编译源代码、运行单元测试、打包应用程序等。Maven使用基于约定的目录结构,并且提供了一系列标准化的构建生命周期,使得构建过程更加简化和可维护。

  3. 项目生命周期管理:Maven引入了项目生命周期的概念,将构建过程划分为不同的阶段(如编译、测试、打包、部署等),每个阶段都与特定的构建目标相关联。你可以根据项目需求配置和扩展生命周期,以满足特定的构建要求。

  4. 应用程序打包和部署:Maven可以将项目打包成各种形式的分发包,如JAR、WAR、EAR等。通过配置插件和目标,你可以指定所需的打包方式,并且可以自定义构建过程中涉及到的资源文件和配置。另外,Maven还能够帮助你将项目部署到服务器或仓库中。

  5. 团队协作:Maven提供了一种标准化的项目结构和依赖管理机制,使得团队开发更加统一和协调。团队成员只需要关注项目的核心代码,而无需手动下载和管理依赖库。同时,Maven也支持多模块项目的管理,可以帮助团队更好地组织和管理复杂的项目结构。

二、maven的优点:

  1. 标准化和约定优于配置: Maven 强调一种标准化的项目结构和约定,使得不同开发者之间更容易理解和维护项目。通过遵循约定优于配置的原则,开发者可以将更多精力放在项目的实际开发上,减少冗余的配置工作。

  2. 依赖管理: Maven 提供了强大的依赖管理功能,使得开发者能够轻松地引入、更新和管理项目的外部依赖库。Maven 能够自动解析依赖关系,并确保每个依赖项的正确版本被使用,从而提高了项目的可靠性和稳定性。

  3. 生命周期管理: Maven 定义了一套清晰的项目生命周期,包括各个构建阶段(如编译、测试、打包、部署等)。开发者可以根据项目需求在 Maven 的项目描述文件中配置对应的插件,从而定义和管理项目的生命周期。这使得构建过程更加规范和可控。

  4. 可扩展性和丰富的插件生态系统: Maven 拥有庞大且活跃的插件生态系统,提供了各种功能强大的插件,如静态代码分析、代码生成、部署等。开发者可以根据项目需求选择适合的插件,扩展 Maven 的功能,满足特定的构建需求。

  5. 跨平台和多模块支持: Maven 是基于 Java 的工具,因此可以在不同的操作系统上运行。同时,Maven 支持多模块项目的构建,使得大型项目的管理更加灵活和可维护。通过将项目拆分成多个模块,每个模块可以独立构建和测试,并且可以共享依赖项和配置。

  6. 社区支持和文档资源丰富: Maven 是一个开源项目,拥有庞大的社区支持。社区中有许多经验丰富的开发者分享他们的经验和解决方案,提供了大量的文档和教程资源,帮助新手快速上手并解决遇到的问题。

三、maven的生命周期

Maven的生命周期是指一系列的构建阶段,用于管理和执行项目的构建过程。它将构建过程划分为不同的阶段,并根据每个阶段执行相应的目标。以下是Maven的生命周期及其阶段的详细解释:

  1. Clean生命周期:该生命周期负责清理项目中生成的文件。包括clean阶段。

  2. Default生命周期:该生命周期是Maven的主要生命周期,负责构建和部署项目。包括以下阶段:

    • validate:验证项目是否正确且所有必要信息可用。
    • compile:编译项目的源代码。
    • test:运行单元测试代码。
    • package:将编译后的代码打包成可发布的格式,如JAR、WAR等。
    • verify:对集成测试结果进行检查,以确保符合质量标准。
    • install:将项目安装到本地仓库,作为其他项目的依赖项。
    • deploy:将项目部署到远程仓库或者其他环境中。
  3. Site生命周期:该生命周期用于生成和发布项目的站点文档。包括以下阶段:

    • site:生成项目的站点文档。
    • site-deploy:将生成的站点文档部署到服务器上。

四、maven的项目结构

Maven使用一种约定的项目结构,可以帮助我们更好地组织和管理项目。一个标准的Maven项目结构如下:

  • src/main/java:主要用于存放项目的Java源代码。
  • src/main/resources:用于存放项目的资源文件,如配置文件、数据库脚本等。
  • src/main/webapp:如果是Web项目,该目录用于存放Web应用的静态资源和WEB-INF目录。
  • src/test/java:用于存放测试代码。
  • src/test/resources:用于存放测试相关的资源文件。
  • pom.xml:Maven项目的核心配置文件,定义了项目的依赖、构建配置等。

五、下载安装maven

1.首先到maven官网中:maven官网下载

 点击之后选择下载安装的路径,安装完成之后解压(!!!解压的位置不能含中文)

 六、配置环境变量

 打开之后找到高级系统设置

选择环境变量

 

 点击新建

变量名填:  MAVEN_HOME

变量值就是maven所在目录 建议直接复制粘贴(最稳妥的方式)

 找到Path点击编辑

 

 然后点击新建将  %MAVEN_HOME%\bin  添加进去然后保存

接下来就打开cmd命令窗口

输入 mvn --version 如下图,显示Maven相关信息,则表示配置生效。

七、maven的配置

先打开maven安装的路径打开conf目录

打开settings.xml文件

以下是我自己的setting.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">
  <!-- 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>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror> 

     
     
        <!-- 中央仓库1 -->
        <mirror>
            <id>repo1</id>
             <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo1.maven.org/maven2/</url>
         </mirror>
     
         <!-- 中央仓库2 -->
         <mirror>
             <id>repo2</id>
             <mirrorOf>central</mirrorOf>
             <name>Human Readable Name for this Mirror.</name>
             <url>http://repo2.maven.org/maven2/</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>

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

然后就可以开始使用了!!!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值