• 解决方案:

    修改maven的pom.xml文件
不拷贝资源文件
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources> 修改打包方式
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
</configuration>
</plugin>
  • 运行

    假设application.properties和application-{profile}.properties都在/tmp/temp/config,jar文件在/tmp/temp

    java -Dloader.path=file:///tmp/temp/config,demo-1.0.jar -jar demo-1.0.jar

  • 原理

    对比jar包中MANIFEST.MF文件在`ZIP配置前后的区别

    配置前:
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.chinaunicom.gateway.GatewayApplication

     配置后:

Main-Class: org.springframework.boot.loader.PropertiesLauncher
Start-Class: com.chinaunicom.gateway.GatewayApplication

     发现是类加载器变了,查看org.springframework.boot.loader包下所有加载器实现:

     查看五个类描述:官方文档

  • JarLauncher

    Launcher for JAR based archives. This launcher assumes that dependency jars are included inside a /BOOT-INF/lib directory and that application classes are included inside a /BOOT-INF/classes directory.

  • WarLauncher

    Launcher for WAR based archives. This launcher for standard WAR archives. Supports dependencies in WEB-INF/lib as well as WEB-INF/lib-provided, classes are loaded from WEB-INF/classes.

  • PropertiesLauncher

    Launcher for archives with user-configured classpath and main class via a properties file. This model is often more flexible and more amenable to creating well-behaved OS-level services than a model based on executable jars.

    Looks in various places for a properties file to extract loader settings, defaulting to application.properties either on the current classpath or in the current working directory. The name of the properties file can be changed by setting a System property loader.config.name (e.g. -Dloader.config.name=foo will look for foo.properties. If that file doesn’t exist then tries loader.config.location (with allowed prefixes classpath: and file: or any valid URL). Once that file is located turns it into Properties and extracts optional values (which can also be provided overridden as System properties in case the file doesn’t exist):

    loader.path: a comma-separated list of directories (containing file resources and/or nested archives in .jar or .zip or archives) or archives to append to the classpath. BOOT-INF/classes,BOOT-INF/lib in the application archive are always used

    loader.main: the main method to delegate execution to once the class loader is set up. No default, but will fall back to looking for a Start-Class in a MANIFEST.MF, if there is one in ${loader.home}/META-INF.

  • spring-boot-maven-plugin帮助

    对zip格式说明

    对jar和war说明

Springboot jar包外指定配置文件及原理的更多相关文章

  1. 手动运行jar包,指定配置文件

    打包程序 mvn package cd target 指定参数运行 java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar --spring.profile.act ...

  2. springboot项目实现jar包外配置文件管理

    背景 为实现快速搭建和开发,项目以Springboot框架搭建,springboot搭建的项目可以将项目直接打成jar包并运行,无需自己安装配置Tomcat或者其他服务器,是一种方便快捷的部署方式. ...

  3. Spring boot框架项目,使用maven命令将配置文件打包到jar包外,项目运行读取jar外配置文件

    1.1      在eclipse中配置maven命令 Mvn -e clean package -Ptest -DskipTests=true 1.1.1    执行命令之后得到jar 1.2    ...

  4. 更新jar包里的配置文件

    更新jar包里的配置文件 起因 从笔记本传了个jar到服务器,运行的时候才发现配置文件一个ip项填错了.本来很简单的问题,maven重新打包就可以了,但是30多M的jar包就因为一个配置项错了又要重新 ...

  5. 如何读取jar包外的properties文件和log4j.properties

    http://jrails.iteye.com/blog/1705464 ***************************************' 一般在项目中使用properties配置文件 ...

  6. main函数读取jar包外部的配置文件properties

    首先,Java的main方法有个初始化入参args,如下所示: public static void main(String[] args) {} 然后,在linux下执行jar包引入外部配置文件的命 ...

  7. 加载所有jar包下指定文件

    加载所有jar包下指定文件: 如spring中加载 META-INF/spring.handlers 加载 org.springframework.core.io.support.Properties ...

  8. Maven引入jar包中的配置文件未被识别

    我用的办法是直接将jar包中的配置文件复制出来,粘贴到我自己项目中的配置文件中,讯飞语音的jar包就有这种情况.

  9. XJar: Spring-Boot JAR 包加/解密工具,避免源码泄露以及反编译

    XJar: Spring-Boot JAR 包加/解密工具,避免源码泄露以及反编译 <?xml version="1.0" encoding="UTF-8" ...

随机推荐

  1. 第三次Scrum编程冲刺

    小组第三次冲刺的任务及其完成情况描述 回顾第二次任务,完成了基本业务:用户可以使拍摄视频并上传至个人的空间页面,来与关注他的粉丝分享.关注“长大故事”社区中的其他用户,来了解别人的动态.别人所发动态下 ...

  2. [转]How to Send Ethereum with Web3.js and Node

    原文:https://davekiss.com/ethereum-web3-node-tutorial/   Ethereum took the web and cryptocurrency worl ...

  3. git教程:撤销修改

    转载:撤销修改 自然,你是不会犯错的.不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行: $ cat readme.txt Git is a distributed ve ...

  4. ckeditor_配置 修改工具栏段落的标签和在文中的格式

    在默认的工具栏中自带一个格式的选项,可以编辑文字的段落属性,将文字转换为标题.ckeditor中提供了方法可以对这个标签进行修改. 正在做的项目中不叫标题1标题2,叫章.节.条... . 需要进行配置 ...

  5. ubuntu下编译小知识点

    #改变编译器选项 SET(CMAKE_C_COMPILER"g++") #出现如下错误:添加C++11特性 #error: #error This file requires co ...

  6. Spring Boot框架的自动配置

    (图片来源于网络,侵删!!!) l  @RestController 因为我们例子是写一个web应用,因此写的这个注解,这个注解相当于同时添加@Controller和@ResponseBody注解 l ...

  7. Virtualbox 虚拟机安装Linux

    背景:Win10系统   MSI主板 目标:基于Win10 利用虚拟机Virtualbox安装Linux 准备工作:Ctrl+Alt+Del打开任务管理器——>性能(查看CPU虚拟化是否开启) ...

  8. Array.sort()

    sort() : 是对数组的元素进行排序,并返回一个数组.默认排序方式是根据字符串的Unicode码表的码点. 由于取决于具体实现,所以无法保证它的时间和空间复杂度. arr.sort(compare ...

  9. 热更新-----为何使用lua进行热更

    事实上我们在安卓端是可以使用c#jit的,但是我们在ios上的代码是AOT(预先编译,静态编译)的,不能用c# jit(实时编译,即时编译). ios不能用c#热更是因为启动了CPU的No eXecu ...

  10. python画高斯分布图形

    高斯分布,也叫正态分布,是一个在数学.物理及工程等领域都非常重要的概率分布,在统计学的许多方面有着重大的影响力. 若随机变量X服从一个数学期望为μ.方差为σ^2的正态分布,记为N(μ,σ^2).其概率 ...