打包优化分离配置文件后,服务器部署时logback.xml加载有问题。问题就是,在ide中需要设置classpath路径,服务器部署需要设置file路径

ide中运行和测试:

#配置外部logback.xml
logging:config: classpath:./config/logback.xml

服务器部署:

#配置外部logback.xml
logging:config: file:./config/logback.xml

以下都是在服务器上部署的加载测试

一、设置成classpath

1、classpath:./config/logback.xml
启动报错:
Logging system failed to initialize using configuration from 'classpath:./config/logback.xml'
java.io.FileNotFoundException: class path resource [./config/logback.xml] cannot be resolved to URL because it does not exist

2、classpath:config/logback.xml
同样报错:
Logging system failed to initialize using configuration from 'classpath:config/logback.xml'
java.io.FileNotFoundException: class path resource [config/logback.xml] cannot be resolved to URL because it does not exist

3、classpath:/config/logback.xml
同样报错:
Logging system failed to initialize using configuration from 'classpath:/config/logback.xml'
java.io.FileNotFoundException: class path resource [/config/logback.xml] cannot be resolved to URL because it does not exist

二、设置成file

1、file:./config/logback.xml
取当前目录下,可以加载

2、file:config/logback.xml
取相对目录,可以加载

3、file:/config/logback.xml
从根目录找,启动报错:
Logging system failed to initialize using configuration from 'file:/config/logback.xml'
java.io.FileNotFoundException: /config/logback.xml (No such file or directory)

三、启动命令中添加calsspath
-Xbootclasspath/a:./ :将classpath添加在核心class搜索路径后面
作用是添加当前目录(可执行的jar所在的目录)到classpath

使用以下命令启动:

java -Xbootclasspath/a:./ -jar myboot-0.0.1-SNAPSHOT.jar --spring.config.location=file:./config/

1、classpath:./config/logback.xml
可以加载

2、classpath:config/logback.xml
可以加载

3、classpath:/config/logback.xml
启动报错:
Logging system failed to initialize using configuration from 'classpath:/config/logback.xml'
java.io.FileNotFoundException: class path resource [/config/logback.xml] cannot be resolved to URL because it does not exist

四、小结
1、使用springboot添加classpath的设置,可以解决logback.xml加载问题
2、路径设置成./config/logback.xml和config/logback.xml都可以,不要设置成绝对路径

五、新增方法
自定义MANIFEST.MF中的Class-Path
修改pom文件maven-jar-plugin插件的配置,添加:

<manifestEntries><Class-Path>. config/</Class-Path>
</manifestEntries>

就是把 . 和 config/ 加入Class-Path路径,完整效果:

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><archive><manifest><!--是否要把第三方jar放到manifest.mf的classpath中 --><addClasspath>true</addClasspath><!--生成的manifest.mf中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/ --><classpathPrefix>lib/</classpathPrefix><!-- 执行的主程序路径 --><mainClass>com.blemall.crcs.main.CrcsApplication</mainClass></manifest><manifestEntries><Class-Path>. config/</Class-Path></manifestEntries></archive><!-- 排除target/classes下的config目录 --><excludes><exclude>config/**</exclude></excludes></configuration>
</plugin>

这样不需要加-Xbootclasspath/a:./ 也能加载logback.xml文件

参考资料:
JAVA 运行springboot jar包设置classpath - 季枫 - 博客园
MANIFEST.MF文件详解 - 隐官陈十一 - 博客园 (cnblogs.com)

注:最新代码上传至https://github.com/csj50/myboot

springboot项目创建笔记29 之《springboot打包优化2—加载logback.xml问题》相关推荐

  1. springboot项目创建笔记33 之《初始化资源》

    以前用springmvc时,程序初始化资源用@PostConstruct注解和ApplicationContextAware接口. springboot提供了一个新接口可以实现这个功能,就是Comma ...

  2. springboot项目创建全局唯一id生成器

    springboot项目创建全局唯一id生成方法,参考 Snowflake算法 yml文件 #app 全局唯一id生成 app:idGenerator:workerId: 1datacenterId: ...

  3. springboot项目创建右键没有run as

    springboot项目创建右键没有run as 解决方案:右键创建项目得pom.xml文件add as maven project即可.

  4. 一个springboot 项目a集成另一个springboot 项目b

    一个springboot 项目a集成另一个springboot 项目b 并且可以运行访问b的controller层 操作1: 项目b打包依赖修改,把上面的springboot默认打包依赖注释,改为下面 ...

  5. DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描...

    DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许禁止转载    系列目录连接 DB数据源之Spr ...

  6. springboot mybatis 热加载mapper.xml文件(最简单)

    大家好,我是烤鸭: 今天介绍一下springboot mybatis 热加载mapper.xml文件. 本来不打算写的,看到网上比较流行的方式都比较麻烦,想着简化一下. 网上流行的版本. https: ...

  7. SpringBoot+Mybatis加载Mapper.xml文件的两种方式

    前言:我们在平常工作中用到mybatis去加载Mapper.xml文件,可能mapper文件放的路径不一样,由此我们需要配置多个路径,幸运的是Mybatis支持我们配置多个不同路径.现在介绍两种方法. ...

  8. Unity3D 网页插件Embedded Browser(ZFBrowser)PC端打包文件无法加载网页解决方法

    网页插件Embedded Browser(ZFBrowser)PC端打包文件无法加载网页 介绍:使用Embedded Browser开发unity项目内嵌网页,打包后发现出现一个问题网页插件无响应,而 ...

  9. 用手机UC浏览器页面打开vue项目,图片,css,js都没加载

    用手机UC浏览器页面打开vue项目,图片,css,js都没加载 用HBuilder X创建了一个普通的vue项目,在电脑上使用浏览器看没有什么问题,然后提交到了公司的服务器上,发现用手机,除了火狐,谷 ...

最新文章

  1. jQuery的三种$()
  2. 【转载】如何做一份突出的PPT
  3. linux 进程 setuid,Linux SetUID(SUID)文件特殊权限用法详解
  4. python之知乎的正确打开方式
  5. Linux系统中病毒怎么解决
  6. 关于collect2: cannot find ld的解决办法
  7. leetcode 561. 数组拆分 I(Java版)
  8. Halcon例程(基于3D形状匹配识别方法)详解 —— create_shape_model_3d_lowest_model_level.hdev
  9. c语言指针用法有哪些
  10. java 正则判断二进制_用正则表达式判断一个二进制数是否能被3整除
  11. vmrc安装出现:未能安装 HCmon 驱动程序 (Failed to install the HCmon driver)
  12. Word2013实战技巧
  13. 机器学习中各分类算法的优缺点比较
  14. nopCommerce 3.9 大波浪系列 之 汉化-Roxy Fileman
  15. shanzhi -接小球游戏4.0
  16. python列表查找整数_Python:找出整数列表是否为coheren
  17. SSD1306-7针脚OLED的使用心得
  18. 安泰:精密电流源电路原理及应用
  19. safe mode bypass and rooting
  20. SpringBoot 优雅地对接口进行数据加解密

热门文章

  1. 分享一个好用智能的,免费的机器人接口,很智能。
  2. 计算机专业的英语复试自我介绍,计算机专业复试英语自我介绍
  3. mysql中的 isnull(exper)、ifnull(exper1,exper2)、nullif(exper1,exper2)函数
  4. 关于Lock锁用法的详细讲解(案例驱动,手把手教学)
  5. 关闭 Win10 右下角天气资讯等图标
  6. java中length,length()和size()各自含义
  7. 如何在本地搭建网站(图文教程)
  8. 蠕虫病毒Worm: VBS/Jenxcus!lnk 解决方案 Python脚本
  9. 存档修改 html,3ds存档修改图文教程【利用SaveDataFiler】
  10. 量子信息基础:预备知识