0

I am accessing the properties file in my code with

    ClassName.class.getClassloader().getResourceAsStream("config/config.properties"));

and

    this.getClass.getClassloader().getResourceAsStream("config/config.properties");

If the properties file is in the resources directory, everything works fine and I run the program with the maven exec plugin without any issues. I just have to call

    mvn exec:java -Dexec.mainClass="MyMainClass"

But this approach as a big disadvantage. It includes the properties file

How do I change maven configuration so the 'config/config.properties' file is available to the maven exec plugin without being inside 'src' and without being included in the jar?

NOTE: Just to Clarify, I don't know where the necessary config file will be when the application runs in production. I just know it is in the classpath. This is why I need to use getResourceAsStream() and I need it not to be included in the jar file.

  • This is variant of http://stackoverflow.com/questions/18931181/hoe-to-pass-value-to-maven-pom-xml-at-rum-time-from-java-file – Robert Scholte Sep 16 '14 at 18:37
  • @RobertScholte Please explain what you are trying to say. I looked at the question and I don't see any information there that is useful to this question. – Alexandre Martins Sep 16 '14 at 21:32
  • "How do I make it so the 'config/config.properties' file is available to the maven exec plugin without being in the resources directory and without being included in the jar." Where *can* it be located? It's got to be somewhere. – Daniel Kaplan Sep 16 '14 at 23:46
  • @tieTYT Is this clearer? – Alexandre Martins Sep 17 '14 at 14:24

1 Answers1

1

If you use getClassloader().getResourceAsStream() it means you are trying to access a file which is available on the classpath. However, you are referring to a file which is not on the classpath. So you have to options:

  • add the config-folder to the classpath. You can do that by adding a <resource> to the pom.xml, but the effect is exactly the same as having src/main/resources/config/config.properties.
  • don't read the properties from a classpath file, but from a file on a known location. This would be my preferred solution. For this solution I'd like to refer to the other link. In your case you could add it as an argument, pick it up from the public static void main(String[]). Or read it from new File("config/config.properties"), which works because it is calculated relative to the execution root.
Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
  • Hello. Unfortunately, using the `resources` tag does not stop the config file from being included in the jar. And using `file()` cannot access the classpath which was the reason I used `getResourceAStream()` in the first place. – Alexandre Martins Sep 18 '14 at 08:46
  • 1
    Exactly, you *must* choose between the two: either a classpath resource or a file. However, if you have a `pom.xml`, then there is a third option: http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html#additionalClasspathElements – Robert Scholte Sep 18 '14 at 17:30
  • That is exactly my question -> "How do I configure maven so that the **maven exec plugin** can access a config file in a directory outside 'src/'" – Alexandre Martins Sep 18 '14 at 17:58
  • 1
    You don't configure Maven, just the plugin: I think it's allowed to refer to folders, so try: config and now read just 'config.properties' from classpath. – Robert Scholte Sep 18 '14 at 18:36
  • I think I have tried the `` tag before asking this question. Let me try again and see if I missed something. – Alexandre Martins Sep 19 '14 at 07:54
  • The `` worked. The config file was in the jar when I tried the first time because of another configuration in the pom.xml. Thanks for the help. – Alexandre Martins Sep 19 '14 at 08:24
  • Spoke too soon. After a `mvn clean`, the `mvn exec:java` command cannot find the config/config.properties file anymore. It's not in the jar but it's not in the execution classpath either. – Alexandre Martins Sep 19 '14 at 08:35
  • 1
    In the end, the java executable is executed (visible by adding -X to the commandline). Please read http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html for the classpath specs. And maybe you could try it without Maven first to ensure that it should be possible. – Robert Scholte Sep 19 '14 at 14:08
  • What you suggested is not forgotten. I just haven't been around this until now. – Alexandre Martins Oct 03 '14 at 11:34