Springboot jar包外指定配置文件及原理
- 解决方案:
修改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包外指定配置文件及原理的更多相关文章
- 手动运行jar包,指定配置文件
打包程序 mvn package cd target 指定参数运行 java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar --spring.profile.act ...
- springboot项目实现jar包外配置文件管理
背景 为实现快速搭建和开发,项目以Springboot框架搭建,springboot搭建的项目可以将项目直接打成jar包并运行,无需自己安装配置Tomcat或者其他服务器,是一种方便快捷的部署方式. ...
- Spring boot框架项目,使用maven命令将配置文件打包到jar包外,项目运行读取jar外配置文件
1.1 在eclipse中配置maven命令 Mvn -e clean package -Ptest -DskipTests=true 1.1.1 执行命令之后得到jar 1.2 ...
- 更新jar包里的配置文件
更新jar包里的配置文件 起因 从笔记本传了个jar到服务器,运行的时候才发现配置文件一个ip项填错了.本来很简单的问题,maven重新打包就可以了,但是30多M的jar包就因为一个配置项错了又要重新 ...
- 如何读取jar包外的properties文件和log4j.properties
http://jrails.iteye.com/blog/1705464 ***************************************' 一般在项目中使用properties配置文件 ...
- main函数读取jar包外部的配置文件properties
首先,Java的main方法有个初始化入参args,如下所示: public static void main(String[] args) {} 然后,在linux下执行jar包引入外部配置文件的命 ...
- 加载所有jar包下指定文件
加载所有jar包下指定文件: 如spring中加载 META-INF/spring.handlers 加载 org.springframework.core.io.support.Properties ...
- Maven引入jar包中的配置文件未被识别
我用的办法是直接将jar包中的配置文件复制出来,粘贴到我自己项目中的配置文件中,讯飞语音的jar包就有这种情况.
- XJar: Spring-Boot JAR 包加/解密工具,避免源码泄露以及反编译
XJar: Spring-Boot JAR 包加/解密工具,避免源码泄露以及反编译 <?xml version="1.0" encoding="UTF-8" ...
随机推荐
- DataGridView设置行高
.Net中DataGridView控件如何设置行高 在DataGridView控件中,默认的行高很大,而标题头的行高却很小,感觉很不匀称. 标题头的行高比较好设置需要修改两个属性1修改ColumnHe ...
- 微信小程序登录(包括获取不到unionid的情况)
我们一般都是先获取到微信的 unionid,然后再通过 unionid 去登录自己的网站,就可以关联到用户在自己网站上的 user_id,但是在小程序登录中,有时候可以获取到 unionid,有时候获 ...
- 使用Docker搭建LNMP开发环境
1.什么是Docker Docker 使用 Google 公司推出的 Go 语言 进行开发实现,基于 Linux 内核的 cgroup,namespace,以及 AUFS 类的 Union FS 等技 ...
- Golang安装与环境的配置
(一) 不需FQ的情况 打开https://studygolang.com/ 下载相应的系统安装包. (二) 安装与环境配置 linux: 解压到指定目录: sudo tar zxvf [安装包的下载 ...
- Python学习笔记,day5
Python学习笔记,day5 一.time & datetime模块 import本质为将要导入的模块,先解释一遍 #_*_coding:utf-8_*_ __author__ = 'Ale ...
- C++报错
编译的时候会报如下错误:warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable ...
- Python线程的用法 函数式线程_thread和threading 样例
函数式线程写起来比较简单,但是功能没有threading那么高级,先来个函数式编程样例: #!/usr/bin/python #coding: utf-8 #————————————————————— ...
- Redis不支持ssl
一直在公司内部推荐redis做cache管理,今天偶然想起虽然C#没问题,可是c/c++没查过可不可行. 结果查了一下,还真tmd有问题,官方的c client版本只支持linux side的,根本没 ...
- Mac OS X L2TP Client Setup
原文链接:http://www.softether.org/4-docs/2-howto/9.L2TPIPsec_Setup_Guide_for_SoftEther_VPN_Server/5.Mac_ ...
- 如何长期试用Beyond Compare 4
打开Beyond Compare 4,发现已经过了试用期 我们可以点击立即购买,购买相关的Beyond Compare 4产品,如果你已经有密钥了,可以选择使用密钥 如果还想继续试用,则找到自 ...