Skip to content

Instantly share code, notes, and snippets.

@arjunKumbakkara
Last active September 8, 2021 11:09
Show Gist options
  • Save arjunKumbakkara/af15161e8a37f753743836d63da965f4 to your computer and use it in GitHub Desktop.
Save arjunKumbakkara/af15161e8a37f753743836d63da965f4 to your computer and use it in GitHub Desktop.
DEPLOYING A COMPRESSED SPRING BOOT APPLICATION(WAR) AS COMPARED TO AN EXPLODED APP PATCH
IN A TOMCAT SERVER WITH EXTERNALIZED PROPERTIES
Author:Arjun Kumbakkara (arjunkumbakkara@gmail.com)|https://arjunkumbakkara.github.io
First of all : SPRING BOOT CONVERSION
1. Convert your Spring boot application to Tomcat compatible (For SpringBoot2.x its easier for 1.x make sure you do the below)
a. remove the embedded Tomcat. Note: When using embedded tomcat , server.context=/api from application.properties wont work!
b.Add the packaging as war or jar
c.In the Application entry point make sure 'SpringBootServletInitializer'
(These conversion steps can be found easily. Not a biggie!)
Now, TOMCAT CONTEXT
Changing the Tomcat App Context to Custom Context:
The one working and flexible solution is as below:
2.Inside ${Tomcat_home}/config/server.xml
Change the autoDeploy="false" deployOnStartup="false" under Host element like below as this is a mandate.
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="false" deployOnStartup="false">
3.Add below line under above Host element.
(I understand , when we use Continous Deployment, the patch with name "bics_api_gw-4.15.1.0.war" would have to be configured seperataley. Well a script can do that! )
<Context path="/api" docBase="bics_api_gw-4.15.1.0.war" reloadable="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
With the above approach we can add as many applications under webapps with different context path names.
OtherMethods:
Context.xml in conf (Doesnt work for Tomcat8.x +)
<build>contextName</build> : this works but downside is that it takes only the app.war name ie as 'app'
For Reference:
Take a look: https://octopus.com/blog/defining-tomcat-context-paths
EXTERNALIZATION :
There are precedences that are mentioned in the Spring boot docs. That a file(application.properties) can be placed
next to the app.war/jar or in a subdirectory(/config) . None of them will work if the spring boot app is being run in the
tomcat engine using tomcat server config.
The way forward is to Override or write a custom EnvironmentAware interface based class that extends it like below.
//=============================================================================
@Configuration
//@PropertySource(value = { "classpath:${catalina.base}/webapps/application.properties" }, ignoreResourceNotFound = false)
//Remember that here we are using file: instead of classpath because for a file outside of spring context is only a file
//and not a file in its classpath.Thus its just a file!
@PropertySource(value = { "file:/${catalina.base}/webapps/application.properties" }, ignoreResourceNotFound = false)
public class ExternalConfiguration implements EnvironmentAware {
private static Environment env;
public static String getProperty(String key) {
return env.getProperty(key);
}
@Override
public void setEnvironment(Environment env) {
ExternalConfiguration.env = env;
}
}
//=============================================================================
Here Environment is extended and we override the autowireable interface EnvironmentAware.This way before the entire
spring boot context is loaded we customize it to pick the file (application.properties) from anywhere practically but from
tomcat/webapps/ in the code above.
Thats about it.
Salud.!
@arjunKumbakkara
Copy link
Author

Slight sample code change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment