文章目录


  1. maven-assembly-plugin
    Assembly 插件支持将项目的所有依赖、文件都打包到同一个输出文件中
    基本使用
<plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.xxx</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

会生成两个包 后缀为 jar-with-dependencies 是含有第三方依赖的 JAR 包 也就是胖包 后缀没有的是瘦包 没有任何依赖瘦包

  1. maven-shade-plugin
    maven-shade-plugin 比 maven-assembly-plugin 功能更为强大,比如你的工程依赖很多的 JAR 包,而被依赖的 JAR 又会依赖其他的 JAR 包,这样,当工程中依赖到不同的版本的 JAR 时,并且 JAR 中具有相同名称的资源文件时,shade 插件会尝试将所有资源文件打包在一起时,而不是和 assembly 一样执行覆盖操作。通常使用 maven-shade-plugin 就能够完成大多数的打包需求,其配置简单且适用性最广,因此建议优先使用此方式。
    基本使用
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                 <!-- 禁止生成 dependency-reduced-pom.xml-->
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                                <relocation>
                                 <!-- 解决包冲突 进行转换-->
                                    <pattern>com.google.protobuf</pattern>
                                    <shadedPattern>shaded.com.google.protobuf</shadedPattern>
                                </relocation>
                            </relocations>
                            <artifactSet>
                                <excludes>
                                    <exclude>log4j:*</exclude>
                                </excludes>
                            </artifactSet>
                            <filters>
                                <filter>
                                    <!-- Do not copy the signatures in the META-INF folder.
                                    Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                            <!-- 某些jar包含具有相同文件名的其他资源(例如属性文件)。 为避免覆盖,您可以选择通过将它们的内容附加到一个文件中来合并它们-->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>reference.conf</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>mainclass</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
  1. 打包非Maven管理Jar
    使用 maven-jar-plugin maven-dependency-plugin 插件将其打入最终的jar中
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                          <!--指定 resources/lib 目录-->
                        <classpathPrefix>lib/</classpathPrefix>
                          <!--应用的主入口类-->
                        <mainClass>mainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>compile</phase>
                    <goals>
                         <!--将 resources/lib 目录所有 Jar 包打进最终的依赖中-->
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                         <!--将 resources/lib 目录所有 Jar 包一并拷贝到输出目录的 lib 目录下-->
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • 排除Jar包
  1. 添加 provid
    此时该jar会被排除,但是不建议 因为在本地进行测试也无法使用此jar包
  2. 在plugin中使用 排除标签
  • 打包scala项目
<!-- scala 依赖 -->
       <dependency>
           <groupId>org.scala-lang</groupId>
           <artifactId>scala-library</artifactId>
           <version>${scala.version}</version>
       </dependency>
       <dependency>
           <groupId>org.scala-lang</groupId>
           <artifactId>scala-compiler</artifactId>
           <version>${scala.version}</version>
       </dependency>


<plugin>
   <groupId>net.alchim31.maven</groupId>
   <artifactId>scala-maven-plugin</artifactId>
   <version>3.2.2</version>
   <executions>
       <execution>
           <goals>
               <goal>compile</goal>
               <goal>testCompile</goal>
           </goals>
           <configuration>
               <args>
                   <!--<arg>-make:transitive</arg>--><!--scala2.11 netbean不支持这个参数-->
                   <arg>-dependencyfile</arg>
                   <arg>${project.build.directory}/.scala_dependencies</arg>
               </args>
           </configuration>
       </execution>
   </executions>
</plugin>
  • 其他插件
    maven-compiler-plugin
    maven 是个管理工具,如果我们不告诉它我们的代码要使用什么样的 jdk 版本编译的话,它就会用 maven-compiler-plugin 默认的 jdk 版本来进行处理,这样就容易出现版本不匹配,以至于可能导致编译不通过的问题。
    maven 的默认编译使用的 jdk 版本有时候不通用,使用 maven-compiler-plugin 插件可以指定项目源码的 jdk 版本,编译后的 jdk 版本,以及编码。
<!--manven打包插件-->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
  • maven 打瘦包胖包
    pom 加入以下插件会生成 包含依赖的jar和不包含依赖的jar 和 全部都是依赖jar的lib目录 将lib目录放到合适的地方 每次修改代码传瘦包就好了 又快又好
<!-- 将所依赖的第三方jar包打入target下的lib目录 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>