我有一个需要在启动时进行配置的应用程序。但是,我无法将其存储在/ resources中,因为承载此应用程序的每个环境都会完全不同。现在,我想将文件系统中的目录添加到类路径-假设 / tr / apps / (其中/ tr / apps /是应用程序部署的根目录)。>
我找到了一个看起来可以完成工作的Maven插件: https://maven.apache.org/surefire/maven-surefire-plugin/examples/configuring-classpath.html
因此我将此添加到了pom中:
<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>
现在,我已经使用以下方法来获取文件:
InputStream inputStream = Thread.currentThread().getcontextClassloader().getResourceAsStream("tr/apps/myFile.txt")
if(inputStream == null) {
throw new Exception("myFile.txt not found");
}
似乎总是抛出该异常(即,它无法在类路径上找到文件)。有人知道这是为什么吗?或是否有替代插件/解决方案?
非常感谢您的投入!