Does anyone know how to set up the context in an embedded Tomcat instance to serve files from a local directory not within the deployed folder?
In the standard server.xml it looks something like this:
<Context docBase="/MyWebApp/images" path="/tmp/images/" reloadable="false"/>
I have tried different variations on the following tomcat embedded call without success:
tomcat.addContext(tomcat.getHost(), "/MyWebApp/images", "/tmp/images/");
I also tried:
tomcat.addContext("/MyWebApp/images", "/tmp/images/");
It looks like this grails question was along the same lines:
grails: add context to embedded tomcat in development
But im not using grails. (Using Java)
Here is the full startup code I am using:
package launch;
import java.io.File;
import org.apache.catalina.startup.Tomcat;
public class Main {
public static void main(String[] args) throws Exception {
String webappDirLocation = "/src/main/webapp/";
Tomcat tomcat = new Tomcat();
//The port that we should run on can be set into an environment variable
//Look for that variable and default to 8080 if it isn't there.
String webPort = System.getenv("PORT");
if(webPort == null || webPort.isEmpty()) {
webPort = "8080";
}
tomcat.setPort(Integer.valueOf(webPort));
tomcat.addWebapp("/MyWebApp", new File(webappDirLocation).getAbsolutePath());
tomcat.addContext("/MyWebApp/images", "/tmp/images/");
tomcat.start();
tomcat.getServer().await();
}
}