Maven Plugins 插件使用

  • 一 Maven Pom 配置
  • 1.Pom参数
  • 2.常用配置
  • 3.扩展
  • 1.产品标识
  • 2.父依赖
  • 二 Maven Jar 编译配置
  • 1.maven-jar-plugin
  • 1.工程
  • 2.配置
  • 3.编译
  • 4.运行
  • 2.maven-assembly-plugin
  • 1.配置
  • 2.编译
  • 3.执行
  • 4.包结构
  • 三 Maven SpringBoot 编译配置
  • 1.修改项目结构
  • 2.配置
  • 3.编译和运行
  • 4.清单
  • 5.依赖外置
  • 6.编译和运行
  • 7.清单
  • 四 包配置和本地仓库存储位置关系


一 Maven Pom 配置

1.Pom参数

Maven Pom 参数配置官网说明

maven spring maven spring boot maven plugin_maven spring

2.常用配置

参数

含义

modelVersion

Pom模板 版本信息(保证Pom可正常解析)

groupId

唯一标识

artifactId

唯一标识

version

版本

packaging

包类型

description

描述信息

dependencyManagement

依赖管理

name

项目名称

repository

远程仓库

pluginRepository

插件仓库

build

构建配置

outputDirectory

class 输出位置

resource

资源

plugin

插件



更多详细的配置使用可直接参考官网的参数说明

3.扩展

1.产品标识

## 产品唯一标识
<groupId>org.sun.moon</groupId>
<artifactId>moon-rabbit</artifactId>
<version>1.0.0</version>

groupId

artifactId

version

域名.公司名.项目名

项目名-模块名

版本

一般来说,groupId 格式:

maven spring maven spring boot maven plugin_pom_02

2.父依赖

## 指定父依赖
<relativePath/>

maven spring maven spring boot maven plugin_pom_03

二 Maven Jar 编译配置

1.maven-jar-plugin

1.工程

maven spring maven spring boot maven plugin_springboot_04

2.配置

依赖和包分离

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

    <!--Pom 模板版本-->
    <modelVersion>4.0.0</modelVersion>

    <!--产品标识和版本-->
    <groupId>com.demo</groupId>
    <artifactId>moon</artifactId>
    <version>V1.0.0</version>

    <!--指定:编译和打包,版本即系统Jdk版本;源码编码格式-->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <other.jar.path>lib</other.jar.path>
    </properties>

    <!--外部依赖-->
    <dependencies>

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

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>
    </dependencies>

    <build>
        <!--maven 官网:https://maven.apache.org/shared/maven-archiver/index.html-->
        <!--是否包含 pom.xml 和 pom.properties-->
        <finalName>my-demo</finalName>
        <outputDirectory>D:\out</outputDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <!-- Jar 包输出位置 -->
                    <outputDirectory>D:\out</outputDirectory>
                    <archive>
                        <!--是否包含 pom.xml 和 pom.properties-->
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <!-- MANIFEST MF 清单文件信息配置-->
                        <manifest>
                            <!--主类名(全类名)、是否包含外部依赖、依赖的路径前缀-->
                            <mainClass>com.demo.moon.Rabbit</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>${other.jar.path}/</classpathPrefix>
                        </manifest>

                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <!--执行ID、阶段、目标、复制到的位置-->
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>D:\out\${other.jar.path}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

maven spring maven spring boot maven plugin_maven_05

3.编译

maven spring maven spring boot maven plugin_pom_06

可以将Jar包扩展名改为ZIP并解压缩,查看下 MF 文件信息

maven spring maven spring boot maven plugin_maven_07

4.运行

运行 Jar 包 (未使用到依赖包的功能)

maven spring maven spring boot maven plugin_build_08

运行 class 文件,注意点

错误: 找不到或无法加载主类 com.demo.moon.Rabbit

Java 命令执行 Class 文件 遵循以下原则
1.根据 Classpath 指定的位置来查找; Class 要包含当前目录,检查环境变量是否包含中括号内的信息 [.;]
2.以当前用户路径为基础,根据包路经去搜索 Class 文件,即在当前执行 Java 命令的目录,依次新建包路经并将Class文件放于最里层,才可以执行

maven spring maven spring boot maven plugin_build_09

根据上面原则,如果 Java 会从上层目录检索 Class 文件,则可以在 com 目录内执行
修改环境变量

maven spring maven spring boot maven plugin_maven spring_10

执行

maven spring maven spring boot maven plugin_maven_11

2.maven-assembly-plugin

1.配置

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

    <!--Pom 模板版本-->
    <modelVersion>4.0.0</modelVersion>

    <!--产品标识和版本-->
    <groupId>com.demo</groupId>
    <artifactId>moon</artifactId>
    <version>V1.0.0</version>

    <!--指定:编译和打包,版本即系统Jdk版本;源码编码格式-->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <other.jar.path>lib</other.jar.path>
    </properties>

    <!--外部依赖-->
    <dependencies>

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

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>
    </dependencies>

    <!--配置阿里云插件仓库-->
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


    <build>
        <!--maven 官网:https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html-->
        <!--是否包含 pom.xml 和 pom.properties-->
        <finalName>my-demo</finalName>
        <outputDirectory>D:\out</outputDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.4.2</version>
                <configuration>
                    <outputDirectory>D:\out</outputDirectory>
                    <archive>
                        <manifest>
                            <mainClass>com.demo.moon.Rabbit</mainClass>
                        </manifest>
                    </archive>
                    <!--assembly 默认的一种压缩方式-->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!--去除 jar-with-dependencies 后缀-->
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <!--合并继承、阶段、目标-->
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2.编译

maven spring maven spring boot maven plugin_maven spring_12

3.执行

maven spring maven spring boot maven plugin_maven_13

4.包结构

maven spring maven spring boot maven plugin_springboot_14


清单文件

maven spring maven spring boot maven plugin_build_15

三 Maven SpringBoot 编译配置

1.修改项目结构

项目结构

maven spring maven spring boot maven plugin_build_16

启动类

package com.demo.moon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author
 * @date 2022-09-29 15:38
 * @since 1.8
 */
@SpringBootApplication
public class MoonApplication {

    public static void main(String[] args) {
        SpringApplication.run(MoonApplication.class,args);
    }
}

初始化

package com.demo.moon.config;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.time.LocalDateTime;

/**
 * @author
 * @date 2022-09-30 0:49
 * @since 1.8
 */
@Component
public class Init implements ApplicationRunner {

    /**
     * 启动执行
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("Current Date Time is : " + LocalDateTime.now());
    }
}

2.配置

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

    <!--Pom 模板版本-->
    <modelVersion>4.0.0</modelVersion>

    <!--产品标识和版本-->
    <groupId>com.demo</groupId>
    <artifactId>moon</artifactId>
    <version>V1.0.0</version>

    <!--指定:编译和打包,版本即系统Jdk版本;源码编码格式-->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <other.jar.path>lib</other.jar.path>
    </properties>

    <!--外部依赖-->
    <dependencies>

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

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>
    </dependencies>

    <!--配置阿里云插件仓库-->
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


    <build>
        <finalName>my-moon</finalName>
        <outputDirectory>D:\out</outputDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.3</version>
                <configuration>
                    <outputDirectory>D:\out</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--smart doc 接口文档 插件-->
            <plugin>
                <groupId>com.github.shalousun</groupId>
                <artifactId>smart-doc-maven-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <configFile>./src/main/resources/doc/smart-doc.json</configFile>
                    <projectName>moon-api</projectName>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.编译和运行

maven spring maven spring boot maven plugin_springboot_17

4.清单

目录结构

maven spring maven spring boot maven plugin_springboot_18


清单信息

maven spring maven spring boot maven plugin_pom_19

5.依赖外置

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

    <!--Pom 模板版本-->
    <modelVersion>4.0.0</modelVersion>

    <!--产品标识和版本-->
    <groupId>com.demo</groupId>
    <artifactId>moon</artifactId>
    <version>V1.0.0</version>

    <!--指定:编译和打包,版本即系统Jdk版本;源码编码格式-->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <other.jar.path>lib</other.jar.path>
    </properties>

    <!--外部依赖-->
    <dependencies>

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

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>
    </dependencies>

    <!--配置阿里云插件仓库-->
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


    <build>
        <finalName>my-moon</finalName>
        <outputDirectory>D:\out</outputDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.3</version>
                <configuration>
                    <outputDirectory>D:\out</outputDirectory>
                    <fork>true</fork>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>nothing</groupId>
                            <artifactId>nothing</artifactId>
                        </include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--拷贝依赖-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>D:\out\lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--smart doc 接口文档 插件-->
            <plugin>
                <groupId>com.github.shalousun</groupId>
                <artifactId>smart-doc-maven-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <configFile>./src/main/resources/doc/smart-doc.json</configFile>
                    <projectName>moon-api</projectName>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

6.编译和运行

java -Dloader.path=lib/ -jar

maven spring maven spring boot maven plugin_pom_20

7.清单

目录结构

maven spring maven spring boot maven plugin_maven spring_21

清单信息

maven spring maven spring boot maven plugin_maven spring_22

四 包配置和本地仓库存储位置关系

maven spring maven spring boot maven plugin_maven spring_23