• maven常用三种打包插件


    1. maven-shade-plugin(常用)

    可以将依赖打进jar包里面,只有一个插件目标shade:shade,通常绑定在package生命周期阶段。

    1.1 包含或过滤jar包

    <artifactSet>
        <excludes>
            <exclude>junit:junitexclude>
        excludes>
        <includes>
            <include>com.alibaba:fastjsoninclude>
        includes>
    artifactSet>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    1.2 过滤特定的资源文件

    <filters>
        <filter>
            <artifact>*:*artifact>
            <excludes>
                <exclude>META-INF/*.SFexclude>
                <exclude>META-INF/*.DSAexclude>
                <exclude>META-INF/*.RSAexclude>
            excludes>
        filter>
    filters>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.3 没用的类直接过滤掉,打最小包

    <minimizeJar>trueminimizeJar>
    
    • 1

    1.4 修改包名

    <shadedArtifactAttached>trueshadedArtifactAttached>
    <shadedClassifierName>myclassnameshadedClassifierName>
    
    • 1
    • 2

    1.5 构建可执行的jar包(需要指定默认main类)

    <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.king.MyMainClassmainClass>
        transformer>
    transformers>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    完整的插件格式如下:

    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-shade-pluginartifactId>
        <version>3.2.6version>
        <executions>
            <execution>
                <phase>packagephase>
                <goals>
                    <goal>shadegoal>
                goals>
                <configuration>
                    
                    
                    
                    <filters>
                        <filter>
                            <artifact>*:*artifact>
                            <excludes>
                                <exclude>META-INF/*.SFexclude>
                                <exclude>META-INF/*.DSAexclude>
                                <exclude>META-INF/*.RSAexclude>
                            excludes>
                        filter>
                    filters>
                    <shadedArtifactAttached>trueshadedArtifactAttached>
                    <shadedClassifierName>shadeshadedClassifierName>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer"/>
                    transformers>
                configuration>
            execution>
        executions>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    2. maven-compiler-plugin

    编译目标: compile:compile和compile:test-compile
    通常简写为以下:

    <properties>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
    properties>
    
    • 1
    • 2
    • 3
    • 4

    如果显示的声明如下:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-compiler-pluginartifactId>
            <version>3.8.1version>
            <configuration>
                <source>1.8source>
                <target>1.8target>
            configuration>
        plugin>
    plugins>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3. maven-resource-plugin(针对资源文件的配置)

    将资源文件打包进jar包中。

    3.1 简写配置

    默认不用显示配置,就会把src/main/resources复制到jar包中的classes下,
    srct/test/resources复制到jar包中的test-classes下。

    <resources>
        <resource>
            <directory>src/main/envdirectory>
        resource>
    resources>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.2 过滤或包含资源文件

    <includes>
        <include>2.textinclude>
    includes>
    <excludes>
        <exclude>1.textexclude>
    excludes>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.3 自定义打包的位置

    当在build设置该属性,maven-resource-plugin将不会执行默认的src/main/resources复制到jar中的classes)

    <outputDirectory>${basedir}/target/classes1outputDirectory>
    
    • 1

    3.4 过滤资源文件的属性

    例如定义公共属性,可以在其他文件引入属性的值(例如password等),需指定两个事:构建配置的filters元素中的属性文件列表,以及一个标记告诉Maven资源目录需要过滤。

    下面的例子:定义default.properties为过滤文件(可以理解为其他文件要经过该文档过滤下),src/main/resources中的文件可以使用default.properties定义的属性值
    注意设置true和有没有没有直接关系,即使没有文件,true也是可以获取pom文件或settings.xml,profile中定义的属性。

    <build>
        <filters>
            <filter>src/main/filters/default.propertiesfilter>
        filters>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
            resource>
        resources>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.5 自定义资源文件目录

    我们知道上面几个例子都是插件目标resources:resources完成,当我们需要更加自由的自定义文件来源和文件来源的时候,这就要用resources:copy-resources插件目标,很明显这个插件的功能就是复制文件。
    下面的例子:在generate-resources生命周期调用resources:copy-resources插件目标完成文件的复制工作
    注意:resources:copy-resources并不会影响resources:resources,索引默认的资源位置也同样生成。

    <plugins>
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-resources-pluginartifactId>
            <version>3.2.0version>
            <executions>
                <execution>
                    <id>copy-fileid>
                    <phase>generate-resourcesphase>
                    <goals>
                        <goal>copy-resourcesgoal>
                    goals>
                    <configuration>
                        
                        <outputDirectory>${basedir}/target/classes1outputDirectory>
                        <resources>
                            <resource>
                                
                                <directory>${basedir}/src/main/resourcesdirectory>
                                <includes>
                                    <include>1.textinclude>
                                includes>
                               
                            resource>
                        resources>
                    configuration>
                execution>
            executions>
        plugin>
    plugins>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    上面的所有语法集合:

    <build>
       
       <plugins>
           <plugin>
               <groupId>org.apache.maven.pluginsgroupId>
               <artifactId>maven-resources-pluginartifactId>
               <version>3.2.0version>
               <executions>
                   <execution>
                       <id>copy-fileid>
                       <phase>generate-resourcesphase>
                       <goals>
                           <goal>copy-resourcesgoal>
                       goals>
                       <configuration>
                           
                           <outputDirectory>${basedir}/target/classes1outputDirectory>
                           <resources>
                               <resource>
                                   
                                   <directory>${basedir}/src/main/resourcesdirectory>
                                   <includes>
                                       <include>1.textinclude>
                                   includes>
                                  
                               resource>
                           resources>
                       configuration>
                   execution>
               executions>
           plugin>
       plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    4. maven-jar-plugin

    maven-jar-plugin通常也是不需要显示声明配置的,因为本身就默认绑定到package生命周期上的。功能是把工程打成一个可以运行的jar(注意这个插件不会把依赖打入jar包中)

    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-jar-pluginartifactId>
        <version>3.1.0version>
        <configuration>
            <archive>
                <manifest>
                    
                    <mainClass>com.sf.MainmainClass>
                    
                    <addClasspath>trueaddClasspath>
                    
                    <classpathPrefix>libclasspathPrefix>
                manifest>
            archive>
        configuration>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    注意事项:
    如果我们打包成功,直接运行 java -jar xxx.jar 基本会提示没有依赖包,这是因为并没有把依赖打进来,所以需要你的运行环境提供依赖包,而且依赖包要放在lib这个目录下;
    这个包是怎么知道我们运行的主类呢?通过上面的配置,jar中有META-INF/MANIFEST.MF
    下面是我这个工程的MANIFEST.MF文件内容

    Manifest-Version: 1.0
    Built-By: zhangsan
    Class-Path: lib/junit-4.12.jar lib/hamcrest-core-1.3.jar lib/fastjson-1.2.58.jar
    Created-By: Apache Maven 3.3.3
    Build-Jdk: 1.8.0_202
    Main-Class: com.sf.Main
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    通过该文件,该jar可以知道去哪里找依赖jar和主类
    如果你需要知道这个工程需要哪些jar包,并获取可以使用maven-dependency-plugin插件把依赖的jar包放入一个单独的目录中

    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-dependency-pluginartifactId>
        <executions>
            <execution>
                <id>copy-dependenciesid>
                <phase>packagephase>
                <goals>
                    <goal>copy-dependenciesgoal>
                goals>
                <configuration>
                     <type>jartype>
                     <includeTypes>jarincludeTypes>
                     <outputDirectory>${project.build.directory}/liboutputDirectory>
                configuration>
            execution>
        executions>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    6 maven-assembly-plugin

    个性化打包的maven-assembly-plugin插件,该插件只有一个目标assembly:single,默认不会执行,要自己绑定到生命周期阶段。
    官方也介绍了如果想要打成jar,建议使用maven-shade-plugin。
    使用这个插件你要告诉这个插件怎么执行,这个配置就是assembly的描述符,maven提供了几个assembly插件预定义的,也可以自定义,同时在配置中,我们可以一次执行多个描述符。

    • 指定预定义的描述符(就是已经设置了很多参数,告诉插件什么事了)
    • 指定自定义的描述符;描述符的id会作为生成包的命名的一部分。

    官方预定义的描述符:

    • bin: 默认将bin目录下文件打入包中
    • jar-with-dependencies : 会将所有依赖都解压打包
    • src:只将源码目录下的文件打包
    • project:将整个project资源打包

    下面是官方预定义的jar-with-dependencies描述符。

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
      
      <id>jar-with-dependenciesid>
      <formats>
        <format>jarformat>
      formats>
      <includeBaseDirectory>falseincludeBaseDirectory>
      <dependencySets>
        <dependencySet>
          <outputDirectory>/outputDirectory>
          <useProjectArtifact>trueuseProjectArtifact>
          <unpack>trueunpack>
          <scope>runtimescope>
        dependencySet>
      dependencySets>
    assembly>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    自定义的描述符:distribution.xml,定义文件过滤和依赖打包

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
        <id>distributionid>
        <formats>
            <format>jarformat>
        formats>
        <files>
            <file>
                <source>src/main/env/prod.propertiessource>
                <outputDirectory>outputDirectory>
                <filtered>truefiltered>
            file>
            <file>
                <source>src/main/resources/1.txtsource>
                <outputDirectory>outputDirectory>
            file>
        files>
        <includeBaseDirectory>falseincludeBaseDirectory>
        <dependencySets>
            <dependencySet>
                <outputDirectory>/outputDirectory>
                <useProjectArtifact>trueuseProjectArtifact>
                <unpack>trueunpack>
                <scope>runtimescope>
            dependencySet>
        dependencySets>
    assembly>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    上面用到的所有语法集合:

    <plugin>
        <artifactId>maven-assembly-pluginartifactId>
        <version>3.3.0version>
        <executions>
            <execution>
                <id>make-assemblyid>
                <phase>packagephase>
                <goals>
                    <goal>singlegoal>
                goals>
            execution>
        executions>
        <configuration>
            
            <filters>
                <filter>src/main/assembly/filter.propertiesfilter>
            filters>
            <descriptors>
                <descriptor>src/main/assembly/distribution.xmldescriptor>
            descriptors>
            
            <archive>
                <manifest>
                    <mainClass>com.sf.MainmainClass>
                manifest>
            archive>
        configuration>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    更多参数参考地址:https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

  • 相关阅读:
    Java面试题——你们怎么解决消息重复消费?
    OpenCV-17制作LOGO小练习
    SpringBoot 之 普通类获取Spring容器中的bean
    天工开物 #9 Why Async Rust(译文)
    LeetCode简单题之装满杯子需要的最短总时长
    Elasticsearch启动后自动退出
    【JS】数据结构之图结构
    【Leetcode】1320. Minimum Distance to Type a Word Using Two Fingers
    阿里云服务器ECS windows server已开放端口但连不上的问题
    Linux生产者消费者与信号量
  • 原文地址:https://blog.csdn.net/wang6733284/article/details/126457234