Deploy Spark webapp (java app) on Heroku...

Deploy Spark webapp (java app) on Heroku...


// /src/main/java/Main.java


import static spark.Spark.*;

import java.util.Optional;

import spark.Spark;

public class Main {
    public static void main(String[] args) {
        // set port
        Optional.ofNullable(System.getProperty("heroku.port"))
        .map(Integer::parseInt)
        .ifPresent(Spark::port);
        
        get("/", (req, res) -> "HELLO WORLD");
    }
}

// build.gradle

plugins {
    id 'java'
    id 'application'
}

repositories {
    jcenter()
}

mainClassName = 'Main'
installDist.destinationDir = file('target')

dependencies {
  compile 'com.sparkjava:spark-core:2.8.0'
}

task install(dependsOn:[installDist]) {
  doLast {
     def libdir = file(new File(installDist.destinationDir, 'lib'))
     def deps = libdir.list().collect{"target/lib/${it}"}.join(':')
     file('Procfile').text = "web: java  \$JAVA_OPTS -Dheroku.port=\$PORT -cp \"$deps\" ${mainClassName}";
  } 
}

task stage(dependsOn: [clean, install])

Comments

Popular posts from this blog

How i'm Using the http-server for the web-development needs