问题
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.
回答1:
Elaborating Tomasz's comment, you can do the following to achieve this.
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
Use
unpack
goal ofmaven dependency plugin
to unzip tomcat to a work folder- 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.
回答2:
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();
}
}
来源:https://stackoverflow.com/questions/5554147/how-do-i-package-my-web-app-and-tomcat-together-using-maven