IntroductionAs you may already know, creating a war archive and deploying it on a preinstalled Servlet Container is not the only way running a Java webapp, since you can also embed the Web-Server in the application itself. Sometimes such artifacts are delivered as standalone executable programs that can be easily started and stopped in the commandline and even run as a service in the background. Maybe you should also consider producing your app this way and benefit from its huge advantages:
- No preinstalled Webserver/Servlet Container required that would take additional maintenance.
- Easy Cross-Platform. Same artifact can of course be run on any System.
- Complete Standalone Software
Embedded TomcatIn my tutorial ill use an embedded Tomcat as a Servlet Container. So we want to add the Maven dependencies first.
We can use them now to create a StartServer class
Exploded WarNow we need to make an exploded war in our "target" directory. I choose to create a folder inside "target" that holds the application as a whole including bin files, configuration and the webapp itself. This makes it easy to transport the complete app to another machine.
If you run mvn clean package right now you should get your webapp compiled into target/myapp/webapp
Startup ScriptsFinally we need startup Scripts to actually run our application. We need a shell script for unix Systems and a bat file for Windows. The Appassembler maven plugin comes in handy here. It is a very thoughtout plugin that creates exactly what we need and even sets the basedir Systemproperty. I'll configure it to use our exploded webapp and StartServer class.
Done. Appassembler now creates executable scripts for all platforms inside the target/myapp/bin directory. You could even put configuration files into src/main/config that would be copied into target/myapp/conf automatically by the plugin.
If we run mvn clean package now we get a completely transportable standalone webapp in our target directory!