This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Ant, Maven and Other Build Tools and the fly likes how to deploy different hibernate config file for qa/prod via maven Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "how to deploy different hibernate config file for qa/prod via maven" Watch "how to deploy different hibernate config file for qa/prod via maven" New topic
Author

how to deploy different hibernate config file for qa/prod via maven

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi all,

I am running mvn compile war:war command to copy my project's war to the target for my server.

Now, I use hibernate config file that has db connection properties and I want to do following,
1. When I run mvn compile war:war with some additional properties "-Dtarget.db=prod" it should pickup hibernate config file for prod and copy it as hibernate.cfg.xml and then do deploy that to my tomcat in prod

I am not sure how to do this. Can anybody help me?

I know how to use properties in the maven pom.xml but I can't figure out how to do the above.

Any help is greatly appreciated.

PS: I know I can do similar task in Ant build file via copy task where depending upon property I can copy hibernate.cfg.${propname} as hibernate.cfg.xml and achieve the above effect but I want to do it via maven.

Regards
Maulin
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi there, you want to investigate Maven's "profile" concepts and the usage of the assembly plugin to achieve this. In short you specify which profile you're running maven for (e.g. PRD) and the assembly plugin helps you pick up the right files..


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Thanks Martijn. I am starting to look into it.

Regards
Maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi

I could do it by using only Profiles.

First as suggested, I learned how profiles work in maven and then I found the following thread on stackoverflow.

http://stackoverflow.com/questions/512516/how-to-copy-from-one-file-to-another-in-maven

(I didn't have to use assembly plugin at all. I read about it but felt thats not going to help my use case before I found above thread that exactly addressed what I wanted)

Essentially all I had to do was -
1. Create dev and prod Profiles
2. Within the profile I had to put build->resources->resource->directory xml element hierarchy and specify appropriate dev and prod resource folders

Slowly I am starting to love Maven! So far its amazing.

Regards
Maulin
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Well done! The best way is always to figure yourself
Karthik Rajendiran
Ranch Hand

Joined: Aug 13, 2004
Posts: 211
Hello Maulin

Let me think if i have understood your requirement correctly.
You have hibernate.cfg.xml which has entries varying wrt to environment.
1. Mostly it will be DB Connectivity things
2. Some tags specific to environment.

Questions
1. Let us assume you pack the configurations in tar or jar and give to deployment team.
Will you repack the jar for every environment or is it one time packing and
environment team keeps deploying in different environment.
for ex
you pack configurations for QA environment.
Once testing completes, will you give a release for prod or will you use the qa environment TAR.GZ for production deployment.


Case 1
If same tar to be used across environment, and settings to be changed localised to that environment.

Solution1.
Step 1. Let us take hibernate.cfg.xml, it has

Now this value varies wrt to varies environment.

Step 2. Create a Project called projectResources
with this file as hibernate.cfg.xml.template
edit the file in such a way


Step 3.
Have a properties file
local_db_connection_string=jdbc:Oracle:thin:@x.x.x.x:x:SID
Note: This properties will be residing in every environment, carrying values wrt to that environment.
QA local.properties will be
local_db_connection_string=jdbc:Oracle:thin:@x.x.x.x:x:QASID
PRod local.properties will be
local_db_connection_string=jdbc:Oracle:thin:@x.x.x.x:x:PRODSID

Step 4.
Now while giving a release , we have to pack the template file instead of hibernate.cfg.xml, in this way
we have a release generic to all environment
Step 5.
Now we have to use small program using velocity to replace the macro with values actual before deploying.
Check velocity how to replace macro with values.
pS: Any information /help on this let me know

SCJP 1.4 SCWCD 1.4 SCDJWS 1.4
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960
    
  19

I don't like to create multiple versions of a WAR myself. It's much easier to debug production problems if the code is identical on the test sytem.

Instead, I supply the server/platform-specific data externally. For databases, that means using the server's database connection pooling facilities to that I can selected whichever database server I want at the appserver-level. For other things such as slapping a nice big "TEST" banner on the webpages for test apps, I use JNDI, code resource definitions in the web.xml file and override them in the server's deployment description.

I do have some projects using profiles, but that's because the test machines are Linux and the production machines are IBM mainframes and I actually have to vary the JARs included, since WebSphere has certain features built into it that Tomcat doesn't. There's limits to what you can externally configure, after all.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to deploy different hibernate config file for qa/prod via maven
 
Similar Threads
uPortal 3.2.4 Build Failed
Simplifying exploded format deployment to weblogic using Maven
properties-maven-plugin-1.0.jar plugin's descriptor contains the wrong version: 1.0-SNAPSHOT
Can't unzip war file
Hibernate mapping:Bean class & MySQL DB View