aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes How to put configuration files of Spring, JPA and log4j outside jar in standalone app? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "How to put configuration files of Spring, JPA and log4j outside jar in standalone app?" Watch "How to put configuration files of Spring, JPA and log4j outside jar in standalone app?" New topic
Author

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

Serge Yurk
Greenhorn

Joined: Apr 09, 2011
Posts: 5
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

Joined: May 14, 2008
Posts: 5772
    
    7

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)


JBoss In Action
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15629
    
  15

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Serge Yurk
Greenhorn

Joined: Apr 09, 2011
Posts: 5
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

Joined: May 14, 2008
Posts: 5772
    
    7

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

Joined: Apr 09, 2011
Posts: 5
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

Joined: Jun 25, 2001
Posts: 15629
    
  15

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

Joined: Apr 09, 2011
Posts: 5
Thanks,Tim. It works.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5772
    
    7

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

Joined: Apr 09, 2011
Posts: 5
I use this plugin as I have mentioned before.
I’ll try to teach it to assemble projects with unpacked configuration files.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to put configuration files of Spring, JPA and log4j outside jar in standalone app?
 
Similar Threads
[Newbie Issue] hibernate.cfg.xml and hbm.xmls are not packaged by Maven?
Spring Major version '3' expected
Error filterStart
Is it required to add spring jar files at Java Build Path for every new spring project?
Struts2 and Spring integration