简体   繁体   English

如何在Maven项目中获取资源文件的相对路径?

[英]How to get a relative path of a resource file in a Maven project?

My project uses Maven and is a little bit complicated. 我的项目使用Maven,有点复杂。 I can't seem to get a relative path for a resource file. 我似乎无法获得资源文件的相对路径。 The structure is like this: 结构是这样的:

project_folder
    |-package1
       |-src
          |-test
              |-java
                |-folder1
                  |-folder2
                     ...
                       |-foldern
                           |-**myfile**

              |-resources
                |-folder
                  |-**targetfile**

In test , there is a java folder and a resources folder . test ,有一个java文件夹和一个resources folder myfile is deep within the java folder. myfile位于java文件夹中。 So in myfile , I want to refer to the relative path of target file . 因此,在myfile ,我想引用target file的相对路径。 What is the path? 路径是什么?

Assuming you have not changed the defaults for test source and resource location: 假设您尚未更改测试源和资源位置的默认值:

MyFile.class.getResourceAsStream("../../../folder/targetfile");

with N "../" instead of 3. It would of course be easier to do: 用N "../"代替3。这样做当然会更容易:

MyFile.class.getResourceAsStream("/folder/targetfile");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM