Bundling Compass with JRuby (I've read all of the articles - can't get it to work)

I’m trying to write a simple Maven plugin for my team that will call
compass and convert out sass to css in our automated build. I’ve spent
several days going through many of the articles and help requests, but I
still haven’t been able to get things working.

I used Nick Seiger’s article to build a JRuby (1.5.6.1) with compass and
all dependencies:
http://blog.nicksieger.com/articles/2009/01/10/jruby-1-1-6-gems-in-a-jar

$ java -jar jruby-complete-1.6.5.1.jar -S gem install -i ./compass
compass
$ jar uf jruby-complete-1.6.5.1.jar -C compass.
$ mv jruby-complete-1.6.5.1.jar compass-0.12.alpha.4.jar

$ java -jar compass-0.12.alpha.4.jar -S compass compile

$ jruby: No such file or directory – compass (LoadError)

The first problem is that the command line doesn’t even work. I get the
following error:

$ jruby: No such file or directory – compass (LoadError)

The second problem is that I eventually got the scripts to run from
within
my Java program, but I get errors that I don’t see when run compass on
the
command line (I have Ruby installed). Here is basically all of the code
for the Maven Mojo:

String myPath =
CompassMavenMojo.class.getProtectionDomain().getCodeSource().getLocation().toExternalForm();
System.out.println(“Path: " + myPath);
String command = String.format(”%s!/bin/start.rb", myPath);
System.out.println("Command: " + command);
org.jruby.Main.main(new String[]{command,“compile”, configLocation});

The start.rb script is in the Mojo project and gets distributed with the
jar. The configLocation variable is a Mojo parameter and is passed in.
It’s the directory where the config.rb file resides. I have a
dependency
on the JRuby complete compass-0.12.alpha.4.jar that I build, so all of
the
JRuby files and the gems reside in that JAR. Here are the contents of
start.rb:

begin

ENV[‘GEM_HOME’] ||= File.expand_path(‘…/gems’, FILE)

require ‘rubygems’
$LOAD_PATH.unshift FILE.sub(/!.*/,
‘!/gems/compass-0.12.alpha.4/lib’)

load ‘gems/compass-0.12.alpha.4/bin/compass’

0
rescue SystemExit => e
e.status
end

It’s actually a file that was generated by Warbler (something else I
tried
that didn’t work), but modified by me. When I install my maven plugin
and
then run a build from the project that uses the plugin I get errors like
this:

Path:
file:/C:/Users/Tony/.m2/repository/com/codeworkslp/compass-maven-plugin/1.0/compass-maven-plugin-1.0.jar
Command:
file:/C:/Users/Tony/.m2/repository/com/codeworkslp/compass-maven-plugin/1.0/compass-maven-plugin-1.0.jar!/bin/start.rb
{:sass=>{:load_paths=>[#<Sass::Importers::Filesystem:0x5fe1153a
@root=“C:/Users/Tony/workspaces/ADT/SelfService/self-service/src/main/webapp/sass”>,
#<Sass::Importers::Filesystem:0x465863 @root=“file:
C:/Users/Tony/.m2/repository/org/jruby/compass/0.12.alpha.4/compass-0.12.alpha.4.jar!/gems/compass-0.12.alpha.4/frameworks/blueprint/stylesheets”>,
#<Sass::Importers::Filesystem:0xa54cbb9 @root=“file:
C:/Users/Tony/.m2/repository/org/jruby/compass/0.12.alpha.4/compass-0.12.alpha.4.jar!/gems/compass-0.12.alpha.4/frameworks/compass/stylesheets”>,
Compass::SpriteImporter], :style=>:compact, :line_comm
ents=>true, :cache=>true,
:cache_location=>“C:\Users\Tony\workspaces\ADT\SelfService\self-service\src\main\webapp/.sass-cache”},
:project_name=>“C:\Users\Tony\workspaces\ADT\SelfService
\self-service\src\main\webapp”, :sass_files=>nil,
:cache_location=>“C:\Users\Tony\workspaces\ADT\SelfService\self-service\src\main\webapp/.sass-cache”}
error src/main/webapp/sass/alarm_contacts.sass (Line 1: File to
import not found or unreadable: compass.
Load paths:
C:/Users/Tony/workspaces/ADT/SelfService/self-service/src/main/webapp/sass
file:C:/Users/Tony/.m2/repository/org/jruby/compass/0.12.alpha.4/compass-0.12.alpha.4.jar!/gems/compass-0.12.alpha.4/frameworks/blueprint/stylesheets
file:C:/Users/Tony/.m2/repository/org/jruby/compass/0.12.alpha.4/compass-0.12.alpha.4.jar!/gems/compass-0.12.alpha.4/frameworks/compass/stylesheets
Compass::SpriteImporter)
overwrite alarm_contacts.css
error src/main/webapp/sass/alarm_dashboard.sass (Line 1: File to
import not found or unreadable: compass.

…and so on, repeating from “Load Paths” for each SASS file processed.
I
realize I’m using an alpha build, but I had this exact problem with
0.11.7,
so that’s not the issue. Also, when I run compass from the command line
(Ruby, not JRuby), it works brilliantly. Like I said, I’ve tried
everything I could find on how to do this including using
ScriptContainer.java, but I end up with the same error. Also, when I
used
Warbler to build an executable jar it also gives me the same output.
I’m
not a Ruby developer, so I don’t know what I’m doing wrong. Any help
would
be appreciated.

Thanks,
Tony

did you try to use the jruby-maven-plugin and/or gem-maven-plugin
(GitHub - torquebox/jruby-maven-plugins: maven plugin to handle rubygems in a maven way. including support for rspec, rails, cucumber, rake, etc) ? it should have
all the tools to execute ruby files, using gems within an (isolated)
maven environment. just add your gems as test-dependencies and your
ruby files in src/test/ruby and execute them at the right phase.

  • Kristian

That’s one I didn’t know about. I’ll give it a try and report back.
Thanks.

The documentation is a bit sparse. Are there any usage examples
anywhere
that might give me a hint? From my post can you tell me which of the
two
plugins is the most suitable for running "compass compile " during
the maven compile lifecycle phase without requiring Ruby/JRuby to be
installed on the local machine? Basically I want to run a
self-contained
JRuby/Compass in the Maven build.

Thanks,
Tony

have a look at this gist

hope that gives you a start. I know the documentation is lacking all
over the place.

regards, Kristian

On Wed, Jan 25, 2012 at 1:09 AM, Tony C. [email protected]
wrote:

Do you have any idea why it might fail in this way via Nexus?

the torquebox maven repository for gems has no directory listings
(yet) which is needed by nexus to proxy a maven repository. let’s see
if I find time for the next week or so.

regards,
Kristian

Thanks Kristian. I got it working, but I had to add all of the gems to
the
3rd party repository of our Nexus instance to get it working.

In my initial try I added the rubygem repositories (releases, snapshots)
as
proxy repositories to Nexus and then added them to Public Repositories.
Unfortunately this did not work:

Failed to collect dependencies for [rubygems:compass:gem:0.12.alpha.4
(compile)]: No versions available for rubygems:sass:gem:[3.1,3.99999]
within specified range -> [Help 1]

When I build without the Nexus repo I did not get that problem. Do you
have any idea why it might fail in this way via Nexus?

Thanks,
Tony