Eleanor
Eleanor

Reputation: 306

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

My project uses Maven and is a little bit complicated. 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. myfile is deep within the java folder. So in myfile, I want to refer to the relative path of target file. What is the path?

Upvotes: 3

Views: 5393

Answers (1)

Chris K
Chris K

Reputation: 1723

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:

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

Upvotes: 3

Related Questions