You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by adeelmahmood <ad...@gmail.com> on 2013/03/28 15:54:50 UTC

maven profiles - load config.properties based on environment

I am trying to setup maven profiles in a way that allows me to read a
properties file based on an environment property e.g. either
config-dev.properties or config-prod.properties and then have those
propreties within the config-*.properties files available for the
application to use. I am kind of new to the maven profiles concept so I am
not sure what the best way is to set something up like this. This is how I
have things setup so far (following
http://www.petrikainulainen.net/programming/maven/running-solr-with-maven/)

-- Pom.xml
<profiles>
              <profile>
                     <id>dev</id>
                     <activation>
                           <activeByDefault>true</activeByDefault>
                     </activation>
                     <properties>
                           <build.profile.id>dev</build.profile.id>
                     </properties>
              </profile>
              <profile>
                     <id>prod</id>
                     <activation>
                           <property>
                                  <name>WASENV</name>
                                  <value>PROD</value>
                           </property>
                     </activation>
                     <properties>
                           <build.profile.id>prod</build.profile.id>
                     </properties>
              </profile>
       </profiles>

                <build>
              <finalName>solr</finalName>

              <filters>
                    
<filter>${project.basedir}/src/main/resources/config-${build.profile.id}.properties</filter>
              </filters>
              <resources>
                     <resource>
                           <filtering>true</filtering>
                           <directory>src/main/resources</directory>
                     </resource>
              </resources>

                                ……

                                <plugin>
                           <groupId>org.mortbay.jetty</groupId>
                           <artifactId>jetty-maven-plugin</artifactId>
                           <version>8.1.8.v20121106</version>
                           <configuration>
                                  
                                 
<systemPropertiesFile>${project.basedir}/src/main/resources/config-${build.profile.id}.properties</systemPropertiesFile>
                                  <stopPort>9966</stopPort>
                                  <stopKey>stop</stopKey>
                                  <connectors>
                                         
                                         <connector
implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                                                <port>8983</port>
                                               
<maxIdleTime>60000</maxIdleTime>
                                         </connector>
                                  </connectors>
                                  <webApp>
                                         <contextPath>/solr</contextPath>
                                  </webApp>
                           </configuration>
                     </plugin>
              </plugins>
       </build>

So first thing I have defined two profiles (dev and prod). Dev is active by
default and prod is activated based on WASENV environment variable/system
property/jndi entry (I am hoping). Then in the build section I am using
maven filtering to read the correct config.properties based on the activated
profile id. Also in the jetty configuration I am reading system properties
from the config.properties based on activated profile id. This works fine if
I run the app with jetty. If I run with jetty:run –P dev or –P prod, it
loads the correct profile and the correct config.properties file and
everything works.

However when I package it into a war file and deploy on tomcat, nothing
really happens (neither config.properties file is loaded). The fact is I am
not even sure how its supposed to work because maven is a build tool so if
the war is already built how can any of this activation of correct profile
will happen in tomcat based on environment variable. This is where I think I
am not sure how deployment based on profiles is supposed to work and can use
some help in understanding this better.

Any help would be appreciated.

Thanks
Adeel




--
View this message in context: http://maven.40175.n5.nabble.com/maven-profiles-load-config-properties-based-on-environment-tp5752105.html
Sent from the Maven - Users mailing list archive at Nabble.com.

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


Re: maven profiles - load config.properties based on environment

Posted by David Karr <da...@gmail.com>.
As Stephen states, and you've realized intuitively, this is not what Maven
is for.

A simple way to do what you're trying to do is simply to package all of the
config files for each environment in your artifact, although perhaps
without the production one (which typically has production credentials).
Design your application to expect a system property that defines what
environment it is running in, like "env", with possible values of "dev",
"test", and "prod" (many other variations are possible). Make your
application load "config-${env}.properties".  For production, configure
Tomcat so your application will have an additional pre-classpath entry that
is controllable by your production deployment team, and place the modified
version of "config-prod.properties" in that directory.


On Thu, Mar 28, 2013 at 8:08 AM, Stephen Connolly <
stephen.alan.connolly@gmail.com> wrote:

> Most likely you are trying to do something that most Maven users would
> consider to be an anti-pattern.
>
> Please have a read of the following:
>
>
> http://stackoverflow.com/questions/14725197/reading-properties-file-from-pom-file-in-maven/14727072#14727072
>
>
> http://stackoverflow.com/questions/14650468/whats-a-practicable-way-for-automated-configuration-versioning-and-deployment/14661186#14661186
>
> The first may steer you away from some property related things that people
> think they should be able to do, though it may not be exactly your context.
> The second is more general, but is actually very relevant to what you are
> trying to do.
>
> If nether of those seem to address your problem, then you might have to
> seek assistance from other than me as I tend towards grumpy this late in my
> afternoon ;-)
>
> -Stephen
>
>
>
> On 28 March 2013 14:54, adeelmahmood <ad...@gmail.com> wrote:
>
> > I am trying to setup maven profiles in a way that allows me to read a
> > properties file based on an environment property e.g. either
> > config-dev.properties or config-prod.properties and then have those
> > propreties within the config-*.properties files available for the
> > application to use. I am kind of new to the maven profiles concept so I
> am
> > not sure what the best way is to set something up like this. This is how
> I
> > have things setup so far (following
> >
> http://www.petrikainulainen.net/programming/maven/running-solr-with-maven/
> > )
> >
> > -- Pom.xml
> > <profiles>
> >               <profile>
> >                      <id>dev</id>
> >                      <activation>
> >                            <activeByDefault>true</activeByDefault>
> >                      </activation>
> >                      <properties>
> >                            <build.profile.id>dev</build.profile.id>
> >                      </properties>
> >               </profile>
> >               <profile>
> >                      <id>prod</id>
> >                      <activation>
> >                            <property>
> >                                   <name>WASENV</name>
> >                                   <value>PROD</value>
> >                            </property>
> >                      </activation>
> >                      <properties>
> >                            <build.profile.id>prod</build.profile.id>
> >                      </properties>
> >               </profile>
> >        </profiles>
> >
> >                 <build>
> >               <finalName>solr</finalName>
> >
> >               <filters>
> >
> > <filter>${project.basedir}/src/main/resources/config-${build.profile.id
> > }.properties</filter>
> >               </filters>
> >               <resources>
> >                      <resource>
> >                            <filtering>true</filtering>
> >                            <directory>src/main/resources</directory>
> >                      </resource>
> >               </resources>
> >
> >                                 ……
> >
> >                                 <plugin>
> >                            <groupId>org.mortbay.jetty</groupId>
> >                            <artifactId>jetty-maven-plugin</artifactId>
> >                            <version>8.1.8.v20121106</version>
> >                            <configuration>
> >
> >
> > <systemPropertiesFile>${project.basedir}/src/main/resources/config-${
> > build.profile.id}.properties</systemPropertiesFile>
> >                                   <stopPort>9966</stopPort>
> >                                   <stopKey>stop</stopKey>
> >                                   <connectors>
> >
> >                                          <connector
> > implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
> >                                                 <port>8983</port>
> >
> > <maxIdleTime>60000</maxIdleTime>
> >                                          </connector>
> >                                   </connectors>
> >                                   <webApp>
> >                                          <contextPath>/solr</contextPath>
> >                                   </webApp>
> >                            </configuration>
> >                      </plugin>
> >               </plugins>
> >        </build>
> >
> > So first thing I have defined two profiles (dev and prod). Dev is active
> by
> > default and prod is activated based on WASENV environment variable/system
> > property/jndi entry (I am hoping). Then in the build section I am using
> > maven filtering to read the correct config.properties based on the
> > activated
> > profile id. Also in the jetty configuration I am reading system
> properties
> > from the config.properties based on activated profile id. This works fine
> > if
> > I run the app with jetty. If I run with jetty:run –P dev or –P prod, it
> > loads the correct profile and the correct config.properties file and
> > everything works.
> >
> > However when I package it into a war file and deploy on tomcat, nothing
> > really happens (neither config.properties file is loaded). The fact is I
> am
> > not even sure how its supposed to work because maven is a build tool so
> if
> > the war is already built how can any of this activation of correct
> profile
> > will happen in tomcat based on environment variable. This is where I
> think
> > I
> > am not sure how deployment based on profiles is supposed to work and can
> > use
> > some help in understanding this better.
> >
> > Any help would be appreciated.
> >
> > Thanks
> > Adeel
> >
> >
> >
> >
> > --
> > View this message in context:
> >
> http://maven.40175.n5.nabble.com/maven-profiles-load-config-properties-based-on-environment-tp5752105.html
> > Sent from the Maven - Users mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > For additional commands, e-mail: users-help@maven.apache.org
> >
> >
>

Re: maven profiles - load config.properties based on environment

Posted by Stephen Connolly <st...@gmail.com>.
Most likely you are trying to do something that most Maven users would
consider to be an anti-pattern.

Please have a read of the following:

http://stackoverflow.com/questions/14725197/reading-properties-file-from-pom-file-in-maven/14727072#14727072

http://stackoverflow.com/questions/14650468/whats-a-practicable-way-for-automated-configuration-versioning-and-deployment/14661186#14661186

The first may steer you away from some property related things that people
think they should be able to do, though it may not be exactly your context.
The second is more general, but is actually very relevant to what you are
trying to do.

If nether of those seem to address your problem, then you might have to
seek assistance from other than me as I tend towards grumpy this late in my
afternoon ;-)

-Stephen



On 28 March 2013 14:54, adeelmahmood <ad...@gmail.com> wrote:

> I am trying to setup maven profiles in a way that allows me to read a
> properties file based on an environment property e.g. either
> config-dev.properties or config-prod.properties and then have those
> propreties within the config-*.properties files available for the
> application to use. I am kind of new to the maven profiles concept so I am
> not sure what the best way is to set something up like this. This is how I
> have things setup so far (following
> http://www.petrikainulainen.net/programming/maven/running-solr-with-maven/
> )
>
> -- Pom.xml
> <profiles>
>               <profile>
>                      <id>dev</id>
>                      <activation>
>                            <activeByDefault>true</activeByDefault>
>                      </activation>
>                      <properties>
>                            <build.profile.id>dev</build.profile.id>
>                      </properties>
>               </profile>
>               <profile>
>                      <id>prod</id>
>                      <activation>
>                            <property>
>                                   <name>WASENV</name>
>                                   <value>PROD</value>
>                            </property>
>                      </activation>
>                      <properties>
>                            <build.profile.id>prod</build.profile.id>
>                      </properties>
>               </profile>
>        </profiles>
>
>                 <build>
>               <finalName>solr</finalName>
>
>               <filters>
>
> <filter>${project.basedir}/src/main/resources/config-${build.profile.id
> }.properties</filter>
>               </filters>
>               <resources>
>                      <resource>
>                            <filtering>true</filtering>
>                            <directory>src/main/resources</directory>
>                      </resource>
>               </resources>
>
>                                 ……
>
>                                 <plugin>
>                            <groupId>org.mortbay.jetty</groupId>
>                            <artifactId>jetty-maven-plugin</artifactId>
>                            <version>8.1.8.v20121106</version>
>                            <configuration>
>
>
> <systemPropertiesFile>${project.basedir}/src/main/resources/config-${
> build.profile.id}.properties</systemPropertiesFile>
>                                   <stopPort>9966</stopPort>
>                                   <stopKey>stop</stopKey>
>                                   <connectors>
>
>                                          <connector
> implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
>                                                 <port>8983</port>
>
> <maxIdleTime>60000</maxIdleTime>
>                                          </connector>
>                                   </connectors>
>                                   <webApp>
>                                          <contextPath>/solr</contextPath>
>                                   </webApp>
>                            </configuration>
>                      </plugin>
>               </plugins>
>        </build>
>
> So first thing I have defined two profiles (dev and prod). Dev is active by
> default and prod is activated based on WASENV environment variable/system
> property/jndi entry (I am hoping). Then in the build section I am using
> maven filtering to read the correct config.properties based on the
> activated
> profile id. Also in the jetty configuration I am reading system properties
> from the config.properties based on activated profile id. This works fine
> if
> I run the app with jetty. If I run with jetty:run –P dev or –P prod, it
> loads the correct profile and the correct config.properties file and
> everything works.
>
> However when I package it into a war file and deploy on tomcat, nothing
> really happens (neither config.properties file is loaded). The fact is I am
> not even sure how its supposed to work because maven is a build tool so if
> the war is already built how can any of this activation of correct profile
> will happen in tomcat based on environment variable. This is where I think
> I
> am not sure how deployment based on profiles is supposed to work and can
> use
> some help in understanding this better.
>
> Any help would be appreciated.
>
> Thanks
> Adeel
>
>
>
>
> --
> View this message in context:
> http://maven.40175.n5.nabble.com/maven-profiles-load-config-properties-based-on-environment-tp5752105.html
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>