Springboot打包 依赖包到单独文件夹

2023-07-18 18:05:33 浏览数 (2)

在使用Springboot开发项目时,打包好的jar包是可以直接运行的,但是通常情况下,这个jar都会很大,因为它默认打包了所有的依赖jar包。此时,就可以通过maven-dependency-plugin插件将依赖包复制到独立的文件夹,在启动jar包的时候引用即可。

代码语言:javascript复制
<build>
        <finalName>iteachyou-cms</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>cn.xuexiluxian.open.admin.OpenDeerApplication</mainClass>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>${project.artifactId}</artifactId>
                        </include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>open-deer</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <type>jar</type>
                            <includeTypes>jar</includeTypes>
                            <includeScope>runtime</includeScope>
                            <outputDirectory>${project.build.directory}/libs</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
配置说明
  1. cn.xuexiluxian.open.admin.OpenDeerApplication用于指定该jar包启动时的主类(建议加上)
  2. 加上自己项目的groupId和artifactId即可
  3. open-deer值自定义
  4. ${project.build.directory}/libs依赖包输出目录
  5. 其它写死
启用项目
代码语言:javascript复制
java -Dloader.path=libs包全路径 -jar open-deer.jar

代码语言:javascript复制
java -Dloader.path=libs/ -jar open-deer.jar

0 人点赞