Java—打包成可执行jar包(推荐)

1、父pom

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.4.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.6.0</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

2、子module(想要打包的module)

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
<!--                    <excludes>
                    <exclude>/**.yaml</exclude>
                    <exclude>Dockerfile</exclude>
                </excludes>-->
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.aolingo.starchat.gateway.GatewayEntry</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

3、打包命令

mvn clean package -Pdev -pl starchat-gateway

4、解决一些错误

(1)、错误:Exception in thread “main” java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

原因:

这个错误通常发生在你尝试运行一个包含签名文件的 JAR 包时。错误信息表明 JAR 包的签名验证失败,通常是因为某些依赖库(如 Bouncy Castle 或其他加密库)的签名文件(.SF、.DSA、*.RSA)与 JAR 包的 MANIFEST.MF 不匹配。

解决:在 pom.xml 中添加 maven-shade-plugin 并配置排除签名文件。

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <!-- 排除所有签名文件 -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.p12</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>