TomEE embedded is great but until now it needed a real application to deploy to at least get web features (was possible to deploy ejb modules from classpath but then you don’t get Servlet/WebSocket/… features).
Now you can do it simply calling deployClasspathAsWebApp() method!
Idea is simple: consider your classpath as a webapp. Once this is done you can:
- Use @WebServlet, @WebFilter, @WebListener to register your web components (Servlet, Filter, ServletContextListener, HttpSessionListener…)
- Use a ServletContainerInitializer to add programmatically web components
- Use EJBs
- Use CDI
- Use JSP or any web resources (html, js, css…)…yes, you read correctly! Just put them in META-INF/resources
- Use JAX-RS
- Use WebSocket
- Surely much more 😉
That’s one of the strengh of JavaEE: all is declarative so no need of redeclaring everything.
One demo can be found in TomEE source tree (a bit more complicated than your own code cause of its location in TomEE build): http://svn.apache.org/repos/asf/tomee/tomee/trunk/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/ClasspathAsWebappTest.java
If you want to use it just import as dependency org.apache.openejb:tomee-embedded:${tomee.snapshot} and use something like:
try (final Container container = new Container(new Configuration()).deployClasspathAsWebApp()) { // wait end of your program }
Not that a main is also provided: org.apache.tomee.embedded.Main. Just use the new “-as-war” option.