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)
}
}
}