Search code examples
javahtmlgradleapache-tomee

Cannot access static html resources in a tomee-embedded application that publishes classpath as webApp


I am trying to configure a tomee-embedded in an application. All classes and html files are in the same gradle project so the tomee-embedded serves the classpath as a webApp.

I can verify that EJB and Servlets are working and so are webservices.

However it seems that the html static resources that are found in /src/webapp are not served. I cannot access for instance the index.html that is in /src/webapp/index.html, nor any of the other files and folders.

I have tried some approaches, like the ones shown below.

  • Adding the webapp folder as CustomWebResources in the Configuration.

  • Adding the webapp folder as docBase when deploying classpath as webApp.

    public final class Main {

    public static final void main(final String[] args) {
    
     final Configuration configuration=new Configuration();
    
     //Attempt one, does not work
     configuration.addCustomWebResources("webapp");
     configuration.setHttpPort(8082);
     try (final Container container = new Container(configuration)) {
    
         //Attempt two, does not work either
         final File docbase=new File("webapp");
         System.out.println("Docbase:"+docbase.getAbsolutePath());
         container.deployClasspathAsWebApp("/",docbase);
         System.out.println("Started on http://localhost:" + container.getConfiguration().getHttpPort());
    
         container.await();
     } catch (final Exception exception) {
         LOGGER.error(ExceptionUtils.getStackTrace(exception));
     }
    

    } }

For reasons of completeness the gradle import I use is the following:

  implementation 'org.apache.tomee:tomee-embedded:8.0.6'

How can I tell the tomee-embedded to also serve the index.html and the other resources under src/webapp folder?


Solution

  • I was finally able to figure this out. The following will work when running with the "run" task of the gradle application plugin.

       public final class Main {
    
       public static final void main(final String[] args) {
        
        final Configuration configuration=new Configuration();
        configuration.setHttpPort(8082);
        try (final Container container = new Container(configuration)) {
    
            //Works in gradle application:run
            final File docbase=new File("src/main/webapp");
            System.out.println("Docbase:"+docbase.getAbsolutePath());
            container.deployClasspathAsWebApp("/",docbase,true);
            System.out.println("Started on http://localhost:" + container.getConfiguration().getHttpPort());
    
            container.await();
        } catch (final Exception exception) {
            LOGGER.error(ExceptionUtils.getStackTrace(exception));
        }
        }
        }
    

    The docbase should point to the exact location of the webapp folder. This means that in an actual implementation distribution the relative src/main/webapp should be replaced by the actual webapp folder location.