正在将 application.properties 文件外部化到 side war 之外。当我通过 jenkins 创建 war 文件时显示错误。
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-11-28 18:06:07.900 ERROR 22612 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
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).
2019-11-28 18:06:07.909 ERROR 22612 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@233795b6] to prepare test instance [com.assessmentAggregator.api.AssessmentAggregatorApiApplicationTests@74024f3]
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) ~[spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) [spring-test-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12] at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12] at
为了外部化属性文件,我使用了这段代码。
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder
.properties("spring.config.name:" + AppConstants.Config.APPLICATION_PROPERTY_NAME,
"spring.config.location:" + AppConstants.Config.APPLICATION_PROPERTY_SOURCE_PATH)
.sources(AssessmentAggregatorApiApplication.class);
}
和值
AppConstants.Config.APPLICATION_PROPERTY_NAME= String APPLICATION_PROPERTY_NAME = "foxmatrixapp.properties";<br/>
AppConstants.Config.APPLICATION_PROPERTY_SOURCE_PATH=String APPLICATION_PROPERTY_SOURCE_PATH = "file:////mnt/assessment_aggregator/data/pdf/";
并在 pom.xml
中添加了此代码用于排除 application.properties
的文件我使用的war文件中的文件。
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
最佳答案
您可以使用放置在目标文件夹中的另一个属性源和点。
例如
@PropertySources({@PropertySource(value="file:somefolder/conf/bootstrap.properties", ignoreResourceNotFound=true),
@PropertySource(value="file:conf/bootstrap.properties", ignoreResourceNotFound=true)})
在保存在somefolder/conf中的服务器文件中,然后加载文件中定义的属性。
@Value("${datasource.url}")
private String url;
@Value("${datasource.username}")
private String username;
@Value("${datasource.password}")
private String password;
一旦加载,就可以在任何地方使用。
ignoreResourceNotFound=true放置的原因是如果找不到属性,则不会发生异常,这都是基于场景的。您可以改回 false
关于java - 无法从 War 文件外部找到 application.properties 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59098869/