Contents
  1. 1. 1. 在你的pom.xml文件中加上这个插件就可以了:
  2. 2. 2. 然后在控制台运行:
  3. 3. 3. 错误列表
    1. 3.0.1. 3.1 如果没有指定方法入口,会报 hello-1.0-SNAPSHOT.jar中没有主清单属性的错误。
    2. 3.0.2. 3.2 如果没有排除那些不需要打包的配置文件,会报这样的错误:
  • 4. 4. 更多配置
  • maven 默认只会打包本项目的文件,不会打包依赖的jar,如果想把代码连同依赖的jar包整体打包成一个jar包,就需要用到maven的插件 —— maven-shade-plugin。

    1. 在你的pom.xml文件中加上这个插件就可以了:

    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
    <project>
    ...
    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
    <execution>
    <phase>package</phase>
    <goals>
    <goal>shade</goal>
    </goals>
    <configuration>
    <transformers>
    <!--默认带main方法的类信息不会添加到manifest中,需要加上此配置-->
    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    <!--指定你的程序入口-->
    <mainClass>com.hello.HelloWorld</mainClass>
    </transformer>
    </transformers>
    <!--排除不需要打包的文件-->
    <filters>
    <filter>
    <artifact>*:*</artifact>
    <excludes>
    <exclude>META-INF/*.SF</exclude>
    <exclude>META-INF/*.DSA</exclude>
    <exclude>META-INF/*.RSA</exclude>
    <exclude>junit:junit</exclude>
    <exclude>org.apache.maven:lib:tests</exclude>
    </excludes>
    </filter>
    </filters>
    </configuration>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>
    ...
    </project>

    2. 然后在控制台运行:

    mvn clean package

    就可以了。

    3. 错误列表

    3.1 如果没有指定方法入口,会报 hello-1.0-SNAPSHOT.jar中没有主清单属性的错误。

    3.2 如果没有排除那些不需要打包的配置文件,会报这样的错误:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    Error: A JNI error has occurred, please check your installation and try again
    Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
    at sun.security.util.SignatureFileVerifier.processImpl(Unknown Source)
    at sun.security.util.SignatureFileVerifier.process(Unknown Source)
    at java.util.jar.JarVerifier.processEntry(Unknown Source)
    at java.util.jar.JarVerifier.update(Unknown Source)
    at java.util.jar.JarFile.initializeVerifier(Unknown Source)
    at java.util.jar.JarFile.getInputStream(Unknown Source)
    at sun.misc.URLClassPath$JarLoader$2.getInputStream(Unknown Source)
    at sun.misc.Resource.cachedInputStream(Unknown Source)
    at sun.misc.Resource.getByteBuffer(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

    至于为什么需要排除这些文件,可以看一下 这篇文章

    4. 更多配置

    maven-shade-plugin 插件还有很多实用的配置,例如:排除具体的不需要打包的jar,优化jar包依赖冲突,自动排除不依赖的jar包,把shade构件作为辅助构件,给程序入口设置Build-Number,Transformers有哪些配置等等。更多配置请访问maven-shade-plugin官网

    Contents
    1. 1. 1. 在你的pom.xml文件中加上这个插件就可以了:
    2. 2. 2. 然后在控制台运行:
    3. 3. 3. 错误列表
      1. 3.0.1. 3.1 如果没有指定方法入口,会报 hello-1.0-SNAPSHOT.jar中没有主清单属性的错误。
      2. 3.0.2. 3.2 如果没有排除那些不需要打包的配置文件,会报这样的错误:
  • 4. 4. 更多配置