0

I am trying to migrate a Jersey Tomcat project into Embedded Tomcat with the Shadow Plugin to simplify deployment.

I based my changes on this tutorial . It is for Spring, so I skipped the Spring specific parts.

The main changes are the addition to the Embedded Tomcat and Shadow Plugin dependencies to build.gradle.kts ...

plugins {
    id("java")
    id("maven-publish")
    id("application")
    id("com.github.johnrengelman.shadow") version "7.1.0"
}

dependencies {
    .
    .
    .
    implementation("org.apache.tomcat.embed:tomcat-embed-jasper:10.0.12")
}

... and the creation of the Main class as follows:

import org.apache.catalina.startup.Tomcat;

import java.io.File;
import java.io.IOException;

public class Main {
    private static final int PORT = 8080;

    public static void main(String[] args) throws Exception {
        String appBase = ".";
        Tomcat tomcat = new Tomcat();
        tomcat.setBaseDir(createTempDir());
        tomcat.setPort(PORT);
        tomcat.getHost().setAppBase(appBase);
        tomcat.addWebapp("", appBase);
        tomcat.start();
        tomcat.getServer().await();
    }

    // based on AbstractEmbeddedServletContainerFactory
    private static String createTempDir() {
        try {
            File tempDir = File.createTempFile("tomcat.", "." + PORT);
            tempDir.delete();
            tempDir.mkdir();
            tempDir.deleteOnExit();
            return tempDir.getAbsolutePath();
        } catch (IOException ex) {
            throw new RuntimeException(
                    "Unable to create tempDir. java.io.tmpdir is set to " + System.getProperty("java.io.tmpdir"),
                    ex
            );
        }
    }
}

When I try to run or debug this either from the cli or from IntelliJ I get no build errors and IntelliJ reports that the app is running. However, I can't reach any of the endpoints of the server.

I do get this warning:

Dec 08, 2021 6:57:15 PM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
INFO: No global web.xml found

The project does have a global web.xml located in src/main/webapp/WEB-INF/web.xml, but it doesn't seem to be recognized.

It seems like Shadow is not loading my code into the Tomcat server. Is there something missing to link Shadow Plugin > Embedded Tomcat > My Jersey project?

buzoherbert
  • 1,537
  • 1
  • 12
  • 34
  • Unless you are using Jersey 3.x, you need to use Tomcat 9.0 (cf. [this question](https://stackoverflow.com/q/66806582/11748454)). Moreover your `web.xml` will neither be packaged nor read by Tomcat. You need a `web.xml`-less Jersey project. – Piotr P. Karwasz Dec 09 '21 at 03:59
  • Do you have many resources in `src/main/webapp`? – Piotr P. Karwasz Dec 09 '21 at 04:10

0 Answers0