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

  • 相关阅读:
    02 判断和循环
    【Elasticsearch教程7】Mapping字段类型之boolean
    说说HTTP 和 HTTPS 有什么区别?
    EMQX配置ssl/tls双向认证+EMQX http客户端设备认证(Java实现)_真实业务实践
    20221121 秩相同,值域不一定相同
    互联网行业汇总
    go高并发之路——go语言如何解决并发问题
    800*B. Long Long(贪心)
    图像扩增方法研究
    SpringSecurity系列 - 12 自定义过滤器实现登录页面添加验证码的认证
  • 原文地址:https://blog.csdn.net/enthan809882/article/details/139838460