1

I am building a jersey webapp and I am using embedded tomcat for portability. It's quite simple with a single resource. The problem I am experiencing is that when I run the webapp through the IDE (intellij), everything works fine and I see my class extending ResourceConfig being initialised. It also responds to HTTP request. It works with the IDE both using Gradle and the internal build tools

However, as soon as I build it with gradle through the command line, using the shadowJar plugin, nothing is loaded in tomcat. I see it spinning up, but none of the log lines are appearing and all my HTTP requests result in 404. I'm not sure how to debug this.

My Main class

public class Main {

    public static void main(String[] args) {
        new Main().start();
    }

    public void start() {

        String port = System.getProperty("port");
        if (port == null) {
            port = "8442";
        }

        Tomcat tomcat = new Tomcat();
        tomcat.setPort(Integer.parseInt(port));
        tomcat.getConnector();

        String absolutePath = new File(".").getAbsolutePath();
        Context context = tomcat.addWebapp("/webapp", absolutePath);

        tomcat.start();
        tomcat.getServer().await();
    }
}

My MyApplication class

@ApplicationPath("app")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        setApplicationName("My web app");
        LOG.info("Hello!")
        packages("com.xxx.xxx.xxx.resources");
    }
}
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'
buildscript {
    dependencies {
        classpath fileTree(dir: '../libs/shadow/', include: '*.jar')
    }
}
mainClassName = 'com.xxx.xxx.xxx.Main'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

dependencies {
    implementation libraries.javax_servlet
    implementation libraries.args4j
    implementation libraries.log4j2
    implementation libraries.common_loggings
    implementation libraries.jersey
    implementation libraries.tomcat

    implementation project(":common")

}


jar {
    manifest {
        attributes('Main-Class': 'com.xxx.xxx.xxx.Main')
    }

    from {
        configurations.runtimeClasspath.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}
Nick
  • 2,862
  • 5
  • 34
  • 68
  • I think you will have to create a Jersey Servlet and add it to the Tomcat container. You would create an instance of Jersey's ServletContainer passing in your ResourceConfig to the constructor. – Paul Samsotha Apr 03 '20 at 01:35
  • @PaulSamsotha I've tried that too, I can't seem to understand why in the IDE everything is fine and with gradle through CLI it is not. Any way to see how the IDE is building the project? – Nick Apr 03 '20 at 07:47
  • Have a look at the bottom code block in [this post](https://stackoverflow.com/a/37222254/2587435). Look specifically in the @Before setup method – Paul Samsotha Apr 03 '20 at 23:18

0 Answers0