Recommended way of adding dynamic config files to classpath?
Answered
I have modules A, and B.
Module B is a webapp and is dependent on A.
I need to provide config files to both modules. For example, logback.xml. However, they need to be different depending on where it's called.
If I run a class in module A, I need to use the logback.xml that's associated with that module. If I run webapp B, I need to use the logback.xml that's associated with that module.
What's the recommended way to set something like this up in IDEA?
Please sign in to leave a comment.
I don't think such configuration should be specific to IDE. Is your project Maven or Gradle based? How would you solve it there?
We use a very customized ant build, which handles our requirements by building them in separate directories.
What I'm looking for is the IDEA-specific way of doing this. I've tried adding runtime only class directories to the module dependencies, but that doesn't work quite right and the webapp artifact generates a whole lot of warnings.
You can use artifact configuration to override certain files, see https://www.jetbrains.com/help/idea/artifacts.html. It's possible to disable sorting and move certain elements to the top so that you can add the logback.xml file from a different location that will override the default one from the compiler output.
I've looked into that a little bit, but I'm not sure I understand how that relates to classpaths. When you configure a run/test it asks for the module to use as it's classpath. Can I specify an artifact instead? How would that work?
I thought you have a web application that you are deploying on some container in .war or .ear.
If it's not the case, you can override classpath in the module dependencies: https://www.jetbrains.com/help/idea/2017.1/working-with-module-dependencies.html.
Move the resource to the top so that it has the priority, you can configure fake modules which only have the dependencies and use the classpath of this fake module when running.
Specifying classpath for run/debug configuration is not supported yet: https://youtrack.jetbrains.com/issue/IDEA-160167.
I've tried this with module dependencies, but it doesn't look like things gets overridden when using module dependencies.
Module A has a dependency on a directory that contains logback.xml.
Module B has a dependency on A and a directory that contains logback.xml.
Both logback.xml show up in the classpath.
I would expect that the one that shows up first would be included, and others ignored, just as it does with artifacts. Is this not the way it's supposed to work?
As far as I understand, the resource appearing first in the classpath will have the priority, note that you can reorder classpath elements in IntelliJ IDEA module dependencies.
Yes, I've ordered the module dependencies the way I need them. However, it doesn't seem to be eliminating duplicates. All duplicates show up in the classpath, even if they are ordered correctly.
It will not remove duplicates and it shouldn't, but the first resource should have the effect.
OK, then I still need a way to provide config files to different modules.
For example, logback.xml. I can only have one of them in the classpath. How can I provide a unique logback.xml to each module?
If one module depends on another and both have logback.xml, final classpath will include 2 copies of the resource, there is nothing you can do about it in IntelliJ IDEA configuration.
As suggested before, you can create a new module which will have no content roots or sources, but only dependencies. In this module dependencies you can include the libraries from 2 modules you need, but only one logback.xml. In the run/debug configuration you can tell IntelliJ IDEA to use this module to resolve the dependencies.