That’s an old trick, but it helped me today, and I’m pretty sure I’ll use it again.
You can setup some System properties with Spring.
Mix that with Maven resources filtering and you can execute your application with some specific System properties according to your profile.
In your applicationContext.xml, add:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System"/>
<property name="targetMethod" value="setProperty"/>
<property name="arguments">
<list>
<value>logsDir</value>
<value>${logs.dir}</value>
</list>
</property>
</bean>
Here ${logs.dir} is replaced before the execution of the application. During the build phase, maven will look at the properties you’ll have defined in your profile, and use them to filter the files you will have set (in this example, we set maven to filter all files in src/main/resources, where applicationContext.xml is located.
For instance, we can define a property: logs.dir=/mydevmachine/logs for a dev profile, and logs.dir=/myprodmachine/logs for a prod profile.
Then in your application, doing a System.getProperty(“logsDir”) will give you the correct value according to your maven profile.
No need to add System properties to MAVEN_OPTS anymore!
Minor correction, this should be read as:
logsDir
${logs.dir}
Apparently XML is not allowed in comments, see this gist instead https://gist.github.com/1466100
Thanks for the correction (I corrected my post, I don’t know how I messed up the copy/paste from my application code)
Yeah WordPress is a bit tedious, you have to use the tag: <pre> around your code to keep your tags
Maybe like this?