您的位置 首页 >  博文

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

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

<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. 其它写死

启用项目

java -Dloader.path=libs包全路径 -jar open-deer.jar

java -Dloader.path=libs/ -jar open-deer.jar


关于作者: 王俊南(Jonas)

昨夜寒蛩不住鸣。惊回千里梦,已三更。起来独自绕阶行。人悄悄,帘外月胧明。 白首为功名。旧山松竹老,阻归程。欲将心事付瑶琴。知音少,弦断有谁听。

热门文章