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            -- 编译文件目录

在这里插入图片描述
可删除.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

项目实战命令

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

五、POM

1、项目pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <!-- 父工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- 坐标信息 -->
    <!-- 某公司或组织 通常与域名反向一一对应 -->
    <groupId>com.zhengqing</groupId>
    <!-- 项目模块名 -->
    <artifactId>maven-demo</artifactId>
    <!-- 版本 -->
    <version>0.0.1-SNAPSHOT</version>

    <!-- 打包方式:jar、war、pom(标识当前工程管理其它工程,ex:微服务父子工程) -->
    <packaging>jar</packaging>

    <!-- 当前项目名 -->
    <name>maven-demo</name>
    <!-- 当前项目描述 -->
    <description>maven-demo</description>

    <!-- 定义属性值 -->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!-- jar依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!-- jar依赖作用域 -->
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、超级pom.xml

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

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!-- START SNIPPET: superpom -->
<project>
  <modelVersion>4.0.0</modelVersion>

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
<!-- END SNIPPET: superpom -->

六、scope依赖作用域

pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>com.zhengqing.common</groupId>
        <artifactId>common</artifactId>
        <version>1.0.0</version>
        <scope>system</scope>
        <systemPath>${pom.basedir}/src/main/resources/lib/common-1.0.0.jar</systemPath>
    </dependency>
    
    <dependency>
         <groupId>com.zhengqing</groupId>
         <artifactId>base</artifactId>
         <scope>provided</scope>
     </dependency>
</dependencies>

compile

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

runtime

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

test

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

provided

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

system

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

import

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

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

<!-- maven单继承 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.2</version>
</parent>

<!-- maven多继承 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

在这里插入图片描述

依赖传递性

A -> B -> C

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

dependency中的type

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

默认值是jar

七、引用本地jar包

pom.xml引入本地jar包

<!-- 本地jar包依赖 -->
<dependencies>
    <dependency>
        <groupId>com.zhengqing.common</groupId>
        <artifactId>common</artifactId>
        <version>1.0.0</version>
        <scope>system</scope>
        <systemPath>${pom.basedir}/src/main/resources/lib/common-1.0.0.jar</systemPath>
    </dependency>
</dependencies>

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

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- 作用:项目打成jar时把本地jar包也引入进去 -->
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </plugin>
    </plugins>
</build>

八、依赖排除

exclusions

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
        </exclusion>
    </exclusions>
</dependency>

九、继承-父子工程

父工程pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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-api</artifactId>
        <groupId>com.zhengqing</groupId>
        <version>1.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>common</artifactId>
    <packaging>pom</packaging>

    <description>公共模块</description>

    <!-- 聚合子工程 -->
    <modules>
        <module>base</module>
    </modules>
</project>

子工程pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>common</artifactId>
        <groupId>com.zhengqing</groupId>
        <version>1.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>base</artifactId>

    <name>${project.artifactId}</name>
    <version>${small-tools-api.project.version}</version>
    <packaging>jar</packaging>
</project>

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

pom.xml

<!-- 定义属性值 -->
<properties>
    <small-tools-api.project.version>0.0.1</small-tools-api.project.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.zhengqing</groupId>
        <artifactId>base</artifactId>
        <!-- 引用属性值 -->
        <version>${small-tools-api.project.version}</version>
    </dependency>
</dependencies>

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

<dependencies>
    <dependency>
        <groupId>com.zhengqing</groupId>
        <artifactId>base</artifactId>
        <!-- 引用属性值 -->
        <version>${small-tools-api.project.version}</version>
    </dependency>
</dependencies>

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

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

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.5.2</version>
        </dependency>
    </dependencies>
</dependencyManagement>

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

<dependencies>
    <dependency>
        <groupId>io.seata</groupId>
        <artifactId>seata-spring-boot-starter</artifactId>
        <!--            <version>1.5.2</version>-->
    </dependency>
</dependencies>

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

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

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

父工程pom.xml

<build>
    <!-- pluginManagement:仅仅是一种声明,当前工程或其子工程中可以对 pluginManagement 下的 plugin 进行信息的选择、继承、覆盖等 -->
    <pluginManagement>
        <plugins>
            <!-- maven打包插件:将整个工程打成一个 fatjar (注:默认集成`maven-surefire-plugin`插件) -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <finalName>${project.build.finalName}</finalName>
                    <!-- 作用:项目打成jar,同时把本地jar包也引入进去 -->
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <!-- 可以把依赖的包都打包到生成的Jar包中 -->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>

    <!-- plugins:子pom文件中,省去了版本、配置细节等信息,只需要指定groupId和artifactId,其他信息均从父pom文件继承。当然,如果子pom文件想定制自己的特定内容,可以另行设置,并会覆盖从父pom文件继承到的内容。 -->
    <!-- 所有子工程默认都有此插件 -->
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

在这里插入图片描述

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

pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- 跳过父模块的此打包插件 -->
                <skip>true</skip>
                <finalName>${project.name}</finalName>
            </configuration>
        </plugin>
    </plugins>
</build>

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

<!-- 配置maven项目的远程仓库 -->
<repositories>
    <repository>
        <id>aliyun-repos</id>
        <name>aliyun-repos</name>
        <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
        <!-- 是否开启发布版构件下载 -->
        <releases>
            <enabled>true</enabled>
        </releases>
        <!-- 是否开启快照版构件下载 -->
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<!-- 配置maven插件的远程仓库 -->
<pluginRepositories>
    <pluginRepository>
        <id>aliyun-plugin</id>
        <name>aliyun-plugin</name>
        <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

十四、编译xml文件

maven默认不编译xml文件

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

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </testResource>
    </testResources>
</build>

十五、springboot打包插件

<!-- springboot打包插件 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <configuration>
                <finalName>${project.build.finalName}</finalName>
                <!-- 作用:项目打成jar,同时把本地jar包也引入进去 -->
                <includeSystemScope>true</includeSystemScope>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <!-- 可以把依赖的包都打包到生成的Jar包中 -->
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

十六、编译插件

指定jdk1.8版本

Maven全局配置JDK版本方式

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

<profiles>
    <!-- 全局配置项目JDK版本 -->
    <profile>
        <id>jdk1.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的编译插件来实现

pom.xml

<!-- 编译插件 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
                <compilerArgs>
                    <arg>-parameters</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

十七、可选依赖

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <!-- 可选依赖 -->
    <optional>true</optional>
</dependency>

十八、版本仲裁

路径最短优先

依赖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

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <parent>
        <artifactId>test</artifactId>
        <groupId>com.zhengqing</groupId>
        <version>1.0.1</version>
    </parent>

    <artifactId>maven-plugin-test</artifactId>

    <!-- 指定打包方式:maven-plugin -->
    <packaging>maven-plugin</packaging>

    <dependencies>
        <!-- 文档方式 -->
        <!-- https://mvnrepository.com/artifact/org.apache.maven/maven-plugin-api -->
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.8.6</version>
        </dependency>

        <!-- 注解方式 -->
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugin-tools/maven-plugin-annotations -->
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.6.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.5.2</version>
            </plugin>
        </plugins>
    </build>

</project>

定义插件执行逻辑


@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);
    }

}

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

2、使用插件

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

<!-- 使用自定义插件: maven-plugin-test -->
<build>
    <plugins>
        <plugin>
            <groupId>com.zhengqing</groupId>
            <artifactId>maven-plugin-test</artifactId>
            <version>${small-tools-api.project.version}</version>
            <configuration>
                <!-- 向插件传参 -->
                <msg>Hello World</msg>
                <options>
                    <option>one</option>
                    <option>two</option>
                </options>
                <args>hi</args>
            </configuration>
            <executions>
                <execution>
                    <!-- 触发插件的生命周期 -->
                    <phase>clean</phase>
                    <goals>
                        <!-- 插件标识 -->
                        <goal>myPlugin</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

测试
在这里插入图片描述

二十、上传本地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包即可

在这里插入图片描述


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

猜你喜欢

转载自blog.csdn.net/qq_38225558/article/details/126289078