1

So I've been trying to deploy a project as a jar and it needs to read a file this way:

private static final String DEFAULT_PATH = "org/some/thing/cool/necesaryFile.xml";

public void readSomeFile() {
    InputStream is = null;
    try {
        ClassLoader classLoader = SomeOtherClass.class.getClassLoader();
        is = classLoader.getResourceAsStream(DEFAULT_PATH);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        safeClose(is);
    }
}

public static void safeClose(InputStream is) {
    if (is != null) {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I've tested this inside Eclipse and it works fine! Debugging I saw the path and there was no problem at all! When I deployed the jar with maven it showed no error, but when I try to run project A with the jar as a dependency, I get

java.lang.IllegalArgumentException: InputStream cannot be null

And checking the jar inside project A, I can't find the necesaryFile.xml that was there before deployed.

The path for the xml file was like this:

C:/coolUser/workspace/src/main/java/org/some/thing/cool/necesaryFile.xml

so it was inside cool package with some java class.

So my question is, is there a way to tell maven not to erase the file (if that's even what's happening) when deploying the jar? Moving the location of that xml file is not an option (or at least I hope not to do it)

The maven command :

mvn clean deploy -U -Dmaven.test.skip=true -DaltDeploymentRepository=coolRepo::default::${REPO_LOCATION}
Mokz
  • 124
  • 1
  • 16
  • Please post your pom file(especially the plugin configuration which you are using to create jar), you might have to specifically include .xml file while building the jar. Since you have placed your .xml file in src/main/java, it will not get packaged automatically. If you place under src/main/resources then it would be automatically included. – Madhav Kumar Jha Sep 30 '18 at 05:26

1 Answers1

1

Place your .xml file in src/main/resources not in src/main/java. The recommended way is to keep them in resources. If you still want to keep them along with java class, then you have to explicitly tell the plugin (which you are using in your pom to build jar) to inlude xml files.

Madhav Kumar Jha
  • 353
  • 1
  • 2
  • 13