LoginSignup
4
6

More than 5 years have passed since last update.

Spark Framework + Kotlinで書いたGradleプロジェクトをHerokuにデプロイする

Last updated at Posted at 2017-01-12

概要

 タイトルの通りです。

build.gradle

apply plugin: 'kotlin'
version '1.0-SNAPSHOT'

buildscript {
  ext.kotlin_version = '1.0.6'

  repositories {
    mavenCentral()
  }
  dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

repositories {
  mavenCentral()
}

dependencies {
  compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  compile 'com.sparkjava:spark-core:+'
  compile 'org.slf4j:slf4j-simple:+' // Failed to load classで怒られるので入れる
}

// ライブラリのコピー
task copyToLib(type: Copy) {
  into "$buildDir/libs"
  from(configurations.compile)
}

// Herokuから実行されるタスク
task stage(dependsOn: ['clean', 'build', 'copyToLib'])
build.mustRunAfter clean

Procfile

 クラスフォルダとコピー後のライブラリフォルダのパスを通し、実行クラス名を記述します。

web: java $JAVA_OPTS -cp build/classes/*:build/libs/* app.Main

Main.kt

package app

import spark.Spark
import spark.Spark.*


object Main {
  @JvmStatic fun main(args: Array<String>) {
    System.getenv("PORT")?.let {
      Spark.port(it.toInt())
    }
    get("/") { req, res -> "Hello World!" }
  }
}

.gitignore

 ビルドが上手くいかずアプリがクラッシュしたままになってしまうので、下記はGit管理から除外してください。

.gradle
.idea
build

プロジェクト構成

スクリーンショット 2017-01-12 22.52.01.png

サンプル

参考文献

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6