I have a simple java maven project. One of my classes when executing needs to load an xml configuration file from the classpath. I don't want to package such xml file when producing the jar but I want to include a default xml file in a zip assembly under a conf subfolder and I also want this default xml to be available in the unit tests to test against it.
As I see it there are 2 possible places of this default xml:
src/main/resources/conf/default.xml
src/main/conf/default.xml
Both solutions demand special pom actions:
In solution 1, I get the auto copy to target folder during build which means it is available in testing but I also get it in the produced jar which i don't want.
In solution 2, I get the jar as I want it(free of the xml) but I manually have to copy the xml to the target folder to be available for testing. (I don't want to add src's subfolders in test classpath. I think it is bad practice).
Question: what is the best solution of the two?
- If the correct is 2, what is the best way to copy it to target folder?
- Is there any other solution better and more common than those two?
(I also read Where should I put application configuration files for a Maven project? but I would like to know the most "correct solution" from the "convention over configuration" point of view and this link provides some configuration type solutions but not any convention oriented. Maybe there isn't one but I ask anyway. Also the solutions provided include AntRun plugin and appAssembler plugin and I wonder if I could do it with out them.)
You could place it in src/test/conf/default.
The name of that configuration file is always config. properties. The configuration files must be found from the profiles/[profile name] directory. The development profile must be active by default.
In a project that follows the mavenSW conventions, resources such as *. properties files can be placed in a src/main/resources directory. The files/folders in this directory will be copied to the root level of the jarW (or other similar package) that is generated for the project.
On assembly, an application's configuration path is set to one or more configuration directories. These directories are set from the Configuration path attributes in module manifest files.
The question is what is the best solution of the two? If the correct is 2, what is the best way to copy it to target folder? Is there any other solution better and more common than those two?
Since you want that file to be copied to the target/classes
folder, it has somehow to be considered as a resource (so either put in under src/main/resources
or declare src/main/conf
as resource directory). And if you don't want it in the final jar, configure the Maven JAR Plugin to exclude it:
<project> ... <build> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <excludes> <exclude>**/conf/*</exclude> </excludes> </configuration> </plugin> ... </plugins> </build> ... </project>
For the assembly part, assembly descriptors are pretty flexible so it should be possible to achieve what you want regardless of the choice. I'd suggest using the easiest setup though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With