首页 > 其他分享 >Maven依赖管理项目构建工具的安装与配置

Maven依赖管理项目构建工具的安装与配置

时间:2024-09-03 15:27:36浏览次数:7  
标签:compile 依赖 spring jar Maven 构建 RELEASE org com

一、Maven简介

1、为什么学习Maven

1.1、Maven是一个依赖管理工具

①jar 包的规模

随着我们使用越来越多的框架,或者框架封装程度越来越高,项目中使用的jar包也越来越多。项目中,一个模块里面用到上百个jar包是非常正常的。

比如下面的例子,我们只用到 SpringBoot、SpringCloud 框架中的三个功能:

  • Nacos 服务注册发现
  • Web 框架环境
  • 视图模板技术 Thymeleaf

最终却导入了 106 个 jar 包:

org.springframework.security:spring-security-rsa:jar:1.0.9.RELEASE:compile
com.netflix.ribbon: ribbon:jar:2.3.0:compile
org.springframework.boot:spring-boot-starter-thymeleaf:jar:2.3.6.RELEASE:compile
commons-configuration:commons-configuration:jar:1.8:compile
org.apache.logging.log4j:log4j-api:jar:2.13.3:compile
org.springframework:spring-beans:jar:5.2.11.RELEASE:compile
org.springframework.cloud:spring-cloud-starter-netflix-ribbon:jar:2.2.6.RELEASE:compile
org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.39:compile
com.alibaba.cloud:spring-cloud-alibaba-commons:jar:2.2.6.RELEASE:compile
org.bouncycastle:bcprov-jdk15on:jar:1.64:compile
org.springframework.security:spring-security-crypto:jar:5.3.5.RELEASE:compile
org.apache.httpcomponents:httpasyncclient:jar:4.1.4:compile
com.google.j2objc:j2objc-annotations:jar:1.3:compile
com.fasterxml.jackson.core:jackson-databind:jar:2.11.3:compile
io.reactivex:rxjava:jar:1.3.8:compile
ch.qos.logback:logback-classic:jar:1.2.3:compile
org.springframework:spring-web:jar:5.2.11.RELEASE:compile
io.reactivex:rxnetty-servo:jar:0.4.9:runtime
org.springframework:spring-core:jar:5.2.11.RELEASE:compile
io.github.openfeign.form:feign-form-spring:jar:3.8.0:compile
io.github.openfeign.form:feign-form:jar:3.8.0:compile
com.netflix.ribbon:ribbon-loadbalancer:jar:2.3.0:compile
org.apache.httpcomponents:httpcore:jar:4.4.13:compile
org.thymeleaf.extras:thymeleaf-extras-java8time:jar:3.0.4.RELEASE:compile
org.slf4j:jul-to-slf4j:jar:1.7.30:compile
com.atguigu.demo:demo09-base-entity:jar:1.0-SNAPSHOT:compile
org.yaml:snakeyaml:jar:1.26:compile
org.springframework.boot:spring-boot-starter-logging:jar:2.3.6.RELEASE:compile
io.reactivex:rxnetty-contexts:jar:0.4.9:runtime
org.apache.httpcomponents:httpclient:jar:4.5.13:compile
io.github.openfeign:feign-core:jar:10.10.1:compile
org.springframework.boot:spring-boot-starter-aop:jar:2.3.6.RELEASE:compile
org.hdrhistogram:HdrHistogram:jar:2.1.9:compile
org.springframework:spring-context:jar:5.2.11.RELEASE:compile
commons-lang:commons-lang:jar:2.6:compile
io.prometheus:simpleclient:jar:0.5.0:compile
ch.qos.logback:logback-core:jar:1.2.3:compile
org.springframework:spring-webmvc:jar:5.2.11.RELEASE:compile
com.sun.jersey:jersey-core:jar:1.19.1:runtime
javax.ws.rs:jsr311-api:jar:1.1.1:runtime
javax.inject:javax.inject:jar:1:runtime
org.springframework.cloud:spring-cloud-openfeign-core:jar:2.2.6.RELEASE:compile
com.netflix.ribbon:ribbon-core:jar:2.3.0:compile
com.netflix.hystrix:hystrix-core:jar:1.5.18:compile
com.netflix.ribbon:ribbon-transport:jar:2.3.0:runtime
org.springframework.boot:spring-boot-starter-json:jar:2.3.6.RELEASE:compile
org.springframework.cloud:spring-cloud-starter-openfeign:jar:2.2.6.RELEASE:compile
com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.3:compile
com.sun.jersey.contribs:jersey-apache-client4:jar:1.19.1:runtime
io.github.openfeign:feign-hystrix:jar:10.10.1:compile
io.github.openfeign:feign-slf4j:jar:10.10.1:compile
com.alibaba.nacos:nacos-client:jar:1.4.2:compile
org.apache.httpcomponents:httpcore-nio:jar:4.4.13:compile
com.sun.jersey:jersey-client:jar:1.19.1:runtime
org.springframework.cloud:spring-cloud-context:jar:2.2.6.RELEASE:compile
org.glassfish:jakarta.el:jar:3.0.3:compile
org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.11.3:compile
org.springframework.cloud:spring-cloud-commons:jar:2.2.6.RELEASE:compile
org.aspectj:aspectjweaver:jar:1.9.6:compile
com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:jar:2.2.6.RELEASE:compile
com.google.guava:listenablefuture:jar:9999.0-empty-to-avoid-conflict-with-guava:compile
com.alibaba.spring:spring-context-support:jar:1.0.10:compile
jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
org.bouncycastle:bcpkix-jdk15on:jar:1.64:compile
com.netflix.netflix-commons:netflix-commons-util:jar:0.3.0:runtime
com.fasterxml.jackson.core:jackson-annotations:jar:2.11.3:compile
com.google.guava:guava:jar:29.0-jre:compile
com.google.guava:failureaccess:jar:1.0.1:compile
org.springframework.boot:spring-boot:jar:2.3.6.RELEASE:compile
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.3:compile
com.atguigu.demo:demo08-base-api:jar:1.0-SNAPSHOT:compile
org.springframework.cloud:spring-cloud-starter-netflix-archaius:jar:2.2.6.RELEASE:compile
org.springframework.boot:spring-boot-autoconfigure:jar:2.3.6.RELEASE:compile
org.slf4j:slf4j-api:jar:1.7.30:compile
commons-io:commons-io:jar:2.7:compile
org.springframework.cloud:spring-cloud-starter:jar:2.2.6.RELEASE:compile
org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.39:compile
io.reactivex:rxnetty:jar:0.4.9:runtime
com.fasterxml.jackson.core:jackson-core:jar:2.11.3:compile
com.google.code.findbugs:jsr305:jar:3.0.2:compile
com.netflix.archaius:archaius-core:jar:0.7.6:compile
org.springframework.boot:spring-boot-starter-web:jar:2.3.6.RELEASE:compile
commons-codec:commons-codec:jar:1.14:compile
com.netflix.servo:servo-core:jar:0.12.21:runtime
com.google.errorprone:error_prone_annotations:jar:2.3.4:compile
org.attoparser:attoparser:jar:2.0.5.RELEASE:compile
com.atguigu.demo:demo10-base-util:jar:1.0-SNAPSHOT:compile
org.checkerframework:checker-qual:jar:2.11.1:compile
org.thymeleaf:thymeleaf-spring5:jar:3.0.11.RELEASE:compile
commons-fileupload:commons-fileupload:jar:1.4:compile
com.netflix.ribbon:ribbon-httpclient:jar:2.3.0:compile
com.netflix.netflix-commons:netflix-statistics:jar:0.1.1:runtime
org.unbescape:unbescape:jar:1.1.6.RELEASE:compile
org.springframework:spring-jcl:jar:5.2.11.RELEASE:compile
com.alibaba.nacos:nacos-common:jar:1.4.2:compile
commons-collections:commons-collections:jar:3.2.2:runtime
javax.persistence:persistence-api:jar:1.0:compile
com.alibaba.nacos:nacos-api:jar:1.4.2:compile
org.thymeleaf:thymeleaf:jar:3.0.11.RELEASE:compile
org.springframework:spring-aop:jar:5.2.11.RELEASE:compile
org.springframework.boot:spring-boot-starter:jar:2.3.6.RELEASE:compile
org.springframework.boot:spring-boot-starter-tomcat:jar:2.3.6.RELEASE:compile
org.springframework.cloud:spring-cloud-netflix-ribbon:jar:2.2.6.RELEASE:compile
org.springframework:spring-expression:jar:5.2.11.RELEASE:compile
org.springframework.cloud:spring-cloud-netflix-archaius:jar:2.2.6.RELEASE:compile

而如果使用 Maven 来引入这些 jar 包只需要配置三个『依赖』:

<!-- Nacos 服务注册发现启动器 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- web启动器依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 视图模板技术 thymeleaf -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

②jar包的来源问题

  • 这个jar包所属技术的官网。官网通常是英文界面,网站的结构又不尽相同,甚至找到下载链接还发现需要通过特殊的工具下载。
  • 第三方网站提供下载。问题是不规范,在使用过程中会出现各种问题。
  • jar包的名称
  • jar包的版本
  • jar包内的具体细节
  • 而使用 Maven 后,依赖对应的 jar 包能够自动下载,方便、快捷又规范。

③jar包的导入问题

在web工程中,jar包必须存放在指定位置:

Maven依赖管理项目构建工具的安装与配置_jar

在使用Maven之后,通过配置依赖(jar包)的坐标,查找本地仓库中相应jar包,若本地仓库没有,则统一从镜像网站或中央仓库中下载:

Maven依赖管理项目构建工具的安装与配置_maven_02

④jar包之间的依赖

框架中使用的 jar 包,不仅数量庞大,而且彼此之间存在错综复杂的依赖关系。依赖关系的复杂程度,已经上升到了完全不能靠人力手动解决的程度。另外,jar 包之间有可能产生冲突。进一步增加了我们在 jar 包使用过程中的难度。

下面是前面例子中 jar 包之间的依赖关系:

Maven依赖管理项目构建工具的安装与配置_maven_03

而实际上 jar 包之间的依赖关系是普遍存在的,如果要由程序员手动梳理无疑会增加极高的学习成本,而这些工作又对实现业务功能毫无帮助。

而使用 Maven 则几乎不需要管理这些关系,极个别的地方调整一下即可,极大的减轻了我们的工作量。

1.2、Maven是一个构建工具

①你没有注意过的构建

你可以不使用 Maven,但是构建必须要做。当我们使用 IDEA 进行开发时,构建是 IDEA 替我们做的。

Maven依赖管理项目构建工具的安装与配置_jar_04

②脱离 IDE 环境仍需构建

Maven依赖管理项目构建工具的安装与配置_jar_05

1.3、结论
  • 管理规模庞大的 jar 包,需要专门工具。
  • 脱离 IDE 环境执行构建操作,需要专门工具。

2. Maven介绍

https://maven.apache.org/what-is-maven.html

Maven 是一款为 Java 项目管理构建、依赖管理的工具(软件),使用 Maven 可以自动化构建、测试、打包和发布项目,大大提高了开发效率和质量。

Maven就是一个软件,掌握安装、配置、以及基本功能 (项目构建、依赖管理) 的理解和使用即可!

  1. 依赖管理:
    Maven 可以管理项目的依赖,包括自动下载所需依赖库、自动下载依赖需要的依赖并且保证版本没有冲突、依赖版本管理等。通过 Maven,我们可以方便地维护项目所依赖的外部库,避免版本冲突和转换错误等,而我们仅仅需要编写配置即可。
  2. 构建管理:
    项目构建是指将源代码、配置文件、资源文件等转化为能够运行或部署的应用程序或库的过程
    Maven 可以管理项目的编译、测试、打包、部署等构建过程。通过实现标准的构建生命周期,Maven 可以确保每一个构建过程都遵循同样的规则和最佳实践。同时,Maven 的插件机制也使得开发者可以对构建过程进行扩展和定制。主动触发构建,只需要简单的命令操作即可。

场景1: 例如我们项目需要第三方依赖如:Druid连接池、MySQL数据库驱动和Jackson JSON等处理。那么我们可以将想要的依赖项的信息编写到Maven工程的配置文件,Maven就会自动下载并复制这些依赖项到项目中,无需自己导入jar包,管理jar!

场景2: 项目完成开发,我们想要打成war部署到服务器中,使用maven的构建命令可以快速打包!节省大量时间!

3. Maven软件工作原理模型图(了解)

Maven依赖管理项目构建工具的安装与配置_jar_06

二、Maven安装和配置

1. Maven安装

https://maven.apache.org/docs/history.html

各个工具选用版本:

工具

版本

Maven

3.8.8

JDK

17

IDEA

2022.2

安装条件: maven需要本机安装java环境、必需包含java_home环境变量!

软件安装: 右键解压即可(绿色免安装)

软件结构:

Maven依赖管理项目构建工具的安装与配置_maven_07

bin:含有Maven的运行脚本

boot:含有plexus-classworlds类加载器框架

conf:含有Maven的核心配置文件

lib:含有Maven运行时所需要的Java类库

LICENSE、NOTICE、README.txt:针对Maven版本,第三方软件等简要介绍

2. Maven环境配置

  1. 配置MAVEN_HOME
  2. 配置Path
  3. 命令测试(cmd窗口)
mvn -v 
# 输出版本信息即可,如果错误,请仔细检查环境变量即可!

3. Maven功能配置

我们需要需改maven/conf/settings.xml配置文件,来修改maven的一些默认配置。我们主要休要修改的有三个配置:

1.依赖本地缓存位置(本地仓库位置)

2.maven下载镜像

3.maven选用编译项目的jdk版本

  1. 配置本地仓库地址
<!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
 <!-- conf/settings.xml 55行 -->
 <localRepository>D:\maven-repository</localRepository>
  1. 配置国内阿里镜像
<!--在mirrors节点(标签)下添加中央仓库镜像 160行附近-->
<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>
  1. 配置jdk17版本项目构建
<!--在profiles节点(标签)下添加jdk编译版本 268行附近-->
<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>

4. IDEA配置本地Maven软件

我们需要将配置好的maven软件,配置到idea开发工具中即可! 注意:idea工具默认自带maven配置软件,但是因为没有修改配置,建议替换成本地配置好的maven!

选择本地maven软件

Maven依赖管理项目构建工具的安装与配置_maven_08

注意

1、如果本地仓库地址不变化,只有一个原因,就是maven/conf/settings.xml配置文件编写错误!仔细检查即可!

2、一定保证User settings file对应之前修改的settings.xml的路径,若 不一致,选中Override复选框,手动选择配置文件


标签:compile,依赖,spring,jar,Maven,构建,RELEASE,org,com
From: https://blog.51cto.com/xiaobear/11908599

相关文章

  • 基于IDEA创建Maven工程及注意事项
    Java全能学习+面试指南:https://javaxiaobear.cn1.概念梳理Maven工程的GAVPMaven工程相对之前的项目,多出一组gavp属性,gav需要我们在创建项目的时候指定,p有默认值,我们先行了解下这组属性的含义:Maven中的GAVP是指GroupId、ArtifactId、Version、Packaging等四个属性的缩写,其中前......
  • 基于IDEA进行Maven工程构建
    Java全能学习+面试指南:https://javaxiaobear.cn1.构建概念和构建过程项目构建是指将源代码、依赖库和资源文件等转换成可执行或可部署的应用程序的过程,在这个过程中包括编译源代码、链接依赖库、打包和部署等多个步骤。项目构建是软件开发过程中至关重要的一部分,它能够大大提高软......
  • 从零开始:用Flask和UIAutomation构建RPA微信自动群发工具
    文章目录前言一、环境搭建系统要求flask安装uiautomation安装二、群发功能的实现要导入的包1.构建WxOperation类2.搜寻被发送人3.发送文本4.发送文件5.接收消息三、构建后端服务要导入的包文件框架1.index.html文件2.app.py四、成果展示总结前言在这个数字化......
  • sonarqube 项目内maven插件使用
    一般建议保持项目jdk和sonarqube的jdk版本呢一致,sonarqube支持的版本最低的也是jdk11,如果要使用sonarqube,建议项目尽量上jdk17。sonarmaven插件依赖<!--属性配置--><properties><!--sonarqube配置--><sonar.projectKey>TestExample</sonar.p......
  • php邮箱服务器怎么搭建?如何构建服务器?
    php邮箱服务器配置教程指南?php邮件服务器如何搭建?搭建一个稳定高效的php邮箱服务器,不仅可以提升邮件传输的效率,还能增强数据的安全性。那么,如何着手搭建这样一个服务器呢?AokSend将详细探讨php邮箱服务器的搭建步骤。php邮箱服务器:配置环境配置php邮箱服务器环境是搭建过程......
  • 关于maven
    1、依赖范围① complie默认范围,编译和运行时失效,打包的时候也会打包。② provided编译和测试有效、运行时无效,打包不打包。③runtime编译不生效、运行生效。④ test 编译和运行测试有效。实际运行不生效。2、依赖冲突调解① 第一原则:最短路径原则② 第二原......
  • PyTorch构建模型训练模块
    前面几篇分别是数据导入模块,模型类的构建模块,损失函数模块,优化器模块,上述模块搭建好后建立训练模块,将上述几个模块在训练模块中调用完成训练。这个不太好解释直接看模块示例: 上面是训练模块,分别传入训练总轮次steps,实例化模型model,损失函数loss_func,但是下面训练的时候它将损......
  • Bean的循环依赖问题
    什么是Bean的循环依赖Bean的循环依赖,就是A对象中有B属性,B对象中有A属性。即我依赖你,你也依赖我。也就是两个或多个对象之间相互引用成环。比如:丈夫类Husband,妻子类Wife,Husband中有Wife的引用,Wife中有Husband的引用。packagecw.spring.bean;/***ClassName:Wife*P......
  • Unity 脚本编译和构建(Build)说明(官方文档翻译校正)
    目录脚本重要概念事件函数的执行顺序脚本生命周期概述流程图的范围脚本生命周期流程图一般原则第一次场景加载(FirstSceneload)场景加载和卸载之前(Beforesceneloadandunload)Editor第一帧更新之前(Beforethefirstframeupdate)帧之间(Inbetweenframes)更新顺序(Upda......
  • 有手就会之使用Dify构建RAG聊天应用(基于私有知识库和搜索引擎)
    之前我的文章里写的是通过langchain来构建RAG应用,对于很多人来说。langchain作为一个框架上手难度大,代码不够直观。但是通过dify你将学会可视化搭建工作流。什么是dify?Dify是一款开源的大语言模型(LLM)应用开发平台。它融合了后端即服务(BackendasService)和LLMOps的......