快速上手Maven

113 阅读2分钟

Maven

单体项目:目前的项目都是单体项目,一个项目的结构。 父子依赖项目:可以嵌套很多层的项目结构,(父亲-儿子-孙子-重孙子…|项目套项目|大项目套小项目|等)是必须要会的!后期用springcloud开发就要用父子依赖结构的项目

maven父工程

maven镜像

使用镜像代替中央仓库

国内开发人员由于网络原因,直接从中央仓库下载构件时,速度较慢或不稳定,我们通常会使用中央仓库的国内镜像站来解决该问题。 配置 Maven 镜像的方法也非常的简单,我们只需要在 Maven 安装目录中 setting.xml 文件的 mirrors 节点中,使用 mirror 标签添加镜像的相关信息即可。

maven依赖管理

通过groupId, artifactId,version 这三个坐标确定一个项目,一个项目也可以作为依赖,通过坐标指定项目

注意:

optional 可选依赖 选择是否隐藏自己的依赖

exclusions 排除自己依赖项目的依赖

scope 管理依赖范围,打包时,是否把项目的依赖打进包里 默认在runtime,test,package.依赖范围也有传递性

maven插件和声明周期

生命周期

compile 编译

install 安装到maven仓库中

项目构建声明周期每一个阶段都配一个插件,可以在pom.xml文件中配置插件,增加功能

    <!--    打包部署必须使用-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

maven模块管理

把数据层,控制层,业务层分别划分为一个模块

所有模块由一个聚合模块管理 modules,可以快速构建多个模块

<packaging>pom</packaging>
<modules>
	<module></module>
</modules>

模块类型

pom 只提供pom文件 ,父模块

war 打war包

jar 打jar包

模块继承

子模块继承父模块依赖的版本,父模块基本都是聚合模块

父模块

	<modules>
        <module>blog-api</module>
    </modules>
    <!--    声明为聚合模块-->
    <packaging>pom</packaging>


    <dependencyManagement>
        <dependencies>
        </dependencies>
    </dependencyManagement>

子模块

子模块应该groupId 和 version与父模块一样,所以只用写artifactId

<!--声明为父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/>    <!--相对路径-->
    </parent>

maven属性

默认${version}为工程的版本

自定义属性相当于自定义变量

//定义
<properties>
	<sping.version>5.1.9</sping.version>
</properties>
//使用
<version>${sping.version}</version>

maven属性有很多,系统属性、环境变量、自定义属性等

定义多环境(问题)

‘profiles

开发环境/生产环境

使用环境有两种方式,一种是设置activation设置默认启动,另一种是用命令指定环境



<profiles>
         <profile>
              //环境名为   jdk-1.8
              <id>jdk-1.8</id>
             
            
              <activation>
                <activeByDefault>true</activeByDefault> //设置为默认启动环境
                <jdk>1.8</jdk>          //???????????????
              </activation>
              <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
              </properties>
         </profile>
  </profiles>

私服

模仿中央仓库创建一个自己的仓库 -----私服

Maven依赖项

lombook 简化 类set 、get 方法


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

hutool 时间工具类

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.11</version>
        </dependency>