由于多个感叹号,SpringBoot 无法从 Runnable jar 加载 属性 文件
SpringBoot Unable to load Property file from Runnable jar due to multiple exclamation mark
我有一个 Spring 启动项目,在本地一切正常。现在,当我通过 jenkins 创建一个 运行nable jar 到 运行 时,它无法加载 属性 文件。
以下是配置 属性Placeholder 的代码:
@Bean
public static EncryptablePropertyPlaceholderConfigurer propertyPlaceholderConfigurerEncrypted() {
String env = System.getProperty("spring.profiles.active") != null ? System.getProperty("spring.profiles" +
".active") : "ci";
EncryptablePropertyPlaceholderConfigurer ppc =
new EncryptablePropertyPlaceholderConfigurer(getStandardPBEStringEncryptor());
ppc.setLocations(new ClassPathResource("application.properties"),
new ClassPathResource("application-" + env + ".properties"));
return ppc;
}
为了调试,我在其中添加了以下代码:
try {
String s = new String(Files.readAllBytes(new ClassPathResource("application-" + env + ".properties").getFile().toPath()));
LOG.info(s);
} catch (IOException e) {
LOG.error("Unable to read file",e);
}
它给出了这个错误:
java.io.FileNotFoundException: class path resource [application-qa1.properties] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/var/hudson/workspace/pv/target/T-S-21.3.40.jar!/BOOT-INF/classes!/application-qa1.properties
17:23:44 at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217)
我已确认文件位于此位置的 jar 中 BOOT-INF/classes/application-qa1.properties
所以实际上问题是由于从 jar 加载文件时路径中出现第二个感叹号 /var/hudson/workspace/pv/target/T-S-21.3.40.jar!/BOOT-INF/classes!/application-qa1.properties
理想情况下,感叹号应该只出现在 jar 名称之后。
有人可以建议如何解决这个问题。
您不能从这样的 JAR 文件中读取文件。您必须像这样使用 getResourceAsStream:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("application-" + env + ".properties");
我有一个 Spring 启动项目,在本地一切正常。现在,当我通过 jenkins 创建一个 运行nable jar 到 运行 时,它无法加载 属性 文件。 以下是配置 属性Placeholder 的代码:
@Bean
public static EncryptablePropertyPlaceholderConfigurer propertyPlaceholderConfigurerEncrypted() {
String env = System.getProperty("spring.profiles.active") != null ? System.getProperty("spring.profiles" +
".active") : "ci";
EncryptablePropertyPlaceholderConfigurer ppc =
new EncryptablePropertyPlaceholderConfigurer(getStandardPBEStringEncryptor());
ppc.setLocations(new ClassPathResource("application.properties"),
new ClassPathResource("application-" + env + ".properties"));
return ppc;
}
为了调试,我在其中添加了以下代码:
try {
String s = new String(Files.readAllBytes(new ClassPathResource("application-" + env + ".properties").getFile().toPath()));
LOG.info(s);
} catch (IOException e) {
LOG.error("Unable to read file",e);
}
它给出了这个错误:
java.io.FileNotFoundException: class path resource [application-qa1.properties] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/var/hudson/workspace/pv/target/T-S-21.3.40.jar!/BOOT-INF/classes!/application-qa1.properties
17:23:44 at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217)
我已确认文件位于此位置的 jar 中 BOOT-INF/classes/application-qa1.properties
所以实际上问题是由于从 jar 加载文件时路径中出现第二个感叹号 /var/hudson/workspace/pv/target/T-S-21.3.40.jar!/BOOT-INF/classes!/application-qa1.properties
理想情况下,感叹号应该只出现在 jar 名称之后。
有人可以建议如何解决这个问题。
您不能从这样的 JAR 文件中读取文件。您必须像这样使用 getResourceAsStream:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("application-" + env + ".properties");