You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Ryan Moquin <fr...@gmail.com> on 2007/07/12 21:38:08 UTC

questions about using different config files, such as persistence.xml in maven configs

Hi, I know that in order to use different configuration files (such as a
persistence.xml), such as test configurations vs. production configurations
I need to setup profiles to allow these different files to be used.  What is
the preferred way to indicate to maven how to find a configuration file that
should be used?  Such as, I have a persistence.xml file, currently I have my
test database definition in it as a separate entry from my production
configuration.  This works fine in theory because I can mock out my
persistence classes to use the test configuration for tests.  The problem is
that hibernate loads BOTH definitions when it starts up, whether it's used
or not which causes problems with hot deploying, when my production
environment is running (since my test config is loaded in production, and my
tests are trying to use that configuration).  I'm also running into problems
of needing persistence configurations that can use other database servers
other than assuming localhost.

Does anyone have a strategy or know of a link that I can look at that will
help me implement an appropriate strategy to use/bundle differnet
persistence.xml files based upon the loaded maven profile?  Thanks!

Ryan

Re: questions about using different config files, such as persistence.xml in maven configs

Posted by Wayne Fay <wa...@gmail.com>.
Your "Spring" solution (with all environments & properties available,
and a runtime switch to pick the one to use) is probably going to be
the "best" option for a lot of reasons. So keep investigating that
path, and let us know where you go with it.

I think there are probably a bunch of people on this list who would
really benefit from a complete example of such an implementation, so
if you made a little test project you could share, that would be
great.

Wayne

On 7/13/07, Ryan Moquin <fr...@gmail.com> wrote:
> Yeah that's what I was saying.. I'm going to do a little more research on
> the profiles... yeah that's what I was hoping not to do and wasn't sure if
> that's what the other solution was getting at... I'll play around and find
> something I like...
>
> On 7/13/07, Wayne Fay <wa...@gmail.com> wrote:
> >
> > You should read more about Profiles on the Maven website and in the
> > free PDF books from Mergere and Sonatype, and then do some more
> > exploring on your own.
> >
> > Property substitution happens "automatically" by Maven at
> > compile/package time (its static) -- nothing happens at runtime (not
> > dynamic) -- so you do run into the issue of "this Jar was packaged for
> > DEV, this one for PROD" which many people dislike.
> >
> > So keep playing and reading with it, and bounce back over here with
> > more questions as you have them.
> >
> > Wayne
> >
> > On 7/13/07, Ryan Moquin <fr...@gmail.com> wrote:
> > > Hi Tim,
> > >
> > > Thanks for your response... I was having trouble getting the first
> > scenario
> > > to work, but that's because I took an approach of specifying two
> > > configurations in my persistence.xml and then using a spring object to
> > > dictate which one to load based on passing in a flag into my constructor
> > in
> > > my tests.  This has worked fine, other than the problem I've mentioned
> > > below.  When I separated them into two different files, I started
> > getting a
> > > lot of complaining from hibernate.  I think because I must have parts in
> > my
> > > tests that aren't using the mocked out object and therefore causing
> > > hibernate to try to load the one that is nonexistent...  All our other
> > > projects use the database directly instead of mocking out the database
> > > connection because I haven't found an acceptable way to do it.. just
> > easier
> > > to let everything modify the test database.
> > >
> > > I find your second solution interesting.. and I think maybe a bit
> > cleaner
> > > ... it's actually what I was hoping someone would point me to.  My
> > confusion
> > > is still the same though, how would maven know to load the file and
> > > substitute the property?  And if it's hibernate that would do it, then
> > how
> > > would it get it at runtime?  Would I always have to pass in a system
> > > property?  I guess I should look up properties and the persistence.xml...
> > > maybe there is something I'm just not understanding.
> > >
> > > Ryan
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > For additional commands, e-mail: users-help@maven.apache.org
> >
> >
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: questions about using different config files, such as persistence.xml in maven configs

Posted by Ryan Moquin <fr...@gmail.com>.
Yeah that's what I was saying.. I'm going to do a little more research on
the profiles... yeah that's what I was hoping not to do and wasn't sure if
that's what the other solution was getting at... I'll play around and find
something I like...

On 7/13/07, Wayne Fay <wa...@gmail.com> wrote:
>
> You should read more about Profiles on the Maven website and in the
> free PDF books from Mergere and Sonatype, and then do some more
> exploring on your own.
>
> Property substitution happens "automatically" by Maven at
> compile/package time (its static) -- nothing happens at runtime (not
> dynamic) -- so you do run into the issue of "this Jar was packaged for
> DEV, this one for PROD" which many people dislike.
>
> So keep playing and reading with it, and bounce back over here with
> more questions as you have them.
>
> Wayne
>
> On 7/13/07, Ryan Moquin <fr...@gmail.com> wrote:
> > Hi Tim,
> >
> > Thanks for your response... I was having trouble getting the first
> scenario
> > to work, but that's because I took an approach of specifying two
> > configurations in my persistence.xml and then using a spring object to
> > dictate which one to load based on passing in a flag into my constructor
> in
> > my tests.  This has worked fine, other than the problem I've mentioned
> > below.  When I separated them into two different files, I started
> getting a
> > lot of complaining from hibernate.  I think because I must have parts in
> my
> > tests that aren't using the mocked out object and therefore causing
> > hibernate to try to load the one that is nonexistent...  All our other
> > projects use the database directly instead of mocking out the database
> > connection because I haven't found an acceptable way to do it.. just
> easier
> > to let everything modify the test database.
> >
> > I find your second solution interesting.. and I think maybe a bit
> cleaner
> > ... it's actually what I was hoping someone would point me to.  My
> confusion
> > is still the same though, how would maven know to load the file and
> > substitute the property?  And if it's hibernate that would do it, then
> how
> > would it get it at runtime?  Would I always have to pass in a system
> > property?  I guess I should look up properties and the persistence.xml...
> > maybe there is something I'm just not understanding.
> >
> > Ryan
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: questions about using different config files, such as persistence.xml in maven configs

Posted by Wayne Fay <wa...@gmail.com>.
You should read more about Profiles on the Maven website and in the
free PDF books from Mergere and Sonatype, and then do some more
exploring on your own.

Property substitution happens "automatically" by Maven at
compile/package time (its static) -- nothing happens at runtime (not
dynamic) -- so you do run into the issue of "this Jar was packaged for
DEV, this one for PROD" which many people dislike.

So keep playing and reading with it, and bounce back over here with
more questions as you have them.

Wayne

On 7/13/07, Ryan Moquin <fr...@gmail.com> wrote:
> Hi Tim,
>
> Thanks for your response... I was having trouble getting the first scenario
> to work, but that's because I took an approach of specifying two
> configurations in my persistence.xml and then using a spring object to
> dictate which one to load based on passing in a flag into my constructor in
> my tests.  This has worked fine, other than the problem I've mentioned
> below.  When I separated them into two different files, I started getting a
> lot of complaining from hibernate.  I think because I must have parts in my
> tests that aren't using the mocked out object and therefore causing
> hibernate to try to load the one that is nonexistent...  All our other
> projects use the database directly instead of mocking out the database
> connection because I haven't found an acceptable way to do it.. just easier
> to let everything modify the test database.
>
> I find your second solution interesting.. and I think maybe a bit cleaner
> ... it's actually what I was hoping someone would point me to.  My confusion
> is still the same though, how would maven know to load the file and
> substitute the property?  And if it's hibernate that would do it, then how
> would it get it at runtime?  Would I always have to pass in a system
> property?  I guess I should look up properties and the persistence.xml ...
> maybe there is something I'm just not understanding.
>
> Ryan
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: questions about using different config files, such as persistence.xml in maven configs

Posted by Ryan Moquin <fr...@gmail.com>.
Hi Tim,

Thanks for your response... I was having trouble getting the first scenario
to work, but that's because I took an approach of specifying two
configurations in my persistence.xml and then using a spring object to
dictate which one to load based on passing in a flag into my constructor in
my tests.  This has worked fine, other than the problem I've mentioned
below.  When I separated them into two different files, I started getting a
lot of complaining from hibernate.  I think because I must have parts in my
tests that aren't using the mocked out object and therefore causing
hibernate to try to load the one that is nonexistent...  All our other
projects use the database directly instead of mocking out the database
connection because I haven't found an acceptable way to do it.. just easier
to let everything modify the test database.

I find your second solution interesting.. and I think maybe a bit cleaner
... it's actually what I was hoping someone would point me to.  My confusion
is still the same though, how would maven know to load the file and
substitute the property?  And if it's hibernate that would do it, then how
would it get it at runtime?  Would I always have to pass in a system
property?  I guess I should look up properties and the persistence.xml ...
maybe there is something I'm just not understanding.

Ryan

On 7/13/07, Tim Kettler <ti...@udo.edu> wrote:
>
> Hi,
>
> you have a few options based on your concrete scenario:
>
> If you just want to unit test a ejb project you can create two
> 'persistence.xml' files one in 'src/main/resources' for production use
> and one in 'src/test/resources' for testing. The testing persistence.xml
> should then shadow the production one.
>
> An other option would be to enable filtering on the resource dir:
>
>    <build>
>      <resources>
>        <resource>
>          <directory>src/main/resources</directory>
>          <filtering>true</filtering>
>        </resource>
>      </resources>
>    </build>
>
> and have profiles for the different environments:
>
>    <profiles>
>      <profile>
>        <id>test</id>
>        <activation>
>          <activeByDefault>true</activeByDefault/>
>        </activation>
>        <properties>
>          <connection>testconnection</nconnection>
>        </properties>
>      </profile>
>      <profile>
>        <id>production</id>
>        <properties>
>          <connection>prodconnection</nconnection>
>        </properties>
>      </profile>
>    </profiles>
>
> And put ${connection} in the persistence.xml.
>
> Hope this helps
> -Tim
>
>
> Ryan Moquin schrieb:
> > Hi, I know that in order to use different configuration files (such as a
> > persistence.xml), such as test configurations vs. production
> configurations
> > I need to setup profiles to allow these different files to be used.
> > What is
> > the preferred way to indicate to maven how to find a configuration file
> > that
> > should be used?  Such as, I have a persistence.xml file, currently I
> > have my
> > test database definition in it as a separate entry from my production
> > configuration.  This works fine in theory because I can mock out my
> > persistence classes to use the test configuration for tests.  The
> > problem is
> > that hibernate loads BOTH definitions when it starts up, whether it's
> used
> > or not which causes problems with hot deploying, when my production
> > environment is running (since my test config is loaded in production,
> > and my
> > tests are trying to use that configuration).  I'm also running into
> > problems
> > of needing persistence configurations that can use other database
> servers
> > other than assuming localhost.
> >
> > Does anyone have a strategy or know of a link that I can look at that
> will
> > help me implement an appropriate strategy to use/bundle differnet
> > persistence.xml files based upon the loaded maven profile?  Thanks!
> >
> > Ryan
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: questions about using different config files, such as persistence.xml in maven configs

Posted by Tim Kettler <ti...@udo.edu>.
Hi,

you have a few options based on your concrete scenario:

If you just want to unit test a ejb project you can create two 
'persistence.xml' files one in 'src/main/resources' for production use 
and one in 'src/test/resources' for testing. The testing persistence.xml 
should then shadow the production one.

An other option would be to enable filtering on the resource dir:

   <build>
     <resources>
       <resource>
         <directory>src/main/resources</directory>
         <filtering>true</filtering>
       </resource>
     </resources>
   </build>

and have profiles for the different environments:

   <profiles>
     <profile>
       <id>test</id>
       <activation>
         <activeByDefault>true</activeByDefault/>
       </activation>
       <properties>
         <connection>testconnection</nconnection>
       </properties>
     </profile>
     <profile>
       <id>production</id>
       <properties>
         <connection>prodconnection</nconnection>
       </properties>
     </profile>
   </profiles>

And put ${connection} in the persistence.xml.

Hope this helps
-Tim


Ryan Moquin schrieb:
> Hi, I know that in order to use different configuration files (such as a
> persistence.xml), such as test configurations vs. production configurations
> I need to setup profiles to allow these different files to be used.  
> What is
> the preferred way to indicate to maven how to find a configuration file 
> that
> should be used?  Such as, I have a persistence.xml file, currently I 
> have my
> test database definition in it as a separate entry from my production
> configuration.  This works fine in theory because I can mock out my
> persistence classes to use the test configuration for tests.  The 
> problem is
> that hibernate loads BOTH definitions when it starts up, whether it's used
> or not which causes problems with hot deploying, when my production
> environment is running (since my test config is loaded in production, 
> and my
> tests are trying to use that configuration).  I'm also running into 
> problems
> of needing persistence configurations that can use other database servers
> other than assuming localhost.
> 
> Does anyone have a strategy or know of a link that I can look at that will
> help me implement an appropriate strategy to use/bundle differnet
> persistence.xml files based upon the loaded maven profile?  Thanks!
> 
> Ryan
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org