Spring Boot解决Fatjar问题、依赖jar及配置文件外置

Spring Boot默认采用spring-boot-maven-plugin进行打包,打包之后是一个独立的,可运行的jar文件。单个文件比较大,配置文件,视图文件都打包在jar文件之类,某些小修改个人感觉不太方便

1、对视图文件的修改

2、配置文件修改

3、每次发布都需要更新一个大的包,没法增量更新。

以上情况也是针对某些项目,对于采用自动化部署的项目,一个fatjar可能更加方便。总的来说,也算是萝卜青菜,各有所爱。

要解决Spring Boot Fatjar的问题,需要从以下方面着手

1、将依赖的jar文件外置

2、配置文件外置,方便修改

3、页面视图文件外置,方便修改。

4、采用assembly插件代替默认的spring-boot-maven-plugin打包插件。

依赖jar外置

Spring Boot打包的jar文件是经过处理的,跟传统的可运行的jar文件不一样。

1、jar文件的结构不一样,包含一个BOOT-INF目录,这个下面有两个目录,一个是classes,即所有编译后的类文件;一个是lib,里面包含所有的依赖jar。

2、jar文件的MANIFEST.MF

Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.pointcuts.springboot.SpringBootQuickstartApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/

Main-class指定了一个JarLauncher类,Spring Boot 会通过这个Launcher来加载依赖的jar,启动应用。启动时默认从BOOT-INF/classes加载class,从BOOT-INF/lib加载所依赖的Jar包。如果想要加入外部的依赖Jar,可以通过设置环境变量LOADER_PATH来实现。 运行jar命令时,通过配置一个命令行参数:-Dloader.path=lib,spring boot会从lib目录加载需要的jar依赖。这样,我们就可以把依赖的jar文件全部放在一个外置的目录中。打包的过程中需要排除掉依赖的jar。这种方式可以给jar瘦身,但是我个人喜欢的方法还是后面提到的,用assembly插件来代替默认的打包插件,通过jar文件的描述文件来指定Class-Path依赖。

配置文件外置

通过命令行参数,可以控制需要加载的配置文件。Spring Boot配置文件有一定的优先级顺序,命令行传入的配置文件,在spring boot启动加载过程中,会覆盖掉其他路径下的配置文件中存在的属性,优先级最高。 启动参数:--spring.config.location=application.properties

静态资源、页面视图文件外置

默认的路径 静态资源:

classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/

可通过 file:/app/static 配置静态资源地址指向一个系统路径。 页面视图文件: Spring Boot默认采用freemarker渲染页面视图。默认的视图模板文件路径:

classpath*:/templates/

可通过配置以下属性制定模板文件目录:

spring.freemarker.template-loader-path=file:/app/templates

采用assembly插件打包 修改pom文件,设置编译,打包插件

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
    <archive>
    <manifest>
        <mainClass>com.cutpoints.DemoApplication</mainClass
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
    </manifest>
    <manifestEntries>
        <Class-Path>./</Class-Path>
    </manifestEntries>
    </archive>
    <excludes>
    <exclude>src/main/resources/**</exclude>
    <exclude>*.xml</exclude>
    <exclude>*.properties</exclude>
    </excludes>
    <outputDirectory>
        ${project.build.directory}/${project.build.finalName}/
    </outputDirectory>
    </configuration>
    </plugin>

    <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
    <appendAssemblyId>false</appendAssemblyId>
    <descriptors>
    <descriptor>src/assembly/assembly.xml</descriptor>
    </descriptors>
    </configuration>
    <executions>
    <execution>
    <id>make-assembly</id>
    <phase>package</phase>
    <goals>
        <goal>directory</goal>
    </goals>
    </execution>
    </executions>
    </plugin>

使用以上插件编译打包,jar文件的结构跟spring boot打包出来是有区别的,manifest文件中已经指定了依赖的包。启动类即为项目的启动类,jar包中也没有了spring boot loader等相关类,因为不需要从jar中加载一个依赖jar了。

详细打包配置请参考项目代码 github地址 spring-boot-quickstart https://github.com/ronailzz/spring-boot-study.git

results matching ""

    No results matching ""