I used spark web framework to create a webapp, but I don't know how to deploy this webapp. I'm sorry if this is very basic, but I'm new to spark framework and I cannot find any document that guide me how to deploy a spark webapp.:
- How to deploy a spark webapp standalone
- How to build a spark webapp (to war file or such a file) and deploy with web server (jetty or Tomcat).
Here you find information about deploy: http://sparkjava.com/documentation.html#embedded-web-server
Fist of all, set filter options for
web.xml
config:Application
class should implement of the interfacespark.servlet.SparkApplication
and have to initialize the routes in theinit()
method.This one looks like as (in Java SE 8 you can use Lambda Expression for router.):
App with this configuration works fine for tomcat and glassfish servers.
For the standalone scenario, you can just use Gradle (or Maven) to create fat (meaning has all dependencies including an embedded Jetty server), executable jar file. Here is a simple
build.gradle
file that does just that:Build the you application on the command line via
gradle build
. This will create anapp.jar
file in yourbuild/libs
folder then just run:If you want to be really up to date :) then you have to use Docker to package your JRE and application jar, thus you are not dependent on the software stack installed on the server. To do this we can use a
Dockerfile
:Build the docker image and run it, e.g.:
Of course if you want to use the Docker image on a remote web server, you need to push it to Docker Hub or a private docker repository and use
docker pull
to pull it to your server, before running it.1) Clone this repo: https://github.com/simplesteph/ec2-masterclass-sampleapp
2) Navigate to project pom.xml directory
3) mvn clean install
4) go to target folder
5) java -jar ec2-masterclass-sample-app-1.0-jar-with-dependencies.jar
6) In browser, navigate to http://localhost:4567
You will first need to create a regular Java project that can be built into a .war file (in Eclipse this would be a Dynamic Web Project)
The spark documentation at this link describes what needs to be added to your projects web.xml file. http://sparkjava.com/documentation.html#other-webserver
the param-value listed in the documentation within the filter needs to point to the class where you have defined your routes.
Additionally, all the code that was previously in main() needs to be moved to init().
Also, in order for me to deploy it to JBoss, I had to only include the spark libraries and not the Jetty libraries. Once this was done, you should be able to build the war and deploy it to your server the same way you would any other Java project.