5

I would like to distribute my application packaged as WAR embedded in Apache Tomcat. That is I want to distribute Tomcat along with my application.

How can this sort of distribution packaging can be done with Maven?

I have seen the Maven Cargo Plugin, but it appears to be geared towards deploying applications in containers locally. Perhaps an additional step over Cargo plugin is what I need. cargo:package appears to be interesting but lacks documentation.

Tahir Akhtar
  • 11,385
  • 7
  • 42
  • 69
  • 1
    Have you seen `maven assembly plugin`? http://docs.codehaus.org/display/MAVENUSER/Delivery+with+Tomcat – Tomasz Nurkiewicz Apr 05 '11 at 15:21
  • No, checking now. Thanks for the link – Tahir Akhtar Apr 05 '11 at 15:24
  • 2
    Keep in mind that packaging your application along with Tomcat will likely mean your users will not be using the latest version of Tomcat. Tomcat requires Java (JRE) to be installed as well. Might as well include a JRE if you are including Tomcat. – Bernard Apr 05 '11 at 16:41
  • @Tomasz: Do you know what does "stand-alone-module" means in the article you mentioned? – Tahir Akhtar Apr 05 '11 at 20:54
  • 1
    It means "*separate from your other modules*", just somewhere along them. Looks like the authors of this article are aggregating separate Tomcat JARs as dependencies of a single projects that builds Tomcat distro. You might make it a bit simpler and just ZIP Tomcat manually and install it in your local repository, referencing it later in assembly plugin as ZIP. – Tomasz Nurkiewicz Apr 05 '11 at 21:43
  • OK, so it means not related to other modules via parent-child relationship. Thanks for clearing that up – Tahir Akhtar Apr 06 '11 at 13:40

2 Answers2

8

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.

Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • Thanks Raghuram. Although I haven't tried it yet, looking at the links you provided, it seems maven dependency plugin is the way to go – Tahir Akhtar Apr 06 '11 at 15:14
  • Is there a better way if the project is on github? (instead of asking contributors to download and install tomcat) – Madhusoodan P May 24 '20 at 11:38
4

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();
    }
}
anoopelias
  • 9,240
  • 7
  • 26
  • 39