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:
apply plugin: 'java'
apply plugin: 'application'
// TODO Change this to your class with your main method
mainClassName = "my.app.Main"
defaultTasks 'run'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.sparkjava', name: 'spark-core', version: '2.5.5'
// TODO add more dependencies here...
}
// Create a fat executable jar
jar {
manifest {
attributes "Main-Class": "$mainClassName"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
archiveName "app.jar"
}
Build the you application on the command line via gradle build
. This will create an app.jar
file in your build/libs
folder then just run:
java -jar build/libs/app.jar
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
:
FROM java:8
ADD build/libs/app.jar /
EXPOSE 4567
ENTRYPOINT ["java", "-jar", "app.jar"]
Build the docker image and run it, e.g.:
docker build -t myapp:v1 .
docker run --rm --name myapp -p 4567:4567 myapp:v1
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.