Avatar of Rohit Bajaj
Rohit Bajaj
Flag for India asked on

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
Java EEJava

Avatar of undefined
Last Comment
Rohit Bajaj

8/22/2022 - Mon
mccarl

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 Bajaj

ASKER
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
ASKER CERTIFIED SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rohit Bajaj

ASKER
how do you deploy the war at multiple machines ?
just want to know what process you follow.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Rohit Bajaj

ASKER
do you use jetty or tomcat ?
Rohit Bajaj

ASKER
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 Bajaj

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.