报错问题: 未找到数据库url
等配置信息。Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
错误日志:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
背景:
通过dockerfile打包部署后,在启动时提示,配置数据源失败:未指定“url”属性,无法配置嵌入式数据源Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
。
排查过程:
在看到 'url' attribute is not specified and no embedded datasource could be configured.
未指定“url”属性,无法配置嵌入式数据源后,首先确认编译后的target
包或生成的jar
包的./classes/
目录下是否存在application.yml
或application.properties
配置文件,经确认发现编译后的jar
包内并没有此配置文件,那么问题原因就找到了,真相只有一个,就是Maven在编译的过程中没有将普通配置文件编译进jar
包内。
报错原因:
我们知道Maven是根据pom.xml
执行任务,其中build
标签描述了如何来编译及打包项目,而具体的编译和打包工作是通过build中配置的 plugin 来完成。而对于**resources
目录下资源,往往不是代码(.properties
或XML配置文件
),无需编译,所以在构建过程中往往会将资源文件从源路径直接复制到指定的目标路径。**
src/main/java
和src/test/java
这两个目录中的所有*.java
文件会分别在comile
和test-comiple
阶段被编译,编译结果分别放到了target/classes
和targe/test-classes
目录中,但是这两个目录中的其他文件都会被忽略掉。
解决办法:
需要在你的项目的pom文件中添加一下配置
<build>
<resources>
<!-- 解决mapper绑定异常 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<!-- 解决未找到数据源等配置文件异常 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
</includes>
</resource>
</resources>
</build>