views:

105

answers:

2

Java Q: I like CSS for simple web pages but loathe it when it comes to real world sites because you get css explosion and lots of repeating.

I am tempted to use Sass and or Compass but they are Ruby programs which will most likely require some interesting Maven + JRuby love to get working for Java Web app dev. This also makes it difficult if you are using Eclipse or any IDE that supports synchronization with a running web app.

Is there a better alternative for the hell that is CSS in the hell that is Java?

+2  A: 

I went down the same road recently using LessCss, a similar technology. At first I tried to embed JRuby in my build lifecycle. But unfortunately Maven + JRuby is a monster, it's slow, huge and buggy (half the time it wouldn't even start because it would complain about the file path it was running on).

Fortunately, there is now a JavaScript port of LessCss, which I now embed via Mozilla Rhino. I describe the process in this blog post.

Yesterday though I took it to the next level, making a Maven LessCss Plugin to minimize POM configuration and code duplication. Unfortunately I can't share it because it's proprietary code for my current client, but the solution is simple:

Use GMaven to create the Plugin, create an abstract base mojo that calls the LessCss compiler and several concrete implementations that configure the base mojo for different resource sets:

e.g.

  • lesscss:compile
    compiles from all <resources> to ${project.build.outputDirectory}
  • lesscss:test-compile
    compiles from all <testResources> to ${project.build.testOutputDirectory}
  • lesscss:war-compile
    (compiles from all src/main/webapp to ${project.build.directory}/${project.build.finalName} , the exploded war directory)

So while I can't help you with SASS (apart from you asking the auth or to port it to Groovy, Java or JavaScript), I think I've shown you a feasible alternative.

Of course you can also implement a Maven Plugin in java without Groovy (also embedding the JavaScript via Rhino), but I think it's easier in Groovy.

seanizer
Interesting post +1
Pascal Thivent
On my drive to work before I read your response I was thinking of writing a Groovy builder for CSS and or micro DSL similar to SASS with a servlet filter or servlet to serve the CSS (ie what JSP/GSP is to HTML but for CSS). Great post!
Adam Gent
@Adam: As I wrote in my Blog Post, something very similar exists already for Less and Wicket: http://www.richardnichols.net/2010/06/less-css-in-wicket-using-mozilla-rhino/ . BTW If you do ever write such a Groovy Builder please post it here also.
seanizer
@seanizer I'm now thinking of meeting somewhere in between. Servlet Filter/Servlet runs Less.js + Rhino. Basically your idea but nix the dependency on Wicket (which I have nothing against). I'll see if can github a working version this weekend. Awesome blog BTW. -- EDIT Scratch the previous I see Asual already did it.
Adam Gent
@seanizer Have you seen wro4j?Can you add the following links to your response for others: * http://code.google.com/p/wro4j/wiki/LessCssSupport* http://www.asual.com/lesscss/
Adam Gent
A: 

The Sass command-line interface is very thorough. If you call out to sass --update in your build rules, you can just use the standard Sass executable (either via Ruby or JRuby) without having to integrate it directly into your build.

nex3
@nex3 Regardless of whether its an external call or an internal (JRuby) its still integrated with the build. I still have to install JRuby or Ruby on the build machine and each developers machine.
Adam Gent
That's true (of course, the same is true for Rhino + Less). We intend to release an all-in-one Sass+Ruby package at some point in the future, which will hopefully make this easier to manage.
nex3
@nex3 no it's not true for rhino + less, as I can download all required components through maven dependency resolution, hence making the build reproducible anywhere. with jruby being practibly unusable from maven and ruby not running on the java vm there is not really an option to automate sass in a maven build. That's nothing against Sass, it's a cool technology, but the maven / ruby integration sucks. Not your fault, not mine either, and no reason to downvote my answer.
seanizer
To be fair: the only thing I don't download dynamically is the less.js source file, I keep it in the build tree, but it would be simple enough to download it dynamically from the build, as it is available online: http://github.com/cloudhead/less.js/raw/master/dist/less-1.0.35.js
seanizer