Skip to content

Instantly share code, notes, and snippets.

@mrkaspa
Created August 30, 2016 18:46
Show Gist options
  • Save mrkaspa/bb806dff7858519a025785cd48bedc1d to your computer and use it in GitHub Desktop.
Save mrkaspa/bb806dff7858519a025785cd48bedc1d to your computer and use it in GitHub Desktop.
Calling spark from jruby
/*
* This project is intended to be used as an acceptance test *and* a
* documentation example. If you change this file, please be sure that it
* renders appropriately in the generated documentation
*/
buildscript {
repositories { jcenter() }
dependencies {
/* here to make sure that our dependencies get loaded in properly under
* GradleTest, this is NOT needed by end-users
*/
classpath 'com.github.jengelman.gradle.plugins:shadow:[1.2.2,2.0)'
/* Replace "%%VERSION%%" with the version of JRuby/Gradle you wish to
* use if you want to use this build.gradle outside of gradleTest
*/
classpath 'com.github.jruby-gradle:jruby-gradle-plugin:1.3.0'
classpath 'com.github.jruby-gradle:jruby-gradle-jar-plugin:1.1.0'
}
}
apply plugin: "com.github.jruby-gradle.jar"
repositories { jcenter() }
dependencies {
/* Using the built-in `jrubyJar` configuration to describe the
* dependencies our jrubyJar task will need, so the gem is properly
* included in the resulting .jar file
*/
jrubyJar "rubygems:colorize:0.7.7+"
jrubyJar 'org.slf4j:slf4j-simple:1.7.12'
jrubyJar 'com.sparkjava:spark-core:2.5'
}
jrubyJar {
/* We want to use this Ruby script as our start point when the jar executes
*/
initScript "script.rb"
}
/*
* This task is only here for the execution fo the gradleTest
*/
task runGradleTest(type: Exec) {
dependsOn jrubyJar
description "Execute the constructed self-executing jar"
environment [:]
workingDir "${buildDir}/libs"
commandLine 'java', '-jar', jrubyJar.outputs.files.singleFile.absolutePath
}
require 'colorize'
java_import 'org.slf4j.Logger'
java_import 'org.slf4j.LoggerFactory'
java_import 'spark.Spark'
logger = LoggerFactory.getLogger('demo')
puts "-" * 20
logger.info "Ruby version: #{RUBY_VERSION}"
logger.info "Ruby platform: #{RUBY_PLATFORM}"
logger.info "Current file: #{__FILE__}"
puts "-" * 20
puts "Roses are red".red
puts "Violets are blue".blue
puts "I can use JRuby/Gradle".green
puts "And now you can too!".yellow
Spark.get("/", ->(req, res){ "hello" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment