Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to put configuration files of Spring, JPA and log4j outside jar in standalone app?

 
Serge Yurk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I develop a standalone application.

It consists of several modules and uses a Spring container and JPA above Hibernate.
I use Maven maven-jar-plugin and maven-assembly-plugin to build app.

I would like to have configuration files (persistence.xml, Spring context files and a log4j configuration) outside of the application’s jar.

What is the best way to do that?

Thanks in advance.


 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to help you, but you need to take care of the administrative matter first (I'm sure you got a private message about it)
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18156
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for you co-operation, Serge, and welcome to the JavaRanch!

You have 3 very different configuration files there. While it's relatively easy to locate a Spring config file anywhere you like, I'm not so sure about the log4j or persistence.xml files.

The "easy" way to handle that is to create a custom classloader, since all of those files are normally expected to be on the classpath. For a self-executing jar, however, the classpath is the jar and only the jar.

The other way would be to simply not use the jar as a self-executable and invoke it using the "java -classpath jarfile.jar x.y.z.MainClass" form of the Java command. Add additional classpath jars or directories to the "-classpath" option as needed. That would be a lot less trouble than designing and debugging a custom classloader.
 
Serge Yurk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Tim.
The last way is definitely better. But there are at least 2 issues on this way.

1. Let the main class is in a jar and configuration files are in a folder config. Question: How can I tell maven to add config to the Class-Path entry of a jar that contains the main class?

2. Let the main class is not in any jar. Question: How can I tell maven not to make jar from the main module of my project?
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. I don't think this is possible, given the options for the Maven Archiver (used by the Jar plugin to define the manifest contents). If someone else knows differently, I would like to know, because I need this capability also. If there is no way to do this, I will have to write an patch toe the jar plugin and submit it to Apache.

Maven Archiver options: http://maven.apache.org/shared/maven-archiver/index.html
See addClassPat hand classpath Prefix.


2. I am not sure I understand what you are getting at. A Maven project always builds something, usually a JAR. So are you saying that you have a class that you don't want to place into the JAR for the project?
 
Serge Yurk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
by 2 I mean the case when the main class and surrounding classes are not packed at all.
They are in file system as a bunch of files.

In this case we need to get from maven a list of used jars,
concatenate them into JAVA_CLASSPATH,
then add a reference on config to JAVA_CLASSPATH
and launch an application by command like
java $JAVA_OPTS –cp $ JAVA_CLASSPATH MainClass.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18156
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maven is intended to construct deployable objects. A loose directory isn't really a "deployable object" since it's a collection of objects. Besides, most platforms are expecting a unitary deployment, not a flood of objects, anyway, although some (such as the tomcat webapp server) can work with the exploded version of a jar (WAR).

In most jar projects, there would actually be a residual copy of the exploded files in the target directory along with the product jar itself. I'm not 100% sure in the case of executable jars, however, if there would be one with the dependencies in it or not. Note, however, that jars inside a classpath directory are not themselves made part of the classpath.

What I was recommending was more like this:


Note that the colon (":") is a semi-colon (";") when running in Windows.

Whether or not this will actually work depends on whether the custom classloader needed to access any sub-jars inside the executable jar is activated. It should be, but you'll have to test it.
 
Serge Yurk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,Tim. It works.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, take a look at the Assembly plugin, specifically the assembly:single goal. You can use it to gather compiled classes and required JAR files into a single location (or into a zip or tar.gz file) which you can ship to your users.
http://maven.apache.org/plugins/maven-assembly-plugin/
 
Serge Yurk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use this plugin as I have mentioned before.
I’ll try to teach it to assemble projects with unpacked configuration files.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic