Tomcat + Spring and environment variables

12,178

Solution 1

Add system variable in Eclipse: Go to Run --> Run Configurations --> Tomcat Select Arguments tab and add to VM arguments -Denv=blabla

Solution 2

To define a variable in in the tomcat context.xml that can be used in spring add this line to the right context in context.xml.

<Parameter name="env" value="ABCDEFG"  override="false"/>

Solution 3

Tarlog solution can be solve your problem but only inside eclipse, if you remove your server definition from eclipse you lost the definition and you need to add it again each time you change your IDE or delete the server definition.

So better way create a shell or bat file(like runServer.sh/.bat) according to your environment and add this parameter to the JAVA_OPTS variable so this variable called when catalina.sh/.bat is running (startup.sh/.bat called catalina script inside it). You can use this approach at your local, test and prod environment.

Windows: runServer.bat

set JAVA_OPTS="-Dvariable=value"
tomcat/bin/startup.bat

Linux: runServer.sh

export JAVA_OPTS="-Dvariable=value"
tomcat/bin/startup.sh
Share:
12,178
Randomize
Author by

Randomize

Just a random developer that does NOT believe in software development and people

Updated on June 13, 2022

Comments

  • Randomize
    Randomize almost 2 years

    In a Spring file I have:

       <bean id="propertyConfigurer" class="org.myapp.MyConfigurator">
            <property name="locations">
            <list>
                           <value>classpath:configuration-${env}.properties</value>
            </list>
        </property>
    </bean>
    

    the ${env} variable is defined in maven's profile. But when I run from eclipse the application in tomcat 6 (published) it doesn't look in maven. So how can I set the variable for Tomcat?

    Thanks

    Randomize