How do I package my web app and tomcat together using maven?

穿精又带淫゛_ 提交于 2019-12-03 15:17:16

Elaborating Tomasz's comment, you can do the following to achieve this.

  1. Download and install tomcat to your local repository.

    mvn install:install-file -DgroupId=org.apache -DartifactId=tomcat -Dversion=7.0.10 -Dpackaging=zip -Dfile=/path/to/file

  2. Use unpack goal of maven dependency plugin to unzip tomcat to a work folder

  3. Use maven assembly plugin to place the application war in webapps folder and create a zip

You can refer to this pom.xml and this assembly descriptor.

A better way could be something as specified in the Heroku documentation (Though it should work on non-heroku apps as well)

To summarize (Just in case the link dies)

Tomcat embed package can give you a Tomcat API which you can refer in one of your main class,

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>${tomcat.version}</version>
</dependency>

And you would need a main class something like,

package launch;

import java.io.File;
import org.apache.catalina.startup.Tomcat;

public class Main {

    public static void main(String[] args) throws Exception {

        String webappDirLocation = "src/main/webapp/";
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);

        tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
        System.out.println("configuring app with basedir: " + 
            new File("./" + webappDirLocation).getAbsolutePath());

        tomcat.start();
        tomcat.getServer().await();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!