• Maven(8) 实战总结


    文档更新地址:https://gitee.com/zhengqingya/java-developer-document
    Maven安装与配置见 https://zhengqing.blog.csdn.net/article/details/83956373

    一、初识Maven

    https://maven.apache.org

    项目构建管理工具

    1. 管理jar包依赖
    2. 编译、打包、发布java项目

    二、IDEA新建springboot项目

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    三、目录结构说明

    约定大于配置

    D:.
    └─demo
        │  .gitignore       -- git提交代码时忽略指定文件
        │  demo.iml         -- idea生成的该项目模块配置信息
        │  HELP.md
        │  pom.xml          -- maven配置(包含项目基本信息,如何构建,声明项目依赖等等)
        ├─.idea             -- idea生成的该项目的配置信息,包括历史记录,版本控制信息等。
        ├─.mvn              -- 为工程指定maven版本,统一该项目的开发环境 => 避免开发因版本差异引起的诡异错误  -- tips: 即有此配置可无需单独下载maven
        │  └─wrapper
        │          maven-wrapper.jar
        │          maven-wrapper.properties
        │          
        │  mvnw             -- maven liunx脚本  (与上面的`maven-wrapper`关联)  ex:`mvnw clean`       类似于单独安装maven后的`mvn clean`
        │  mvnw.cmd         -- maven windows脚本                              ex:`mvnw.cmd clean`
        ├─src               -- 项目源代码
        │  ├─main
        │  │  ├─java
        │  │  │  └─com
        │  │  │      └─zhengqing
        │  │  │          └─demo
        │  │  │                  DemoApplication.java
        │  │  │                  
        │  │  └─resources
        │  │      └─application.properties
        └─target            -- 编译文件目录
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在这里插入图片描述
    可删除.mvn&mvnw&mvnw.cmd使用自己安装的maven版本,即我们常见的项目结构

    四、常用命令

    基础命令

    # 查看版本
    mvn -v
    
    # 清理项目编译后产生的临时目录`target`
    mvn clean
    
    # 编译源代码 生成target目录
    mvn compile
    
    # 打包
    mvn package
    
    # 将指定jar包打包到本地仓库中
    mvn install
    mvn install:install-file -DgroupId=com.zhengqing -DartifactId=app-demo -Dversion=0.0.1.release -Dfile=/home/soft/app-jar/app-demo-2.0.0.jar -Dpackaging=jar
    
    # 发布到远程仓库,提供给别人下载依赖使用
    mvn deploy
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    项目实战命令

    # 跳过单元测试 打包
    mvn clean package -Dmaven.test.skip=true
    # -f 指定项目路径   ex: 对项目demo进行打包
    mvn -f ./demo clean package -Dmaven.test.skip=true
    
    • 1
    • 2
    • 3
    • 4

    五、POM

    1、项目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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.7.2version>
            <relativePath/> 
        parent>
    
        
        
        <groupId>com.zhengqinggroupId>
        
        <artifactId>maven-demoartifactId>
        
        <version>0.0.1-SNAPSHOTversion>
    
        
        <packaging>jarpackaging>
    
        
        <name>maven-demoname>
        
        <description>maven-demodescription>
    
        
        <properties>
            <java.version>1.8java.version>
        properties>
    
        
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                
                <scope>testscope>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    2、超级pom.xml

    ${MAVEN_HOME}\lib\maven-model-builder-3.8.6.jar\org\apache\maven\model\pom-4.0.0.xml
    定义maven的目录结构

    
    
    
    
    
    <project>
      <modelVersion>4.0.0modelVersion>
    
      <repositories>
        <repository>
          <id>centralid>
          <name>Central Repositoryname>
          <url>https://repo.maven.apache.org/maven2url>
          <layout>defaultlayout>
          <snapshots>
            <enabled>falseenabled>
          snapshots>
        repository>
      repositories>
    
      <pluginRepositories>
        <pluginRepository>
          <id>centralid>
          <name>Central Repositoryname>
          <url>https://repo.maven.apache.org/maven2url>
          <layout>defaultlayout>
          <snapshots>
            <enabled>falseenabled>
          snapshots>
          <releases>
            <updatePolicy>neverupdatePolicy>
          releases>
        pluginRepository>
      pluginRepositories>
    
      <build>
        <directory>${project.basedir}/targetdirectory>
        <outputDirectory>${project.build.directory}/classesoutputDirectory>
        <finalName>${project.artifactId}-${project.version}finalName>
        <testOutputDirectory>${project.build.directory}/test-classestestOutputDirectory>
        <sourceDirectory>${project.basedir}/src/main/javasourceDirectory>
        <scriptSourceDirectory>${project.basedir}/src/main/scriptsscriptSourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/javatestSourceDirectory>
        <resources>
          <resource>
            <directory>${project.basedir}/src/main/resourcesdirectory>
          resource>
        resources>
        <testResources>
          <testResource>
            <directory>${project.basedir}/src/test/resourcesdirectory>
          testResource>
        testResources>
        <pluginManagement>
          
          
          <plugins>
            <plugin>
              <artifactId>maven-antrun-pluginartifactId>
              <version>1.3version>
            plugin>
            <plugin>
              <artifactId>maven-assembly-pluginartifactId>
              <version>2.2-beta-5version>
            plugin>
            <plugin>
              <artifactId>maven-dependency-pluginartifactId>
              <version>2.8version>
            plugin>
            <plugin>
              <artifactId>maven-release-pluginartifactId>
              <version>2.5.3version>
            plugin>
          plugins>
        pluginManagement>
      build>
    
      <reporting>
        <outputDirectory>${project.build.directory}/siteoutputDirectory>
      reporting>
    
      <profiles>
        
        <profile>
          <id>release-profileid>
    
          <activation>
            <property>
              <name>performReleasename>
              <value>truevalue>
            property>
          activation>
    
          <build>
            <plugins>
              <plugin>
                <inherited>trueinherited>
                <artifactId>maven-source-pluginartifactId>
                <executions>
                  <execution>
                    <id>attach-sourcesid>
                    <goals>
                      <goal>jar-no-forkgoal>
                    goals>
                  execution>
                executions>
              plugin>
              <plugin>
                <inherited>trueinherited>
                <artifactId>maven-javadoc-pluginartifactId>
                <executions>
                  <execution>
                    <id>attach-javadocsid>
                    <goals>
                      <goal>jargoal>
                    goals>
                  execution>
                executions>
              plugin>
              <plugin>
                <inherited>trueinherited>
                <artifactId>maven-deploy-pluginartifactId>
                <configuration>
                  <updateReleaseInfo>trueupdateReleaseInfo>
                configuration>
              plugin>
            plugins>
          build>
        profile>
      profiles>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150

    六、scope依赖作用域

    pom.xml

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.7.0version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>com.zhengqing.commongroupId>
            <artifactId>commonartifactId>
            <version>1.0.0version>
            <scope>systemscope>
            <systemPath>${pom.basedir}/src/main/resources/lib/common-1.0.0.jarsystemPath>
        dependency>
        
        <dependency>
             <groupId>com.zhengqinggroupId>
             <artifactId>baseartifactId>
             <scope>providedscope>
         dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    compile

    默认值,适用于所有阶段,依赖会参与项目的编译、测试和运行阶段,属于强依赖。打包时,会打到包里去,随项目发布。

    runtime

    只在运行时使用。
    一般这种类库都是接口与实现相分离的类库,ex: JDBC类库,在编译之时仅依赖相关的接口,在具体的运行之时,才需要具体的mysql、oracle等等数据的驱动程序。 此类的驱动都是为runtime的类库。

    test

    只在测试时使用,用于编译和运行测试代码。不会随项目发布。

    provided

    该依赖可以参与编译、测试和运行等周期,与compile等同。
    区别: 在打包时,不需要打进去

    system

    使用上与provided相同,不同之处在于该依赖不从maven仓库中提取,而是从本地文件系统中提取,其会参照systemPath的属性进行提取依赖。

    import

    只能在dependencyManagement中使用,能解决maven单继承问题,import的依赖实际上并不参与依赖传递。

    ex: 通过import实现多继承,导入SpringBoot和SpringCloud两个父模块的jar包管理

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.2version>
    parent>
    
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.7.0version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>2021.0.2version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    依赖传递性

    A -> B -> C

    compile: A可以使用C的依赖
    test/provided: A不可以使用C的依赖

    dependency中的type

    声明引入依赖的类型jar、war、pom

    默认值是jar

    七、引用本地jar包

    pom.xml引入本地jar包

    
    <dependencies>
        <dependency>
            <groupId>com.zhengqing.commongroupId>
            <artifactId>commonartifactId>
            <version>1.0.0version>
            <scope>systemscope>
            <systemPath>${pom.basedir}/src/main/resources/lib/common-1.0.0.jarsystemPath>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    通过上面方式引入本地jar包后,打包时并不会将本地jar包一起打包,还需要如下配置

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    
                    <includeSystemScope>trueincludeSystemScope>
                configuration>
            plugin>
        plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    八、依赖排除

    exclusions

    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-seata
        
            
                io.seata
                seata-spring-boot-starter
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    九、继承-父子工程

    父工程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">
        <parent>
            <artifactId>small-tools-apiartifactId>
            <groupId>com.zhengqinggroupId>
            <version>1.0.1version>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>commonartifactId>
        <packaging>pompackaging>
    
        <description>公共模块description>
    
        
        <modules>
            <module>basemodule>
        modules>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    子工程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">
        
        <parent>
            <artifactId>commonartifactId>
            <groupId>com.zhengqinggroupId>
            <version>1.0.1version>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>baseartifactId>
    
        <name>${project.artifactId}name>
        <version>${small-tools-api.project.version}version>
        <packaging>jarpackaging>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    十、继承-配置自定义属性

    pom.xml

    
    <properties>
        <small-tools-api.project.version>0.0.1small-tools-api.project.version>
    properties>
    
    <dependencies>
        <dependency>
            <groupId>com.zhengqinggroupId>
            <artifactId>baseartifactId>
            
            <version>${small-tools-api.project.version}version>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    如果properties在父工程中定义,那么在子工程可直接引用

    <dependencies>
        <dependency>
            <groupId>com.zhengqinggroupId>
            <artifactId>baseartifactId>
            
            <version>${small-tools-api.project.version}version>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    十一、继承-父工程中统一声明管理版本

    微服务中父工程pom.xml声明某一依赖并指定版本

    dependencyManagement中配置依赖并不会实际引入,只是为了版本管理。
    实际引入需要直接在dependencies中添加。

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.seatagroupId>
                <artifactId>seata-spring-boot-starterartifactId>
                <version>1.5.2version>
            dependency>
        dependencies>
    dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    子工程pom.xml引用依赖,无需再指定版本号,即实现全局统一版本管理

    <dependencies>
        <dependency>
            <groupId>io.seatagroupId>
            <artifactId>seata-spring-boot-starterartifactId>
            
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    当统一管理版本时,依赖过多时,可以import引入别人封装好的依赖管理

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.7.0version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>2021.0.2version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    十二、继承-父工程中统一管理插件-打包插件

    这里用maven打包插件举例说明

    父工程pom.xml

    <build>
        
        <pluginManagement>
            <plugins>
                
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <version>${spring-boot.version}version>
                    <configuration>
                        <finalName>${project.build.finalName}finalName>
                        
                        <includeSystemScope>trueincludeSystemScope>
                    configuration>
                    <executions>
                        <execution>
                            <goals>
                                
                                <goal>repackagegoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        pluginManagement>
    
        
        
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    在这里插入图片描述

    子工程若是不想继承父工程的插件可如下配置

    pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    
                    <skip>trueskip>
                    <finalName>${project.name}finalName>
                configuration>
            plugin>
        plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    十三、项目单独配置远程仓库

    
    <repositories>
        <repository>
            <id>aliyun-reposid>
            <name>aliyun-reposname>
            <url>https://maven.aliyun.com/nexus/content/groups/public/url>
            
            <releases>
                <enabled>trueenabled>
            releases>
            
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>
    
    
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-pluginid>
            <name>aliyun-pluginname>
            <url>https://maven.aliyun.com/nexus/content/groups/public/url>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        pluginRepository>
    pluginRepositories>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    十四、编译xml文件

    maven默认不编译xml文件

    此配置可解决mybatis中mapper与xml映射关系不对应问题

    <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
            resource>
        resources>
        <testResources>
            <testResource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            testResource>
        testResources>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    十五、springboot打包插件

    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>${spring-boot.version}version>
                <configuration>
                    <finalName>${project.build.finalName}finalName>
                    
                    <includeSystemScope>trueincludeSystemScope>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    十六、编译插件

    指定jdk1.8版本

    Maven全局配置JDK版本方式

    修改${MAVEN_HOME}\conf\settings.xml

    <profiles>
        
        <profile>
            <id>jdk1.8id>
            <activation>
                <activeByDefault>trueactiveByDefault>
                <jdk>1.8jdk>
            activation>
            <properties>
                <maven.compiler.source>1.8maven.compiler.source>
                <maven.compiler.target>1.8maven.compiler.target>
                <maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
            properties>
        profile>
    profiles>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    单独项目配置

    如果没有上面的全局配置,我们可以通过maven的编译插件来实现

    pom.xml

    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.8.1version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                    <encoding>UTF-8encoding>
                    <compilerArgs>
                        <arg>-parametersarg>
                    compilerArgs>
                configuration>
            plugin>
        plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    十七、可选依赖

    即此依赖可有可无,不影响写代码

    
        org.springframework.boot
        spring-boot-devtools
        runtime
        
        true
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    十八、版本仲裁

    路径最短优先

    依赖D将使用版本1.2

    1. A -> B -> C -> D(版本1.1)
    2. A -> E -> D(版本1.2)

    路径相同时先声明者优先

    依赖D将使用版本1.1

    1. A -> B -> C -> D(版本1.1)
    2. A -> B -> C -> D(版本1.2)

    十九、开发自定义插件

    1、自定义插件

    新建项目maven-plugin-test - 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <artifactId>testartifactId>
            <groupId>com.zhengqinggroupId>
            <version>1.0.1version>
        parent>
    
        <artifactId>maven-plugin-testartifactId>
    
        
        <packaging>maven-pluginpackaging>
    
        <dependencies>
            
            
            <dependency>
                <groupId>org.apache.mavengroupId>
                <artifactId>maven-plugin-apiartifactId>
                <version>3.8.6version>
            dependency>
    
            
            
            <dependency>
                <groupId>org.apache.maven.plugin-toolsgroupId>
                <artifactId>maven-plugin-annotationsartifactId>
                <version>3.6.4version>
                <scope>providedscope>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
    
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-plugin-pluginartifactId>
                    <version>3.5.2version>
                plugin>
            plugins>
        build>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    定义插件执行逻辑

    
    @Slf4j
    @Mojo(
            // 标识
            name = "myPlugin"
    )
    public class MyPlugin extends AbstractMojo {
    
        /**
         * 接收使用插件时传递的参数
         */
        @Parameter
        private String msg;
    
        @Parameter
        private List<String> options;
    
        @Parameter(property = "args")
        private String args;
    
        @Override
        public void execute() throws MojoExecutionException, MojoFailureException {
            log.info("****** msg:[{}]", this.msg);
            log.info("****** options:[{}]", this.options);
            log.info("****** args:[{}]", this.args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    运行mvn clean install将插件安装到本地仓库

    2、使用插件

    在要使用的项目pom.xml中引入

    
    <build>
        <plugins>
            <plugin>
                <groupId>com.zhengqinggroupId>
                <artifactId>maven-plugin-testartifactId>
                <version>${small-tools-api.project.version}version>
                <configuration>
                    
                    <msg>Hello Worldmsg>
                    <options>
                        <option>oneoption>
                        <option>twooption>
                    options>
                    <args>hiargs>
                configuration>
                <executions>
                    <execution>
                        
                        <phase>cleanphase>
                        <goals>
                            
                            <goal>myPlugingoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    测试
    在这里插入图片描述

    二十、上传本地jar包到maven中央仓库

    https://zhengqing.blog.csdn.net/article/details/94381467

    二十一、上传本地Jar包到阿里云的云效私有仓库

    https://zhengqing.blog.csdn.net/article/details/94566443

    二十二、jar包冲突

    jar包冲突原因

    可见 十八、版本仲裁

    1. A -> B(版本1.1)
    2. A -> B(版本1.2)

    由于路径相同时先声明者优先原则:依赖B将使用版本1.1
    由于2个版本不同,版本1.1中缺少了版本1.2的xx类;
    导致A使用B时由于找不到类出现问题!

    jar包冲突解决

    IDEA安装插件Maven Helper

    Exclude冲突的jar包即可

    在这里插入图片描述


    今日分享语句:
    生活简单就迷人,人心简单就幸福;学会简单,其实就是不简单。

  • 相关阅读:
    【SUMO】初级 Public Transport - 1
    Redis——》内存最大限制
    信息共享的记忆被囊群算法-附代码
    无人机动力系统详解
    2022- CSS 函数代码了
    机器学习实战应用案例100篇(十九)-鲸鱼算法从原理到实战应用
    Java对象与封装详解
    动漫新闻查询易语言代码
    我要写整个中文互联网界最牛逼的JVM系列教程 | 「JVM与Java体系架构」章节:JVM的发展历程
    mac磁盘工具显示未装载 磁盘读写速度慢的原因
  • 原文地址:https://blog.csdn.net/qq_38225558/article/details/126289078