项目打包优化将所有第三方包单独打包至lib目录

Eave 2024.02.04 11:16

1、在pom.xml中配置以下代码,随后使用mvn clean package打包

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- 主类地址 -->
                <mainClass>com.wizardaws.tools.application.App</mainClass>
                <executable>true</executable>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <!-- nothing 代表不存在的依赖包,意思就是什么依赖包都不引入 -->
                        <groupId>nothing</groupId>
                        <artifactId>nothing</artifactId>
                    </include>
                </includes>
            </configuration>
        </plugin>
        <!-- 将第三方包拷贝到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>
    </plugins>
</build>

2、启动打包的jar文件

java -Dloader.path=./lib -jar tools.jar