Maven打包插件之——maven-jar-plugin、maven-assembly-plugin、maven-shade-plugin

发布时间 2023-12-25 22:04:05作者: l_v_y_forever

转载自:https://blog.csdn.net/calm_encode/article/details/103931537

1. 打包插件的介绍

      打包插件是把class文件,配置文件打包成一个jar(war或者其他格式)的包。而且可执行jar包中包含或者不包含相应的依赖包包,当不包含相应的依赖包时,我们需要建立lib目录,且jar和lib目录在同级别目录。

2. 常见的打包插件

      2.1 maven-jar-plugin

            可执行jar包与依赖包是分开的,需要建立lib目录来存放所需的依赖包,且jar包与lib目录在同级别目录中,相应的pom配置如下:

  1.  
    <plugin>
  2.  
    <groupId>org.apache.maven.plugins</groupId>
  3.  
    <artifactId>maven-jar-plugin</artifactId>
  4.  
    <version>2.6</version>
  5.  
    <configuration>
  6.  
    <archive>
  7.  
    <manifest>
  8.  
    <addClasspath>true</addClasspath>
  9.  
    <classpathPrefix>lib/</classpathPrefix>
  10.  
    <mainClass>com.xxx.xxxService</mainClass>
  11.  
    </manifest>
  12.  
    </archive>
  13.  
    </configuration>
  14.  
    </plugin>
  15.  
    <plugin>
  16.  
    <groupId>org.apache.maven.plugins</groupId>
  17.  
    <artifactId>maven-dependency-plugin</artifactId>
  18.  
    <version>2.10</version>
  19.  
    <executions>
  20.  
    <execution>
  21.  
    <id>copy-dependencies</id>
  22.  
    <phase>package</phase>
  23.  
    <goals>
  24.  
    <goal>copy-dependencies</goal>
  25.  
    </goals>
  26.  
    <configuration>
  27.  
    <outputDirectory>${project.build.directory}/lib</outputDirectory>
  28.  
    </configuration>
  29.  
    </execution>
  30.  
    </executions>
  31.  
    </plugin>

      2.2 maven-assembly-plugin

            该插件会将所有的依赖包放入可执行jar包,但是该插件会缺失spring的xds文件,导致jar包无法运行,而且当同级别目录下还有其他可执行文件依赖可能会产生冲突,相应的pom配置如下:

  1.  
    <plugin>
  2.  
    <artifactId>maven-assembly-plugin</artifactId>
  3.  
    <configuration>
  4.  
    <descriptorRefs>
  5.  
    <descriptorRef>jar-with-dependencies</descriptorRef>
  6.  
    </descriptorRefs>
  7.  
    <archive>
  8.  
    <manifest>
  9.  
    <mainClass>com.xxx.xxxService</mainClass>
  10.  
    </manifest>
  11.  
    </archive>
  12.  
    </configuration>
  13.  
    <executions>
  14.  
    <execution>
  15.  
    <id>make-assembly</id>
  16.  
    <phase>package</phase>
  17.  
    <goals>
  18.  
    <goal>single</goal>
  19.  
    </goals>
  20.  
    </execution>
  21.  
    </executions>
  22.  
    </plugin>

      2.3 maven-shade-plugin

            该插件将所有的依赖包放入可执行的jar包中,当同级别目录中有其他的可执行jar包时,依赖可能会产生冲突,且运行jar包时,有时候会出现类似SF,DSA,RSA文件冲突的提示,选哟派相互META-INF目录下的文件,相应pom位置如下:

  1.  
        <plugin>
  2.  
    <groupId>org.apache.maven.plugins</groupId>
  3.  
    <artifactId>maven-shade-plugin</artifactId>
  4.  
    <version>2.4.3</version>
  5.  
    <executions>
  6.  
    <execution>
  7.  
    <phase>package</phase>
  8.  
    <goals>
  9.  
    <goal>shade</goal>
  10.  
    </goals>
  11.  
    <configuration>
  12.  
    <filters>
  13.  
    <filter>
  14.  
    <artifact>*:*</artifact>
  15.  
    <excludes>
  16.  
    <exclude>META-INF/*.SF</exclude>
  17.  
    <exclude>META-INF/*.DSA</exclude>
  18.  
    <exclude>META-INF/*.RSA</exclude>
  19.  
    </excludes>
  20.  
    </filter>
  21.  
    </filters>
  22.  
    <transformers>
  23.  
    <transformer
  24.  
    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  25.  
    <resource>META-INF/spring.handlers</resource>
  26.  
    </transformer>
  27.  
    <transformer
  28.  
    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  29.  
    <resource>META-INF/spring.schemas</resource>
  30.  
    </transformer>
  31.  
    <transformer
  32.  
    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  33.  
    <resource>META-INF/spring.tooling</resource>
  34.  
    </transformer>
  35.  
    <transformer
  36.  
    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  37.  
    <mainClass>com.xxx.xxxInvoke</mainClass>
  38.  
    </transformer>
  39.  
    </transformers>
  40.  
    <minimizeJar>true</minimizeJar>
  41.  
    <shadedArtifactAttached>true</shadedArtifactAttached>
  42.  
    </configuration>
  43.  
    </execution>
  44.  
    </executions>
  45.  
    </plugin>