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.

4

2 回答 2

8

详细说明 Tomasz 的评论,您可以执行以下操作来实现此目的。

  1. 下载并安装 tomcat 到您的本地存储库。

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

  2. 使用unpack目标maven dependency plugin将 tomcat 解压缩到工作文件夹

  3. 用于maven assembly plugin将应用程序战争放在 webapps 文件夹中并创建一个 zip

您可以参考这个pom.xml 和这个程序集描述符。

于 2011-04-06T04:14:24.120 回答
4

更好的方法可能是Heroku 文档中指定的方法(尽管它也应该适用于非 Heroku 应用程序)

总结(以防万一链接失效)

Tomcat 嵌入包可以为您提供一个TomcatAPI,您可以在其中一个主类中引用该 API,

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

你需要一个主类,比如,

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();
    }
}
于 2013-11-11T11:42:49.743 回答