SparkJava Maven Archetype with .war generation
Spark Java is a Java 8 based lightweight framework which I highly recommend for writing APIs. It is so simple that it comes with an embedded jetty. However, in real world production you’d want to run it in an external container like Tomcat, JBoss, or Jetty (external), etc. I found a SparkJava Hello World archetype, however, it uses the embedded jetty. I was able to fork the repo and add .war support to it.
The fork is here
This post walks through the usage.
Clone the fork
C02STG51GTFM:work mpandit$ git clone https://github.com/marqeta/sparkjava-archetypes.git
Cloning into 'sparkjava-archetypes'...
remote: Counting objects: 115, done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 115 (delta 3), reused 0 (delta 0), pack-reused 101
Receiving objects: 100% (115/115), 13.20 KiB | 0 bytes/s, done.
Resolving deltas: 100% (18/18), done.
Install the archetype in your local .m2
C02STG51GTFM:work mpandit$ cd sparkjava-archetypes/
C02STG51GTFM:sparkjava-archetypes mpandit$ cd sparkjava-helloworld-archetype/
C02STG51GTFM:sparkjava-helloworld-archetype mpandit$ mvn install
INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Spark Java Hello World Archetype 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] ....
[INFO] ....
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.029 s
[INFO] Finished at: 2017-04-06T20:47:41-07:00
[INFO] Final Memory: 13M/245M
[INFO] ------------------------------------------------------------------------
Create the HelloWorld project using this archetype
You may want to replace the groupId
, artifactId
, and package
to match your code structure.
C02STG51GTFM:sparkjava-helloworld-archetype mpandit$ cd ~/work
C02STG51GTFM:work mpandit$ mvn archetype:generate \
> -DarchetypeGroupId=com.sparkjava \
> -DarchetypeArtifactId=sparkjava-helloworld-archetype \
> -DarchetypeVersion=1.0.0 \
> -DgroupId=com.marqeta.mqpay \
> -DartifactId=webhooks-api \
> -Dversion=1.0-SNAPSHOT \
> -Dpackage=com.marqeta.mqpay
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Spark Java Hello World Archetype 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) > generate-sources @ sparkjava-helloworld-archetype >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) < generate-sources @ sparkjava-helloworld-archetype <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ sparkjava-helloworld-archetype ---
[INFO] Generating project in Interactive mode
[INFO] Archetype repository missing. Using the one from [com.sparkjava:sparkjava-helloworld-archetype:1.0.0] found in catalog local
[INFO] Using property: groupId = com.marqeta.mqpay
[INFO] Using property: artifactId = webhooks-api
[INFO] Using property: version = 1.0-SNAPSHOT
[INFO] Using property: package = com.marqeta.mqpay
Confirm properties configuration:
groupId: com.marqeta.mqpay
artifactId: webhooks-api
version: 1.0-SNAPSHOT
package: com.marqeta.mqpay
Y: :
Hit enter, and you will notice a webhooks-api folder with the project in it.
You can notice the generated files - Most importantly the Spark Application and web.xml.
C02STG51GTFM:work mpandit$ cd webhooks-api
C02STG51GTFM:webhooks-api mpandit$ tree
.
├── README.md
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── marqeta
│ └── mqpay
│ └── SparkJavaHelloWorld.java
└── webapp
└── WEB-INF
└── web.xml
8 directories, 4 files
Build the Project
C02STG51GTFM:webhooks-api mpandit$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building webhooks-api 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ webhooks-api ---
[INFO] ..
[INFO] ..
[INFO] ..
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.140 s
[INFO] Finished at: 2017-04-06T20:52:07-07:00
[INFO] Final Memory: 21M/308M
[INFO] ------------------------------------------------------------------------
Deploy the generated war file
I have tomcat under ~/Downloads/tomcat
. Replace it with your destination Servlet container’s webapp folder.
C02STG51GTFM:webhooks-api mpandit$ cp target/webhooks-api-1.0-SNAPSHOT.war ~/Downloads/tomcat/webapps/
Verify if it all worked
We can do a simple curl, or browser to hit localhost.
C02STG51GTFM:webhooks-api mpandit$ curl -i http://localhost:8080/webhooks-api-1.0-SNAPSHOT/hello
HTTP/1.1 200
Content-Type: text/html;charset=utf-8
Transfer-Encoding: chunked
Date: Fri, 07 Apr 2017 04:17:26 GMT
Hello World!
You can modify the code in SparkJavaHelloWorld.java
to add more endpoints, or add new controllers (remember to add them to web.xml
so Spark can process them).