making build runtime dependent on environment variable spring mvc maven

Hi,
I have a spring mvc maven project. Currently in maven i have specified to read from an environment variable env and take the corresponding properties files and build the war. So the war is build with the specific environment properties beforehand and then i can deploy the war.
But what i want is that the war that is built contains all the properties for all the environments. and then when the war is ran in a server such as jetty or tomcat it picks up the correct property files based on an environment variable.
So basically it should be picked up at runTime.

Current way properties file is getting included in war :
 <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources/properties/${env.FLOCK_APPS_CONFIG}</directory>
            </resource>
            <resource>
                <directory>src/main/resources/spring</directory>
            </resource>
        </resources>

Open in new window


Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Ok, so you can get rid of the special resource handling in Maven and then you will need to show us the code/configuration that picks up the different properties files. Because the solution will depend on how the files are accessed.
Rohit BajajAuthor Commented:
Hi,
Currently i am picking up the files like :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
                    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:database.properties</value>
        </property>
    </bean>

    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="${snippet.jdbc.url}"/>
        <property name="username" value="${snippet.jdbc.username}"/>
        <property name="password" value="${snippet.jdbc.password}"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="ds"></property>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/resources/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

Open in new window


Basically currently the database.properties and log4j.properties are in the classpath.
But now to make it runtime dependent i will need to first of all configure pom.xml so everything is in the classpath and then access files under dev, prod etc
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Ok, so the difficult things here is going to be the fact that both log4j and your PropertiesPlaceholderConfigurer and initialised quite early on in the app startup and so getting them to change the location that they are configurer from is a little difficult.

For log4j, I don't think that you would have any other option than specifying a system property to Java upon startup to explicitly specify the properties file to use to load from. I don't think you would be able to get this based on an environment variable.

For the spring stuff, you may have better luck with http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.html and later versions of Spring support for "Environments", see http://spring.io/blog/2011/02/15/spring-3-1-m1-unified-property-management/ but I haven't used any of that so I might not be a lot of help there.


Personally, I'd just stick with having Maven support it at build time and be done with it. It has worked just fine for a large project I have developed at work!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Rohit BajajAuthor Commented:
how do you deploy the war at multiple machines ?
just want to know what process you follow.
Rohit BajajAuthor Commented:
do you use jetty or tomcat ?
Rohit BajajAuthor Commented:
For log4j, I don't think that you would have any other option than specifying a system property to Java upon startup to explicitly specify the properties file to use to load from. I don't think you would be able to get this based on an environment variable.
So this means that the spring application directly looks for log4j.properties file in classpath. If i keep it inside a folder it wont be able to read it ? Even if the path is static ?
suppose i want to hardcode to access log4j.properties under dev folder. can i do that ?
Rohit BajajAuthor Commented:
I just did the following :
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:properties/${FLOCK_APPS_CONFIG}/database.properties</value>
        </property>
    </bean>

Open in new window


This automatically picking up the value inside ${} from environment variables.
So i guess this is all i will need to do.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.