Reputation:
Is there a programatic solution for adding a class path (other than the trivial WEB-INF/classes
) for servlet 3.1 annotation scanning to a Tomcat 9 embedded server? Without using any XML, WAR or WEB-INF folder.
I got this far but I don't know how to proceed without a WEB-INF or WAR artifact:
static void startServer() {
Tomcat server = new Tomcat();
server.setPort(7070);
StandardContext context = (StandardContext) server.addWebapp("", "/");
WebResourceRoot resources = new StandardRoot(context);
resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", "build/classes/main/java/", "/"));
context.setResources(resources);
server.start();
server.getServer().await();
}
This is my solution for Jetty:
static void startServer() {
final Server server = new Server(7070);
final WebAppContext context = new WebAppContext("/", "/");
context.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebInfConfiguration() });
context.setExtraClasspath("build/classes/main");
server.setHandler(context);
server.start();
server.join();
}
Upvotes: 1
Views: 692
Reputation: 22384
You are almost there, the only missing thing is that you need to provide the custom path for the lib
folder (where the application jar files reside and to be loaded into Tomcat server) as part of the DirResourceSet
constructor call, i.e., resources.addPreResources(new DirResourceSet(resources, "/your_own/lib", "build/classes/main/java/", "/"));
For more details on DirResourceSet
, look at the API here
Upvotes: 1