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


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

配置示例

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

@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 这一行就是。

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

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

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

两种:
1、 @Mojo注解中defaultPhase设置默认phase(阶段)。
2、配置文件中指定。
如:

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

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值