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



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

    配置示例

    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-jar-pluginartifactId>
        <version>3.2.2version>
        <configuration>
            
            <archive>
                
                <addMavenDescriptor>trueaddMavenDescriptor>
                
                <manifest>
                    
                    <addClasspath>trueaddClasspath>
                    
                    <classpathPrefix>lib/classpathPrefix>
                    
                    <mainClass>com.example.demo.DemoApplicationmainClass>
                manifest>
                <manifestEntries>
                    
                    
                manifestEntries>
            archive>
            
            <outputDirectory>${project.build.directory}outputDirectory>
            
            <excludes>
                <exclude>${project.basedir}/xml/*exclude>
            excludes>
            
            <includes>
                
                
                <include>**/*.classinclude>
                <include>**/*.propertiesinclude>
            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.examplegroupId>
        <artifactId>my-pluginartifactId>
        <version>1.0.0version>
        <executions>
            
            <execution>
                <id>execution-1id>
                <phase>compilephase>
                <goals>
                    <goal>goal-1goal>
                goals>
            execution>
            
            <execution>
                <id>execution-2id>
                <phase>installphase>
                <goals>
                    <goal>goal-2goal>
                    <goal>goal-3goal>
                goals>
            execution>
        executions>
    plugin>
    

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

    那么另外一个问题也迎刃而解,maven自定义插件可以有多个goal(mojo)吗?
    当然可以,参考maven-install-plugin,有两个mojo:install和install-file。

  • 相关阅读:
    A Yet Another Remainder The 2022 ICPC Asia Regionals Online Contest (II)
    [附源码]java毕业设计拾穗在线培训考试系统
    视频号双11激励政策,快来看一看
    Java框架 SpringMVC处理ajax请求
    JWT基本概念和使用介绍
    超几何分布 MIA MAP Multimodal-intersection-analysis-MIA-
    指定cv::cuda::GpuMat创建所在的GPU卡
    Python实现基于DMN开发的问答系统
    LabVIEW专栏八、类
    JUC并发编程--------AQS以及各类锁
  • 原文地址:https://blog.csdn.net/enthan809882/article/details/139838460