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

打包优化分离配置文件后,服务器部署时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
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值