I am working on a java maven project with several modules.
I am facing issues with sharing configuration files from one module to its dependency.
For instance i have a module named utils which holds a log.properties file and i would like to use it in another module named gui. What is the best practice to do this ?
Currently we put the log.properties in a config directory as Maven standard layout suggest it, and it is not included in the jar file. Is it correct ? Should I put it in resources instead ?
I use assembly plugin to copy it to a common config directory, this works well, but when I try to build each module individually the config file cannot be reached. How can i solve this ?
Thanks for your help,
Pierre.
You should put your configuration in src/main/resources/config/. This way it will be included in the jar by default. The maven convention is that only src/main/java and src/main/resources are contained in the final jar by default.
Making property files directly accessible to other modules is not a good practice. You should provide a service in the module owning the configuration that is the only place where those files are accessed. This service will be able to give configurations to other modules. Otherwise you violate the single responsibility principle.
You can configure maven to include the src/main/config directory in the built artifact by specifying it in the resources section of the pom as described here.
Related
So my project is dependent on some another one which is kinda shared config for several projects.
I have added it as dependency using maven. Now I can easly import classes from this depeneney project and use them.
So the question is how to copy some src/resources or test/resources files into my project or maybe I dont need to copy them but how to point to those files ? As I can import classess so this jar should be in classpath so how to point to resources?
classpath:shared_project_name.jar/src/resources/<file>
Maybe I should't use jar name?
If you are sharing stuff between several projects i suggest you check out Monorepos.
I am creating a multi module maven project and want to keep resources (property files, json) in a separate module. Now to import this resource module in other modules, I have two option - either to use maven-resources-plugin or to use maven-remote-resources-plugin. I already have working code wrt maven-resources-plugin but while searching in internet I found post only on maven-remote-resources-plugin which made me question if I am following maven standards on resources use.
And is it worth to move from maven-resources-plugin to maven-remote-resources-plugin?
Any pointers with respect to this is appreciated.
"...want to keep resources (property files, json) in a separate
module"
You can use Maven Remote Resources Plugin Of course to bundle your resources in a single JAR that can be used across.
This plugin is used to retrieve JARs of resources from remote
repositories, process those resources, and incorporate them into JARs
you build with Maven. A very common use-case is the need to package
certain resources in a consistent way across your organization: at
Apache it is required that every JAR produced contains a copy of the
Apache license and a notice file that references all used software in
a given project.
Maven Resource Plugin has its own usage of copying resources within modules when you don't have them located in the default maven path and want to use them explicitly.
I have a multi-module maven project with one root and multiple child projects. I am planning to use SL4J for logging. However, I have to place the log4j.properties in all child projects to be able to configure log4j.
To be able to reuse a single properties file, I tried keeping the file in src\main\resources directory of the root. However, sl4j complains that it cannot find the properties file unless it exists in each individual project.
Is it possible what I am trying to do? If so, how do I do that?
The 'distributable' is responsible for the final log4j.properties, not every dependency is has. However, tests will probably complain about the absence of this file, so put a version of log4j.properties under src/test/resources.
I have a Spring Integration requirement , where I need to externalize the libraries and properties file from my war file. I am able to achieve this through Maven assembly plugin, where i create a zip file which may contain
lib/*.jar
properties/{artifactId}/*.properties
The reason I am adding the artifact Id to the path is, I will be creating 100s of wars in future and would need to distinguish between them.
This wars will not contain Web.xml and the Initializer is part of one of my libraries file.
THe Initializer should know the artifactId in order to load the correct properties.
With maven, the maven artifact details gets published to
META-INF/maven/${groupId}/${artifactId}/pom.properties
META-INF/maven/${groupId}/${artifactId}/pom.xml
If I could move these files to
META-INF/maven/pom.properties
META-INF/maven/pom.xml
My application would be able to read the artifact id from pom.properties.
I need help in achieve this.
Or if there are any other approach please help in solving the issue.
The Maven archiver component does that: see here at addMavenDescriptor element. It doesn't seem to be possible to customize the paths of these files.
But I guess every property you need can just be placed in a specific file and so you just have to create a resource file (properties like) containing all the information you want and let Maven filter that file for you.
My maven top level project refers to a common-db project. In this project I have a spring file which defines the DB parameters.
However, I want the top-level project to define the DB parameters through the profile and inject these into the spring config file in /src/main/resources.
The top-level project only does the filtering on its own /src/main/resources files and ignores those located in the JAR dependencies.
How can I do this?
So you want to depend on common-db but then modify its contents to change the parameters in the config file? Ok, if you really want to do that, you could do something convoluted where you use dependency:unpack to expand the common-db jar, then overwrite / filter its contents, and then use a custom jar:jar execution to re-jar up the dependency and ship it with your application.
But, wow - why would you jump through all these hoops? Like #hoaz suggested, just place your application-specific config in the same classpath location so that it is loaded before common-db's default configuration. This is the convention followed by many, many Java libraries.