I need a configuration free, deployable war, myapp1.war that can retrieve the configuration files from the tomcat/lib folder. As I have other web applications coexisting on the same Tomcat: myapp2.war, myapp3.war, I need this layout:
tomcat/lib/myapp1/application.properties
tomcat/lib/myapp2/application.properties
tomcat/lib/myapp3/application.properties
This way I can build the war files without any properties files inside the war and deploy on any server.
I have read the Spring documentation but it explains how to set the location when running as a jar:
java -jar myapp.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
I cannot figure out how to do this for the case of multiple coexisting war files.
I would like to know if this is possible or should I give up on Spring Boot and go back to the traditional Spring MVC applications.
With Spring 4.2 and @Annotation config and tomcat on linux serveur
First externalize it in Dev environement ( with eclipse )
instead of
src/main/resources/application-yourapp.properties
Then in your Application class don't forget to set the @PropertySource like that :
Now in eclipse add your config folder to classpath, go to "Run Configurations" of your tomcat server ( or equivalent ) and add the folder Config to User Entries
Ok that's it, your application.properties is out of the application and your project run perfectly in dev environment.
In production
Now, deploy your .war files ( or anything ) on tomcat, and put your application-yourapp.properties anyway on your production machine. ( for exemple in /opt/applyconfigfolder/application-yourapp.properties" )
Then in your tomcat ( here tomcat 7 ) open
bin\catalina.sh
You have this line
Just add the path of the folder which contains application.properties
If you have already some classpath define you can add it
I haven't try with windows but I think there is no problem
A solution could be to load application-{profile}.properties as @PropertySource annotations as this question suggests, but then the logging system wont work, as you can see in the documentation.
This means that your logging properties in application-{profiles}.properties like:
will be ignored and the logging system wont work.
To solve this I have used the SpringApplicationBuilder.properties() method to load properties at the beginning, when the application is configured. There I set the 'spring.config.location' used by Spring Boot to load all the application-{profiles}.properties:
Then I have moved the properties files from src/main/resources to src/main/resources/myapp1
In the pom.xml I have to set the scope of embedded tomcat libraries as "provided". Also, to exclude all properties files in src/main/resources/myapp1 from the final war and generate a configuration free, deployable war:
Then in Tomcat I have
Now I can generate the configuration free war and drop it into the apache-tomcat-7.0.59/webapps folder. Properties files will be resolved using the classpath, independently for each webapp: