Maven (8) combat summary

Document update address: https://gitee.com/zhengqingya/java-developer-document
Maven installation and configuration see https://zhengqing.blog.csdn.net/article/details/83956373

1. Getting to know Maven for the first time

https://maven.apache.org

Project build management tool

  1. Manage jar package dependencies
  2. Compile, package, and publish java projects

2. IDEA creates a new springboot project

insert image description here
insert image description here
insert image description here

3. Directory structure description

convention over configuration

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

insert image description here
You can delete .mvn& mvnw& mvnw.cmduse your own installed maven version, which is our common project structure

Four, common commands

basic command

# 查看版本
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

Project combat order

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

5. POM

1. Projectpom.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. superpom.xml

${MAVEN_HOME}\lib\maven-model-builder-3.8.6.jar\org\apache\maven\model\pom-4.0.0.xml
Define the directory structure of 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 -->

Six, scope depends on the 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

The default value is applicable to all phases, and the dependency will participate in the compilation, testing, and running phases of the project, which is a strong dependency. When packaging, it will be packed into the package and released with the project.

runtime

Only used at runtime.
Generally, this kind of class library is a class library that separates the interface from the implementation. Ex: JDBC class library only depends on the relevant interface at the time of compilation, and only needs specific data drivers such as mysql and oracle at the time of specific operation. program. Such drivers are runtime class libraries.

test

Only used during testing, for compiling and running test code. Will not be published with the project.

provided

This dependency can participate in cycles such as compilation, testing, and running, which is equivalent to compile.
Difference: When packing, there is no need to type in

system

The use is the same as provided, the difference is that the dependency is not extracted from the maven warehouse, but from the local file system, and it will refer to the systemPathproperties to extract dependencies.

import

It can only dependencyManagementbe used in , and can solve the maven single inheritance problem. Import dependencies do not actually participate in dependency transfer.

ex: By <scope>import</scope>implementing multiple inheritance, import the jar package management of the two parent modules of SpringBoot and SpringCloud

<!-- 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>

insert image description here

Dependency transitivity

A -> B -> C

compile: A can use C dependencies
test/ provided: A cannot use C dependencies

dependencyneutraltype

Declare the types that introduce dependencies jar、war、pom, etc.

The default value isjar

7. Reference the local jar package

pom.xmlImport local jar package

<!-- 本地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>

After importing the local jar package through the above method, the local jar package will not be packaged together when packaging, and the following configuration is required

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

8. Dependency exclusion

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>

9. Inheritance - father and son project

parent projectpom.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>

Subprojectpom.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>

10. Inheritance-configure custom properties

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>

If propertiesit is defined in the parent project, it can be directly referenced in the child project

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

11. Inheritance - unified statement management version in the parent project

The parent project in the microservice pom.xmldeclares a certain dependency and specifies the version

dependencyManagementThe configuration dependencies in will not be actually introduced, but only for version management.
The actual import needs to be added directly in dependencies.

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

Sub-projects pom.xmlreference dependencies, no need to specify the version number, that is, to achieve global unified version management

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

When the version is managed in a unified manner, when there are too many dependencies, you can <scope>import</scope>introduce dependency management packaged by others

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

12. Inheritance - unified management plug-in in the parent project - packaging plug-in

Here is an example of maven packaging plug-in

parent projectpom.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>

insert image description here

If the child project does not want to inherit the plug-ins of the parent project, it can be configured as follows

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>

Thirteen, the project configures the remote warehouse separately

<!-- 配置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>

14. Compile the xml file

maven does not compile xml files by default

This configuration can solve the problem that the mapping relationship between mapper and xml in mybatis does not correspond

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

Fifteen, springboot packaging plug-in

<!-- 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>

Sixteen, compile the plug-in

Specify jdk1.8 version

Maven globally configures the JDK version method

Revise${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>

Individual project configuration

If there is no global configuration above, we can achieve it through maven's compilation plug-in

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>

Seventeen, optional dependencies

That is, this dependency is optional and does not affect writing code

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

18. Version Arbitration

shortest path first

Dependency D will use version 1.2

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

When the paths are the same, the one declared first takes precedence

Dependency D will use version 1.1

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

19. Develop custom plug-ins

1. Custom plug-ins

New project 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>

Define plugin execution logic


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

}

Run mvn clean installto install the plugin to the local repository

2. Use plug-ins

pom.xmlIntroduce in the project to use

<!-- 使用自定义插件: 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>

test
insert image description here

20. Upload the local jar package to the maven central warehouse

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

21. Upload the local Jar package to Alibaba Cloud's cloud effect private warehouse

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

Twenty-two, jar package conflict

Causes of jar package conflicts

visible十八、版本仲裁

  1. A -> B (version 1.1)
  2. A -> B (version 1.2)

Due to the principle of first declarator priority when the paths are the same: relying on B will use 版本1.1;
due to the difference between the two versions, the xx class 版本1.1is missing in ; causing problems when A uses B because the class cannot be found!版本1.2

jar package conflict resolution

IDEA installation pluginMaven Helper

Exclude conflicting jar packages

insert image description here


Today's shared sentence:
Simple life is charming, simple heart is happy; learning to be simple is actually not simple.

Guess you like

Origin blog.csdn.net/qq_38225558/article/details/126289078