10

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?

Community
  • 1
  • 1
Adam Gent
  • 47,843
  • 23
  • 153
  • 203

4 Answers4

5

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.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 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 Sep 10 '10 at 14:08
  • 1
    @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. – Sean Patrick Floyd Sep 10 '10 at 14:33
  • 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 Sep 10 '10 at 14:39
  • 2
    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 Sep 19 '10 at 13:00
3

I ended up using wro4j.

Highly recommend the library as it will handle many things like less css and coffee CoffeeScript.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • Is wro4j pure java solution? Seems using less.js ? I have in intranet high isolation from public net, some computers are isolated totally. – Jacek Cz Sep 14 '15 at 09:17
1

A good solution for using sass with eclipse is answered in this question.

Community
  • 1
  • 1
Joep
  • 4,003
  • 3
  • 28
  • 32
0

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.

Natalie Weizenbaum
  • 5,884
  • 28
  • 22
  • 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 Sep 10 '10 at 21:14
  • 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. – Natalie Weizenbaum Sep 11 '10 at 01:37
  • 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. – Sean Patrick Floyd Sep 11 '10 at 10:43
  • 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 – Sean Patrick Floyd Sep 11 '10 at 10:47