making build runtime dependent on environment variable  spring mvc maven

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
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.

Author

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
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
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!
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Author

Commented:
how do you deploy the war at multiple machines ?
just want to know what process you follow.

Author

Commented:
do you use jetty or tomcat ?

Author

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 ?

Author

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial