Maven插件 maven-shade-plugin

maven-shade-plugin 插件可以将项目打包成一个可直接运行的jar(包含依赖和配置文件),运行时可以直接通过 java -jar xxxx.jar 命令启动项目。注意spring-boot项目建议使用spring-boot-maven-plugin插件。

通过<artifactSet><excludes>配置添加排除的依赖内容

通过<filters><filter>配置排除或导入包内的指定文件

通过<minimizeJar>配置可排除掉项目中没有依赖的文件内容达到压缩jar包体积的目的

Maven 配置

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
    <!-- 此处按需删减 -->
    <!--排除依赖-->
    <artifactSet>
        <excludes>
            <exclude>com.owothink:xxxxx</exclude>
            <exclude>junit:junit</exclude>
        </excludes>
    </artifactSet>

    <!--导入或排除指定内容-->
    <filters>
        <filter>
            <artifact>junit:junit</artifact>
            <includes>
                <include>junit/framework/**</include>
                <include>org/junit/**</include>
            </includes>
            <excludes>
                <exclude>org/junit/experimental/**</exclude>
                <exclude>org/junit/runners/**</exclude>
            </excludes>
        </filter>
        <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
            </excludes>
        </filter>
    </filters>
    <!--jar包体积优化-->
    <minimizeJar>true</minimizeJar>
</configuration>
<executions>
    <execution>
        <!-- 和 package 阶段绑定 -->
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
        </goals>
        <configuration>
            <!--指定运行的class-->
            <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                    <manifestEntries>
                        <Main-Class>com.owothink.natferry.NatFerryApplication</Main-Class>
                        <!--自定义配置-->
                        <Build-Number>diy-test</Build-Number>
                    </manifestEntries>
                    <!--<mainClass></mainClass>-->
                </transformer>
            </transformers>
        </configuration>
    </execution>
</executions>
</plugin>