Skip to content

Instantly share code, notes, and snippets.

@sam
Created September 12, 2012 17:10
Show Gist options
  • Save sam/3708231 to your computer and use it in GitHub Desktop.
Save sam/3708231 to your computer and use it in GitHub Desktop.
Simplest possible JRuby+Java test
package org.foo;
public class Bar {
public static int baz() {
return 1;
}
}
#!/usr/bin/env jruby
require "minitest/autorun"
describe org.foo.Bar do
describe "baz" do
it "must return 1" do
org.foo.Bar.baz.must_equal 1
end
end
end
@sam
Copy link
Author

sam commented Sep 12, 2012

The goal is to find a packaging tool for creating mixed Java/Ruby projects with JRuby. Something that will:

  • Compile your Java
  • Set the class-paths for JRuby so you can write your tests with Minitest and not have to use a different test framework for your Java
  • Download JAR dependencies
  • Download Gem dependencies
  • Ultimately the Package task should package your .class AND .rb files (ideally as a RubyGem)

Buildr will download your dependencies (both Gems and JARS!), compile your Java (if you use nasty Maven paths everywhere), set your class-path, and execute your Ruby for testing, but it doesn't support Minitest (and adding your own TestFramework is incredibly complicated).

That said, Buildr comes to closest to a working solution I've tried. It has 1.9.3 warnings (RbConfig), and seems only mildly supported though.

Honestly. How are people doing this? Mixed Java/Ruby projects would seem like JRuby's #1 killer feature, but there's so little guidance out there on how to create a SANE build-process. (Or I'm just looking at all the wrong places.)

@sam
Copy link
Author

sam commented Sep 12, 2012

Looking for alternatives, I could:

  • Satisfy Dependency issues with a combination of JBundler and Bundler
  • Compile with JRuby's built-in Ant support
  • Package as normal with Rubygems

That means I'd be stuck using Rake, and I'd have at-least four files to define my build-process:

  • project.gemspec
  • Gemfile
  • Jarfile
  • Rakefile

I'd like to end up being able to make a Ruby script executable and just run a Listener on my tests/libs like this: https://github.com/sam/harbor/blob/master/test/watch.rb

Not really sure how that's going to work throwing Rake into the mix as a requirement. Not to mention Rake tends to just slow everything down IME.

@sam
Copy link
Author

sam commented Sep 12, 2012

Or I could:

  • Specify my Ruby dependencies with Bundler
  • Add my Java dependencies to a pom.xml
  • git add pom.xml
  • mvn dependency:copy-dependencies
  • mkdir jars
  • cp target/dependency/*.jar jars/
  • git add jars
  • Use JRuby::Ant support to compile my Java code on-the-fly when harnessing my tests and packaging a Gem

Then in my Ruby scripts, after bundler/setup I can just: Dir["jars/*.jar"].each { |jar| require jar }

A standard rake-task could handle actually releasing a gem of the library I'm developing by adding the jars to the FileList.

It's //so// dirty, but it'd work, and I'm drawing a blank thinking of anything better.

Buildr is so close... but it's just much too huge of a project to fix, and the internals are just mind-bending since it's built on top of Rake. Plus it wouldn't let me setup a class-path and ensure the build as a pre-requisite outside of Rake, and I much prefer directly executable test/spec-files.

@sam
Copy link
Author

sam commented Sep 12, 2012

As an example of the above mention of direct execution, you can:

  1. Clone this project: https://github.com/sam/blog
  2. ./install-dependencies
  3. bundle install
  4. test/watch.rb

Which is basically the setup I mention above, except using Maven instead of Ant to compile. (Only because I assume using a non-Maven source tree structure, and custom build-dir would be a little simpler with Ant?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment