Java 将外部配置添加到类路径,并使用getResourcesAsStream()访问它
我有一个应用程序需要在启动时进行配置。但是,我无法将其存储在/resources中,因为将承载此应用程序的每个环境都将完全不同。现在,我想将一个目录从文件系统添加到类路径中,比如说/tr/apps/(其中/tr/apps/是部署应用程序的根目录) 我发现了一个Maven插件,它看起来可以完成这项工作: 所以我把这个添加到我的pom中:Java 将外部配置添加到类路径,并使用getResourcesAsStream()访问它,java,maven,jvm,Java,Maven,Jvm,我有一个应用程序需要在启动时进行配置。但是,我无法将其存储在/resources中,因为将承载此应用程序的每个环境都将完全不同。现在,我想将一个目录从文件系统添加到类路径中,比如说/tr/apps/(其中/tr/apps/是部署应用程序的根目录) 我发现了一个Maven插件,它看起来可以完成这项工作: 所以我把这个添加到我的pom中: <build> <plugins> <plugin> <groupId>or
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>/tr/apps</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
此异常似乎总是被抛出(即,它无法在类路径上找到文件)。有人知道这是为什么吗?或者是否有其他插件/解决方案
如果您有任何意见,我将不胜感激
maven surefire插件
用于执行单元测试,使用additionalClasspathElement
添加到类路径的资源仅在maven的测试
阶段可用。(即,除了单元测试之外,您不能在实际应用程序代码中使用这些文件)
更新:
我不确定此解决方案是否适用于您,但可能的解决方法/解决方案是将文件位置作为JVM参数传递,然后使用FileInputStream
读取文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ConfigFileReader {
private static final Logger logger = LogManager.getLogger(ConfigFileReader.class);
public static void readConfigFile() {
String configFilePath = System.getProperty("external.config.file.path");
try (InputStream inputStream = new FileInputStream(new File(configFilePath))) {
// Do your stuff
} catch (FileNotFoundException e) {
logger.error("{} File not found", configFilePath);
} catch (IOException e) {
logger.error("IOException occurred while reading {}", configFilePath);
}
}
}
例如:
将jvm参数作为-Dexternal.config.file.path=/Users/Documents/my app config.properties
传递,然后在java代码中读取该文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ConfigFileReader {
private static final Logger logger = LogManager.getLogger(ConfigFileReader.class);
public static void readConfigFile() {
String configFilePath = System.getProperty("external.config.file.path");
try (InputStream inputStream = new FileInputStream(new File(configFilePath))) {
// Do your stuff
} catch (FileNotFoundException e) {
logger.error("{} File not found", configFilePath);
} catch (IOException e) {
logger.error("IOException occurred while reading {}", configFilePath);
}
}
}
getResource的文档说明这是不可能的。可能会捕获异常并用FileInputStream打开它。考虑到当今计算的本质(云、docker),依赖主机(容器)目录来获取环境变量可能不是一个好主意。有许多替代设计。这是一个从命令行运行的应用程序,还是基于服务器的应用程序(如Tomcat)?它是基于服务器的DHI@Ramu,这并不是问题的答案。但它确实回应了我将要添加到问题中的一条评论…@SteveC我的回答是针对这个问题的,有人知道为什么吗?谢谢你的回答,但是正如问题中所述,我必须使用InputStream InputStream=Thread.currentThread().getContextClassloader().getResourceAsStream(“tr/apps/myFile.txt”)@reyusay请查看此URL。它可能会给你一个如何做的想法。@reyusay,还有,你想用ClassLoader读取文件的特殊原因吗?