如何确定Maven项目中的文件路径?

dun*_*nfa 3 java maven

我正在使用Maven进行项目.在我的一个java类中,我需要获取一个属性文件.我不能使用绝对文件位置而是相对路径.我尝试了几种方法,但没有运气.

java在

myProject/src/main/java/com/some/myConfiguration.java
Run Code Online (Sandbox Code Playgroud)

属性文件位于

myProject/src/main/com/some/resources/myProperties/myFeatures.properties
Run Code Online (Sandbox Code Playgroud)

在我的java中,我有一些像这样的代码

String filePath = "correct/path/to/myFeatures.properties";
File repositoryFile = new File(filePath);
Run Code Online (Sandbox Code Playgroud)

所以我的问题是我应该给予的 filePath

谢谢你的任何建议.

Ada*_*ion 5

  • 按照Maven的标准目录布局的建议将您的myFeatures.properties属性文件放入其中src/main/resources
  • 使用以下命令在Java代码中访问它:

    this.getClass().getClassLoader().getResourceAsStream("myFeatures.properties");