0
点赞
收藏
分享

微信扫一扫

Spring Boot 项目打包问题集锦: jar依赖多出boot-inf 文件夹问题/多环境动态打包/缺少BOOT-INF目录问题等...

Spring Boot 项目打包 boot-inf 文件夹的问题

spring-boot maven打包,一般pom.xml文件里会加

<plugin>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

这样打的jar里会多一个目录BOOT-INF。

引起问题: 程序包不存在。

解决办法: 如果A子模块包依赖了B子模块包,在B子模块的pom文件,加入 configuration.skip = true

<plugin>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-maven-plugin</artifactId>

    <configuration>

        <skip>true</skip>

    </configuration>

</plugin>

Spring Boot Maven插件打包后,包内没有BOOT-INF目录

使用maven插件打包后,发现包很小100来kb,显然是不对,包内缺少BOOT-INF目录,BOOT-INF是用于存放引用的外部lib的,所以缺少,打出来的包根本不能运行:

解决办法:

在自己项目的pom中,或者父pom中,在plugin中添加executions节点代码,重新打包即可解决。

<plugin>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-maven-plugin</artifactId>

    <executions>

        <execution>

            <goals>

                <goal>repackage</goal>

            </goals>

        </execution>

    </executions>

</plugin>

SpringBoot中如何引入本地jar包,并通过maven把项目成功打包成jar包部署

<build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <configuration>

                    <includeSystemScope>true</includeSystemScope>

                </configuration>

            </plugin>

        </plugins>

</build>

在sprinboot项目中pom.xml文件加<includeSystemScope>true</includeSystemScope>,代表maven打包时会将外部引入的jar包(比如在根目录下或resource文件下新加外部jar包)打包到项目jar,在服务器上项目才能运行,不加此配置,本地可以运行,因为本地可以再lib下找到外部包,但是服务器上jar中是没有的。

Spring Boot项目动态打包

build节点新增,如下代码:

        <resources>

            <resource>

                <directory>src/main/resources/</directory>

                <excludes>

                    <exclude>application.yml</exclude>

                </excludes>

                <filtering>false</filtering>

            </resource>

            <resource>

                <directory>src/main/resources/</directory>

                <includes>

                    <include>application.yml</include>

                </includes>

                <!-- 用参数化值替换文件文本内容(如@profileActive@)  -->

                <filtering>true</filtering>

            </resource>

        </resources>

然后pom.xml文件继续添加以下配置

<profiles>

        <profile>

            <!-- 本地开发环境打包: mvn clean package -P dev -->

            <id>dev</id>

            <properties>

                <profileActive>druid,dev</profileActive>

            </properties>

            <activation>

                <activeByDefault>true</activeByDefault>

            </activation>

        </profile>

        <profile>

            <!-- 测试环境打包:  mvn clean package -P test -->

            <id>test</id>

            <properties>

                <profileActive>druid,test</profileActive>

            </properties>

        </profile>

        <profile>

            <!-- 生产环境打包:  mvn clean package -P prod -->

            <id>prod</id>

            <properties>

                <profileActive>druid,prod</profileActive>

            </properties>

        </profile>

        <profile>

            <!-- 预发布环境打包:  mvn clean package -P pre -->

            <id>pre</id>

            <properties>

                <profileActive>druid,prod,pre</profileActive>

            </properties>

        </profile>

    </profiles>

application.yml文件中配置

spring:

    profiles:

        active:  @profileActive@

@profileActive@打包时,会被动态替换成profileActive节点的值。


举报

相关推荐

0 条评论