maven-jar-plugin maven打包插件笔记

文章目录

maven自定义插件内容很多,也不易理解,这里把maven打包插件单拿出来,作为入口试着理解下。

配置示例

xml 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <!--要使用的打包配置.-->
        <archive>
            <!-- 创建的归档文件是否包含以下pom.xml 和pom.properties Maven 文件,默认是true -->
            <addMavenDescriptor>true</addMavenDescriptor>
            <!-- 生成MANIFEST.MF的设置 -->
            <manifest>
                <!-- 为依赖包添加路径, 这些路径会写在MANIFEST文件的Class-Path下 -->
                <addClasspath>true</addClasspath>
                <!-- 这个jar所依赖的jar包添加classPath的时候的前缀,如果这个jar本身和依赖包在同一级目录,则不需要添加 -->
                <classpathPrefix>lib/</classpathPrefix>
                <!-- jar启动入口类 -->
                <mainClass>com.example.demo.DemoApplication</mainClass>
            </manifest>
            <manifestEntries>
                <!-- 在Class-Path下添加配置文件的路径 -->
                <!--<Class-Path>../config/</Class-Path>-->
            </manifestEntries>
        </archive>
        <!-- jar包的位置,其中${project.build.directory}默认为 target/ -->
        <outputDirectory>${project.build.directory}</outputDirectory>
        <!--过滤掉不希望包含在jar中的文件-->
        <excludes>
            <exclude>${project.basedir}/xml/*</exclude>
        </excludes>
        <!--要包含的文件列表-->
        <includes>
            <!-- 打jar包时,打包class文件和config目录下面的 properties文件 -->
            <!-- 有时候可能需要一些其他文件,这边可以配置,包括剔除的文件等等 -->
            <include>**/*.class</include>
            <include>**/*.properties</include>
        </includes>
    </configuration>
</plugin>

如上标签就是打包插件自定义的。其他的如等都是maven基础标签。

所有标签中的内容,都是用@Parameter注入的,如果必填,加上 required = true。

java 复制代码
@Parameter(defaultValue = "${project.build.outputDirectory}", required = true)
    private File classesDirectory;

其他

官网文档

apache打包插件官网地址:

https://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html

github地址:

https://github.com/apache/maven-jar-plugin.git

问题

maven打包插件是如何和打包动作关联在一起的?

创建类的时候就定义了。 defaultPhase = LifecyclePhase.PACKAGE 这一行就是。

java 复制代码
@Mojo(
        name = "jar",
        defaultPhase = LifecyclePhase.PACKAGE,
        requiresProject = true,
        threadSafe = true,
        requiresDependencyResolution = ResolutionScope.RUNTIME)
public class JarMojo extends AbstractJarMojo {}

配置文件中 goal是必须的吗?

想要解答这个问题,先要知道控制执行有几种方式。

两种:

1、 @Mojo注解中defaultPhase设置默认phase(阶段)。

2、配置文件中指定。

如:

xml 复制代码
<plugin>
   <groupId>com.example</groupId>
    <artifactId>my-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <!-- 在compile阶段执行插件的goal-1目标 -->
        <execution>
            <id>execution-1</id>
            <phase>compile</phase>
            <goals>
                <goal>goal-1</goal>
            </goals>
        </execution>
        <!-- 在install阶段执行插件的goal-2和goal-3目标 -->
        <execution>
            <id>execution-2</id>
            <phase>install</phase>
            <goals>
                <goal>goal-2</goal>
                <goal>goal-3</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这样也实现了goal和phase的绑定。

那么另外一个问题也迎刃而解,maven自定义插件可以有多个goal(mojo)吗?

当然可以,参考maven-install-plugin,有两个mojo:install和install-file。

相关推荐
Hello_Embed10 小时前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
咸甜适中10 小时前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
Grassto11 小时前
RAG 从入门到放弃?丐版 demo 实战笔记(go+python)
笔记
Magnetic_h11 小时前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
周周记笔记14 小时前
学习笔记:第一个Python程序
笔记·学习
丑小鸭是白天鹅14 小时前
Kotlin协程详细笔记之切线程和挂起函数
开发语言·笔记·kotlin
潘达斯奈基~14 小时前
《大数据之路1》笔记2:数据模型
大数据·笔记
..过云雨15 小时前
05.【Linux系统编程】进程(冯诺依曼体系结构、进程概念、进程状态(注意僵尸和孤儿)、进程优先级、进程切换和调度)
linux·笔记·学习
咸甜适中15 小时前
rust语言 (1.88) egui (0.32.2) 学习笔记(逐行注释)(二十八)使用图片控件显示图片
笔记·学习·rust·egui
一又四分之一.15 小时前
高数基础知识(下)②
笔记