Assembling java project with Maven
my question has 3 parts, short, long and additional. I will start with the short one:
Are there any good tutorial/example how to assemble some sort of typical (for example apache like assembly?) I mean tutorial that would address how to develop application using eclipse and finally get it into nice directory structure like:
bin
conf lib LICENSE.txtAnd the long part of the question is: How to appropriately place my log4j.properties without messing up my final assembly? I know there are many answers like "put it into src/main/resources" and I did and it works now in development. I put all my config files into src/main/resources because I just load the configuration files via classpath (.getClassLoader().getResourceAsStream(name);) and I assume this is the best practice. But when I put it into src/main/resources folder then when I run mvn package the log4j.properties gets put into jar. But that's exactly not what I want. I want to have nice clean jars and in the end when the software is packaged log4j.properties will sit in conf directory and the conf, directory will be on classpath somehow.
Additional question is: Is my appr开发者_开发百科oach of solving config loading via classpath good? My thinking is that I want to avoid getting into trouble with development/production path differencies. When I just assume the config will be on CP then everything get's much easier.
I will comment on the last part about how to load configuration files. Putting configuration files i classpath is used quite extensivly in Java. This works well if the configuration is fixed at deployment time and the file can be embedded in the jar, but in my experience it creates more trouble than it solves if the configuration should be changed later.
There are mainly two reasons for this. First, it makes it more difficult to know exactly which configuration file that is actually used and debug configuration issues. More than one directory may be included in the classpath, there may be files in the jar (or in another jar) etc. Classloading is one of the more difficult topics in Java, and classloading and classpath usually is an opaque topic for those who should actually change the configuration. It is better to have a fixed location where the configuration file should be, write a line to the log file with this location and create an error if the file is missing.
Second, configuration files in the classpath is sometimes placed in a package that matches the classes that reads the file. This is natural and best practise for classpath resources, but it requires you to create a matching set of folders in the directory where your configuration goes. This makes it even more difficult for those who should create or edit the file.
Log4j gives you an option where you may specify the log4j file on the command line when you start java. I always use that option to specify the exact location for my log4j configuration file.
Use the maven-assembly-plugin to create archives by combining dependencies, classes, src, resources, and artifacts as you see fit.
精彩评论