maven高级使用教程

文章目录

      • 一、简介
        • 1、maven是Apache软件基金会的一款依赖管理和快速构建的软件。
        • 2、仓库:
        • 3、坐标:
        • 4、修改settings.conf设置阿里云镜像
        • 5、maven项目构建命令
        • 6、idea结合maven
        • 7、pom.xml基本结构
      • 二、依赖
        • 1、依赖传递:
        • 2、依赖传递冲突:
        • 3、可选依赖:隐藏所依赖的资源。实际上引用了,但是别的项目看不到。
        • 4、排除依赖:断掉依赖传递。
        • 5、依赖范围:
      • 三、生命周期与插件
      • 四、聚合与继承
        • 1、聚合:
        • 2、继承:
        • 3、聚合和继承的区别:
      • 五、属性:
      • 六、版本管理:
      • 七、私服

一、简介

1、maven是Apache软件基金会的一款依赖管理和快速构建的软件。

安装与下载:
从官网下载压缩包,直接解压缩就可以。

目录结构:
conf 配置文件目录
lib jar包目录
bin 二进制文件目录
boot 启动目录

2、仓库:

本地仓库:在本机的资源仓库
远程仓库:不在本机的仓库。包括私服仓库、中央仓库。
私服仓库:公司搭建的资源仓库。
中央仓库:maven团队维护的仓库。

3、坐标:

定位jar包的方式
由GAV构成:

<group>:组织id
<artifactid>:项目id
<version>:版本
4、修改settings.conf设置阿里云镜像
<mirrors>
    <mirror>
        <id>alimavenid>
         <name>aliyun mavenname>
         <url>http://maven.aliyun.com/nexus/content/groups/public/url>
         <mirrorOf>centralmirrorOf>
     mirror>
mirrors>
5、maven项目构建命令
mvn  compile  编译
mvn  clean   清理
mvn  packaging   打包
mvn  test   测试
mvn  install  安装到本地仓库
6、idea结合maven

在idea设置中配置maven的安装路径、settings.conf和本地仓库的位置

7、pom.xml基本结构


<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/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0modelVersion>
    
    <pacagking>jarpacagking>
     
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.1.RELEASEversion>
    parent>
    
    <groupId>com.yybgroupId>
    <artifactId>redis-springbootartifactId>
    
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>11maven.compiler.source>
        <maven.compiler.target>11maven.compiler.target>
    properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

二、依赖

1、依赖传递:

项目的依赖也有依赖。依赖会进行传递。我依赖的依赖也是我的依赖。在项目中直接引入的依赖是直接依赖,依赖的依赖是间接依赖。

2、依赖传递冲突:

路径优先:当依赖中 出现相同的资源时,层级越深优先级越低,层级越少优先级越高。
声明优先:当同级中出现相同的资源,配置顺序靠前的覆盖配置顺序靠后的。
maven高级使用教程_第1张图片

3、可选依赖:隐藏所依赖的资源。实际上引用了,但是别的项目看不到。
<optional>trueoptional>
4、排除依赖:断掉依赖传递。
<exceptions>
    <exception>
        <groupId>...groupId>
        <artifactId>...artifactId>
    <exception>
exceptions>
5、依赖范围:
<scope>...</scope>

compile(默认)、test、provided、runtime

compile(默认) test provided runtime
主代码
测试代码
打包

三、生命周期与插件

(1) clean 清理工作
(2) compile test package install
(3) site 产生报告,发布站点

四、聚合与继承

1、聚合:

在一个模块中管理其他模块。
聚合模块通常是空模块,没有src文件夹,只有pom.xml文件,打包方式为pom。

<packaging>pompackaging>
<modules>
    <module>...module>
    <module>...module>
    <module>...module>    
modules>

打包方式有: jar(java项目)、war(web项目)、pom(聚合项目)
普通模块默认为jar,聚合模块为pom

2、继承:

子工程继承父工程,父工程管理子工程

在父工程的pom.xml中对依赖进行统一管理,在dependencesManager中声明依赖,而不是引入依赖。

<dependencesManager>
    <dependences>
        <dependence>
            <groupId>...groupId>
            <arifactid>...arifactid>
            <version>...version>
        dependence>
        <dependence>
            <groupId>...groupId>
            <arifactid>...arifactid>
            <version>...version>
        dependence>        
    dependences>
<dependencesManager>

在子工程的pom.xml中,实质地引入依赖,可以不写依赖的版本号,使用父工程pom.xml中指定的版本号,如果在子工程中指定版本号,则使用子工程的版本号。

<parent>
    <groupId>...groupId>
    <artifactId>...artifactId>
    <version>...version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>...artifactId>

 <dependences>
     <dependence>
         <groupId>...groupId>
         <arifactid>...arifactid>
     dependence>
     <dependence>
         <groupId>...groupId>
         <arifactid>...arifactid>
     dependence>        
dependences>
3、聚合和继承的区别:

聚合是用于 快速构建,继承是用于快速配置 ,聚合和继承可以不在一个模块里,但通常放在一个模块里。

五、属性:

属性可以理解为变量,在子工程中进行使用
在父工程中对版本进行统一管理。

  <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.complier.target>1.8maven.complier.target>
        <junit.version>4.12junit.version>
        <log4j.version>1.2.17log4j.version>
        <lombok.version>1.18.16lombok.version>
        <mysql.version>5.1.47mysql.version>
        <druid.version>1.1.16druid.version>
       <mybatis.spring.boot.version>1.3.0mybatis.spring.boot.version>
 properties>

在子工程中引用

<version>${junit.version}version>

六、版本管理:

release 正式发布版
snapshot 快照版本

版本号约定:
<主版本>.<次版本>.<增量版本>.<里程碑版本>

主版本:项目架构有重大变化
次版本:有较大的功能新增或变化,或全面系统的修复漏洞
增量版本:修复重大漏洞
里程碑版本:测试功能,与下一个正式版本相比,不是很稳定,有待更多地测试。

例:2.2.1.RELEASE

七、私服

私服是一种远程仓库,通常由公司在局域网搭建,使用nexus等工具。
这里展示使用docker运行nexus私服。

docker pull sonatype/nexus3
docker run -d -p 8081:8081 --name nexus sonatype/nexus3
http://ipAdress:8081

你可能感兴趣的:(#,Java,maven,java,intellij-idea)