0

I have a repository which looks something like this:

/
/resources/resource1.txt
/resources/resource2.txt
/code/
/code/C/...
/code/java/pom.xml
/code/java/src/main/XYZ.java
/code/java/src/main/ABC.java

In other words, there is a maven project rooted in /code/java/, and there are resource files located at /resources/.

Now, I want to refer to the resource files in my code. The code will be something like the files in /code/java/src/main/XYZ.java. How should I refer to this file?

I do not want to hard code the filepath from my local machine. For example, I do not want something like /Users/agnishom/projectX/resources/resource1.txt. This will make it difficult for other people who clone my repository to make it work.

I would like to be able to name a file by their relative path, possibly relative to the pom.xml file.

What is the best way to do this? Moving the resource files into the /code/java/ is not an option because they are shared by other projects which are not part of the java

  • You say “ there is a maven project rooted in /code/java/”, but then the Java source should be under `/code/java/src/main/java`. Still, you can use the `..` parent directory notation to keep the paths relative (eg try `../../resources` ) – racraman Feb 08 '22 at 00:11
  • @racraman yes you are right about the file paths. I have corrected them – Agnishom Chattopadhyay Feb 08 '22 at 00:16
  • Does this answer your question? [Handling unconventional source directory for a web project in maven](https://stackoverflow.com/questions/224373/handling-unconventional-source-directory-for-a-web-project-in-maven) – Joe Feb 08 '22 at 00:23
  • First follow convention over configuration paradigm and structure your project different. Source code `src/main/java/` unit- and integration tests into `src/test/java/` and resources which should be packaged with the productive code into `src/main/resources`. Resources which are only needed for unit- and integration testing `src/test/resources` (https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html) – khmarbaise Feb 08 '22 at 05:44
  • If you follow the conventions it's easy to access resources within Java code via `this.getClass().getResourceAsStream("/resource1.txt")` – khmarbaise Feb 08 '22 at 05:48

1 Answers1

1

In the build section of your pom.xml, you can specify the resources directory relative to the project's base directory (/pom.xml), as you were suggesting:

<build>
    ...
    <resources>
        <resource>
            <targetPath>.</targetPath>
            <directory>${basedir}/../../resources</directory>
        </resource>
    </resources>
</build>

targetPath is the path to where included resources should be placed, relative to the jarfile/resulting build.

directory is the directory to include resources from.

Rogue
  • 11,105
  • 5
  • 45
  • 71
  • Thanks for this suggestion! How do I access the files in this directory with Java? – Agnishom Chattopadhyay Feb 08 '22 at 00:37
  • You'd use the classloader to include the file like normal resources: `someClassLoader.getResource("myfile.txt")`. You can access the main classloader from any class loaded within your project (e.g. `MyMain.class.getClassLoader()` or `this.getClass().getClassLoader()`. – Rogue Feb 08 '22 at 00:40
  • Accessing resources via relative path outside the given module is a bad idea. – khmarbaise Feb 08 '22 at 05:41
  • 1
    @khmarbaise in this case, it sounds like the entire project is multi-language, and it doesn't make much sense to have a `pom.xml` at the top-level if java is a minor part of the project. I assumed that someone cloning the project would be getting everything from OP's top-level (including `/code` and `/resources`). – Rogue Feb 08 '22 at 14:44
  • Which does not follow conventions which will produce confusion.. – khmarbaise Feb 08 '22 at 14:57
  • That's why you document these things. Not everything in the world follows convention, and short of "refactor the entire project around Java" I'm not seeing a much easier alternative. – Rogue Feb 08 '22 at 16:38