Charrette
Charrette

Reputation: 720

Where should I put a configuration file needed by one of my maven jar dependency?

So I have 2 projects, let's say Project1 & Project2.

The first one is a Java application (no maven) which is working well, reading a configuration file as follow:

public static void init() {
  Path path = Paths.get("config.properties");
  byte[] data = Files.readAllBytes(path);
  ...
}

I created project1.jar from this project, and I'm using it as a maven dependency in my Project2, which is a Dynamic Web Project. This dependency is well included because I'm able to use other part of it without any trouble.

The problem I get now is when I try to call the init() method from my Project2, I get a java.nio.file.NoSuchFileException.

I guess it's because I have to put that config.properties file somewhere in Project2 tree (I don't want it to be included in the project1.jar)

I took this config.properties file and tried to put it everywhere in my maven tree WebContent, META-INF, WEB-INF, target, each sub-directory of target, src... Nothing is working, I still get the Exception.

Do I have to include it in the pom.xml in some way? If yes, how? If no... Any idea?

Upvotes: 0

Views: 1401

Answers (1)

user1615664
user1615664

Reputation: 621

Straight forward. Do what RITZ XAVI Suggested. add to src/main/resources

Then replace you init function as follows.

public static void init() {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
    byte[] data = IOUtils.toByteArray(in);
    ...
}

As per how you added as maven dependency I think you must have installed the jar in your repo with custom group and artifact id

Upvotes: 1

Related Questions