0

I need to create a JAR file for the Spring MVC (not spring boot) project with an embedded tomcat server. I am using shadow gradle plugin to create the JAR.

Everything works fine when I run the main method which launches the tomcat server and deploys my web app, the problem is when I create a JAR and try to run it.

Here is my main method

    server = new Tomcat();
    server.setBaseDir(BASE_DIR);
    server.setPort(LOCAL_PORT);
    server.getHost().setAppBase(".");
    server.addWebapp("", new File("src/main/webapp").getAbsolutePath());
    getDocBaseFile();
    server.start();
    server.getServer().await();

shadow task in build.gradle

shadowJar {
from('src/main') {
    include 'webapp/**'
}
mergeServiceFiles()
archiveBaseName.set('MyApp')
archiveVersion.set('1.0.0')
archiveClassifier.set('')
manifest {
    attributes 'Main-Class': 'com.myapp.MainClass'
}

}

my folder structure

src
  |
  main
     |
     webapp
     resources

webapp folder contains all the static files, web.xml, content.xml, and other configuration files like spring-security.xml

The problem here is the path src/main/webapp is not recognized once the project is packaged to a JAR file. I have seen many examples but none of the solutions works for me.

Any help is very appreciated.

Yaphet17
  • 123
  • 9

1 Answers1

0

Try this:

    server = new Tomcat();
    server.setBaseDir(BASE_DIR);
    server.setPort(LOCAL_PORT);
    server.getHost().setAppBase(".");
    server.addWebapp("/", new File("src/main/webapp/").getAbsolutePath());
    getDocBaseFile();
    server.start();
    server.getServer().await();