Maven基础学习---1、Maven的作用、什么是Maven

简介: Maven基础学习---1、Maven的作用、什么是Maven

1、Maven的作用

1.1 Maven作为依赖管理工具

1、jar包的规模

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

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

(1)Nacos服务注册发现

(2)Web框架环境

(3)视图模板技术 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:compilecom.netflix.hystrix:hystrix-core:jar:1.5.18:compilecom.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:compileorg.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>

2、jar包的来源

这个jar包所属技术的官网。官网通常是英文界面,网站的结构又不尽相同,甚至找到下载链接还发现需要通过特殊的工具下载。

第三方网站提供下载。问题是不规范,在使用过程中会出现各种问题。

(1)jar包的名称

(2)jar包的版本

(3)jar包内的具体细节

而使用Maven后,依赖对应的jar包能够自动下载,方便、快捷又规范。

3、jar包之间的依赖关系

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

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

84c32f9968b7475cad3381efb6387f51.png

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

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

1.2 Maven作为构建管理工具

1、你没有注意过的构建

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

2、脱离IDE环境仍需要构建

91299752b95c4d7b9275c75718a5bea2.png

3、结论

(1)管理规模庞大的jar包,需要专门的工具

(2)脱离IDE环境执行构建操作,需要专门的工具。

2、什么是Maven

Maven是Apache软件基金会组织维护的一款专门为Java项目提供构建依赖管理支持的工具。

2.1 构建

1、Java项目开发过程中,构建指的是使用[原材料生产产品]的过程。

(1)原材料

Java源码

基于HTML的Thymeleaf文件

图片

配置文件

(2)产品

一个可以在服务器上运行的项目

2、构建过程包含的主要环节:

(1)清理:删除上一次构建的结果,为下一次构建做好准备。

(2)编译:Java源程序编译成*.class字节码文件。

(3)测试:运行提前准备好的的测试程序。

(4)报告:针对刚才测试的结果生成一个全面的信息。

(5)打包:

Java工程包:jar包

Web工程包:war包

(6)安装:把一个Maven工程经过打包操作生产的jar包或war包存入Maven的本地仓库。

(7)部署:

部署jar包:把一个jar包部署到一个Nexus私服服务器上。

部署war包:借助相关的Maven插件(例如cargo),将war包部署到Tomcat服务器上。

2.2 依赖

如果A工程里面用到了B工程的类、接口、配置文件等等这样的资源,那么我们就可以说A依赖B。例如:

junit-4.12 依赖 hamcrest-core-1.3

thymeleaf-3.0.12.RELEASE 依赖 ognl-3.1.26

thymeleaf-3.0.12.RELEASE 依赖 ognl-3

thymeleaf-3.0.12.RELEASE 依赖 attoparser-2.0.5.RELEASE

thymeleaf-3.0.12.RELEASE 依赖 attoparser-2.0.5.RELEAS

thymeleaf-3.0.12.RELEASE 依赖 slf4j-api-1.7.26

依赖管理中要解决的具体问题:

(1)jar包的下载:使用Maven后,jar包会规范的从远程仓库下载到本地。

(2)jar包之间的依赖:通过依赖的传递性自动完成

(3)jar包之间的冲突:通过对依赖的配置进行调整,让某些jar包不会被导入

2.3 Maven的工作机制

473a471fc875475ba7b9ce7c7e35233d.png


相关文章
|
29天前
|
XML Java Shell
【深入浅出Maven开发实战】「入门教程系列」带你零基础学习和开发使用Maven开发工具实战指南(实战技术总结)(一)
【深入浅出Maven开发实战】「入门教程系列」带你零基础学习和开发使用Maven开发工具实战指南(实战技术总结)
76 1
|
8月前
|
Java 应用服务中间件 Maven
零基础入门学习Maven(一)
零基础入门学习Maven(一)
|
8月前
|
Java 调度 Maven
Maven基础学习---5、其他核心概念
Maven基础学习---5、其他核心概念
|
8月前
|
存储 JavaScript Java
Maven基础学习---3、Maven的使用(命令行)
Maven基础学习---3、Maven的使用(命令行)
|
4月前
|
XML Java 大数据
答应粉丝的Maven仓库学习笔记,今天它来了 一起来学习快速入门Maven
答应粉丝的Maven仓库学习笔记,今天它来了 一起来学习快速入门Maven
78 1
|
8月前
|
Java Maven 开发工具
Maven基础学习---4、Maven的使用(IDEA)
Maven基础学习---4、Maven的使用(IDEA)
|
8月前
|
Java Apache Maven
Maven基础学习---2、Maven安装与配置
Maven基础学习---2、Maven安装与配置
|
8月前
|
Java Maven 数据安全/隐私保护
Maven学习(四)
Maven学习
34 0
|
8月前
|
Java 数据库连接 Maven
Maven学习(三)
Maven学习
52 0
|
8月前
|
XML Java Maven
Maven学习(二)
Maven学习
37 0

热门文章

最新文章