java - Maven standard convention for application properties file -
i hava maven project of type jar. intended standalone java se application running service. usual has own app.properties file properties database connection information goes. not project ment uploaded maven central, it's supposed stay in organization , makes sense have property file alongside jar.
i have seen many places people put applicaiton properties file inside src/main/resources. gets copied inside jar excatly dont' want.
i can manage ask maven copy inside example src/main/external-resources/*
target/*
when package project can java -jar ...
, properties file alongside jar.
but messes netbeans expects properties file inside src/main/external-resources
- how make app.properties file available in classpath of ide when debug it. means can access file
new file("app.properties")
- how make when package application app.properties file goes alongside .jar file.
basically want able debug program in ide , use properties file new file("app.properties")
, when application packaged or assembled want java -jar ... , application can still access it's property file (which alongside jar) new file("app.properties")
bonus: if not possible convention , have work arround it, why .... haven't maven changed date?
there many ways of achieving desired output. assume have app.properties default values should deployed application. assume further, have many libraries ship application, too.
what in such case assembling zip file following content:
<root> | +--- lib/ | | | +--- [all libraries here] | +--- application.jar (of course named want) | +--- app.properties
there maybe properties file configuring logging framework, place root directory. application.jar correctly refers jars , root dir in class-path section of manifest file.
this zip file can assembled maven-assembly-plugin. @ least, do. done in own project exists purpose. project contains app.properties file defaults in directory think suitable.
if want test project running main classes directly ide (like eclipse), can got app.properties in src/test/resources
directory. on class path while running ide, not end in jar file. , on class path when running tests.
does fit needs?
edit per request:
you should place app.properties
file project directory src/test/resources
. way, in class path when running main classes ide (for testing purposes). way in class path of running tests maven.
second, should not load app.properties
file new file(...)
. use class loader purpose, correct way load resources in program:
classloader loader = thread.currentthread().getcontextclassloader(); url appresourceurl = loader.getresource("app.properties");
note, don't provide path (or package, or whatever) when loading resource. file name. because file in root of class path. when running tests example, due fact directly located in src/test/resources
.
now want deploy application. application not consist of main jar file, depends on many library jar files , on configuration files, such app.properties
file, want placed outside main jar file.
the maven philosophy have 1 generated artifact. let untouched, create new project has purpose of creating zip file deployment. can (i did in many projects, too) adjust project's pom generate jar file , assembled zip file.
the pom should contain following plugin:
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-assembly-plugin</artifactid> <configuration> <descriptors> <descriptor>src/main/assembly/zip.xml</descriptor> </descriptors> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
read assembly plugin, has many configuration options. descriptor element refers descriptor file, configures how zip file assembled. use built-in configurations. read documentation.
a last step must configure jar plugin manifest contains correct class path. example:
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-jar-plugin</artifactid> <configuration> <archive> <addmavendescriptor>false</addmavendescriptor> <manifestentries> <class-path>config/</class-path> </manifestentries> <manifest> <addclasspath>true</addclasspath> <classpathprefix>lib/</classpathprefix> </manifest> </archive> </configuration> </plugin>
note, did place configuration files directory called config (only in resulting zip). must part of class path.
if afterwards extract zip file directory, can start program whatever method like. runnable jar or executable (or maybe script) alongside jar.
Comments
Post a Comment