I'm looking for SASS implementation in Java (could be used with JSP/JSF). For Python I've found CleverCSS, but there is nothing for Java. Anyone heard something about this sort of tool for generating CSS?
-
3SASS/SCSS is developed in Ruby. To get the best and latest features, it's better to use Ruby then a native implementation in Java. A native implementation will also be behind the lastest and greatest. – Joep Aug 06 '12 at 17:12
-
5@Joepie, Having less dependencies is more important to me than having the latest version. SASS/SCSS as it is at the moment is good enough for my needs. I just want a lightweight way to use it. – Sam Hasler Aug 08 '12 at 13:42
-
2Also, just because something isn't a "native" implementation doesn't automatically mean it will become out of date. Machine translation could be used, or one enthusiastic developer could keep it up to date. – Sam Hasler Aug 08 '12 at 13:46
-
There is new fast and pure Java compiler https://github.com/i-net-software/sass-compiler – Horcrux7 Jul 24 '23 at 07:37
12 Answers
With ANT:
- Download JRuby complete jar file (JRuby Complete jar download page)
- Download the latest HAML/SASS code (HAML/SASS tarball), and extract it. Put it in "/libs/sass-[VERSION]"
- Add the following to an ant build file.
- Replace [VERSION] in the script to the corresponding versions of JRuby and SASS
- Run the ant script, and the sass or scss files will be compiled!
<path id="JRuby">
<fileset file="libs/jruby-complete-[VERSION].jar"/> <!-- Location of JRuby jar file -->
</path>
<target name="compileSCSS">
<echo message="Compiling scss files..." />
<property name="filesIn" value="${dir.css}/scss/**/[^_]*.scss" />
<property name="fileOutDir" value="/${dir.css}/${dir.css.build}" />
<script language="ruby" classpathref="JRuby">
<![CDATA[
require 'libs/sass-[VERSION]/lib/sass'
require 'sass/exec'
files = Dir.glob($project.getProperty('filesIn'))
Dir.mkdir($project.getProperty('fileOutDir')) unless File.exists?($project.getProperty('fileOutDir'))
files.each do
| file |
puts " [sass compiler] " + file + " -> " + $project.getProperty('fileOutDir') + "/" + File.basename(file, ".*") + ".css"
opts = Sass::Exec::Sass.new(["--load-path", File.dirname(file), file, File.join($project.getProperty('fileOutDir'), File.basename(file, ".*") + ".css")])
opts.parse
end
]]>
</script>
<echo message="Done compiling scss files!" />
</target>
With MAVEN:
Maven can also do this: Using the antrun plugin:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>compileAndMinify</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<mkdir dir="${project.build.directory}/compiled" />
<echo message="Compiling scss files..."/>
<path id="JRuby">
<fileset file="${basedir}/jars/jruby-complete-[VERSION].jar"/>
</path>
<property name="filesIn" value="${project.build.directory}/css/**/[^_]*.scss" />
<property name="fileOutDir" value="${project.build.directory}/compiled/css" />
<script language="ruby" classpathref="JRuby">
<![CDATA[
require 'libs/sass-[VERSION]/lib/sass'
require 'sass/exec'
files = Dir.glob($project.getProperty('filesIn'))
Dir.mkdir($project.getProperty('fileOutDir')) unless File.exists?($project.getProperty('fileOutDir'))
files.each do
| file |
puts " [sass compiler] " + file + " -> " + $project.getProperty('fileOutDir') + "/" + File.basename(file, ".*") + ".css"
opts = Sass::Exec::Sass.new(["--load-path", File.dirname(file), file, File.join($project.getProperty('fileOutDir'), File.basename(file, ".*") + ".css")])
opts.parse
end
]]>
</script>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

- 4,003
- 3
- 28
- 32
-
-
Where do you extract the haml/sass archive? Do I need to put it in the gemfiles for JRuby or can I reference them directly somehow? That is, without using the JRuby installer, just the jar. Thanks! – Alex Ciminian May 08 '12 at 13:15
-
The first require "require 'libs/sass/lib/haml'" is the path relative to your project folder. – Joep May 09 '12 at 09:03
-
This means I exrtacted it in the libs/sass directory in my project folder – Joep May 09 '12 at 09:09
-
-
-
Maven has a good plugin for sass now (worked on it myself). sass-maven-plugin. With compass support and watch goal. https://github.com/Jasig/sass-maven-plugin for github, and maven repo: http://mvnrepository.com/artifact/org.jasig.maven/sass-maven-plugin/1.1.0 – Joep Apr 04 '13 at 13:21
-
Thanks @Joepie, I was planning to use your solution instead of that plugin. I will double check it. I just don't want to push other users to install the whole Ruby thing. :) – Rubens Mariuzzo Apr 04 '13 at 13:54
-
1For some reason the `require 'libs/sass-[VERSION]/lib/sass'` didn't work as relative for me, I had to put the absolute from a property I had in hand. So it worked with `require $project.getProperty('project.root') + '/sass/sass-3.2.9/lib/sass'` – L. Holanda Jun 06 '13 at 21:07
-
Has anyone tried this on Mac? It's working fine on windows calling ant target manually in a command prompt. Also works fine under hudson. But a teammate which has a Mac is getting this error: The following error occurred while executing this line: `build.xml:171: org.jruby.exceptions.RaiseException: odd number of args for Hash` (Line 171 on my build.xml is the starting of the ` – L. Holanda Sep 12 '13 at 18:46
-
1Hi Joepie the link to HAML/SASS seems to be dead, could you update the link if possible? – bummi Nov 07 '13 at 18:38
-
Try https://github.com/nex3/sass/archive/stable.zip. Don't know if it still works that way, but you could try... – Joep Nov 12 '13 at 12:14
-
A note to those who use the snippet for the maven plugin: The closing `
` and ` – Mike Jun 29 '14 at 03:30` tags are missing the `/` character. I tried editing the answer, but edits must be at least 6 characters, so this is the best I can manage. -
ZUSS is a good alternative to LESS and SASS. It is similar to LESS. Unlike LESS and SASS, the processing of the ZUSS files doesn't require the JavaScript interpreter.
Disclaimer: I am the developer of ZUSS. I developed it simply because I can't find a neat solution for Java.

- 1,987
- 2
- 15
- 23
-
IMO, Zuss is the best fit for Java environment, such as Java method invocation, variable resolver and resource locator. – Mo. Nov 15 '11 at 01:07
-
Wow, nice solution. I will consider this for future java projects! Really nice. Do you have some more information about caching when using the servlet? The documentation isn't clear about that... How fast is the compile step when using the servlet? – Joep Dec 19 '11 at 11:04
-
1The default servlet doesn't provide any cache. If you like, please extend it. – Tom Yeh Mar 30 '12 at 09:19
-
GitHub location: https://github.com/tomyeh/ZUSS; Maven repo: https://mvnrepository.com/artifact/com.github.tomyeh/zuss/1.0.0-FL-2012-03-20 – nikodaemus Sep 14 '17 at 13:56
There is one project: http://code.google.com/p/jsass/ (but it's in a very early stage).
If you are interested in Less, there is a ready-to-use Java version for it: http://www.asual.com/lesscss/

- 681
- 14
- 23
-
Im only saying this in the interest of more info is good, the project above uses rhino to run a javascript file. – mP. May 02 '12 at 07:23
-
3
You can also take a look to Web Resource Optimizer 4 J (WRO4J) which allows a lots a things (minification, resource merging) and supports Less CSS (at runtime as far as I know).
This means: you add wro4j filter to your web.xml and when you ask for a CSS, your .scss (or .saas) files are compiled to standard CSS.
I have not used it yet, but it seems more advanced than other products listed here.
In fact, I was reading comments on the Less for Java website (http://www.asual.com/lesscss/) and WRO4J uses this library to provide it's "on the fly Less compilation". So I think Less for Java is the way to go.
Did you know that the Vaadin web framework comes with it's own built-in Sass compiler? See https://vaadin.com/blog/-/blogs/state-of-sass-support-in-vaadin-7-today ...

- 3,439
- 32
- 38
I personally find SASS syntax deeply, horribly annoying. For Ruby / Python crowd it may come as second nature; for me as Java guy - not so much. I strongly prefer LESS which builds upon CSS syntax instead of coming up with a new one. That has a nice added advantage of being able to use your existing CSS files "as is" and incorporate LESS features as needed.
That said, neither SASS nor LESS have java ports as far as I know. Both are ruby-based, however, so you can install them under JRuby. The only issue with that approach is JRuby is mind-numbingly slow to start up. It's not a huge deal, though, because you're likely going to use file monitoring in development (and once it does startup it runs very smooth) and you're not going to care as much that your build takes few seconds longer during deployment.
There are also some PHP-based implementations like LessPhp, xCSS and others. Haven't tried them personally, though.

- 99,456
- 24
- 206
- 195
-
3Sass syntax has been toned down: It's now much like CSS without the brackets. The upcoming version of Sass will allow optional brackets. One advantage of Sass is that the compiler and tools are far more mature than the Less counterparts. IntelliJ 9 supports Sass (Install the Ruby plugin to get it). We switched from Less to Sass primarily for the editor support. I now like it more than Less (the parent reference '&' is invaluable) The css2sass tool makes it very easy to convert css files - it will also nest selectors where possible. We converted a large project and have not had any issues. – jeremyh Dec 22 '09 at 23:50
-
I hate sass syntax too. It's supposedly easier to view, but braces make it easier for me. – Ruan Mendes Dec 15 '10 at 17:17
-
10
It seems there is one after all (developed probably a while after this question was asked)

- 44,555
- 61
- 184
- 276
-
Pity it's pulling in jRuby: `
` I'd prefer something that didn't have any dependencies. – Sam Hasler Aug 09 '12 at 11:03org.jruby jruby-complete 1.6.7.1 -
1@SamHasler understood, why do you prefer no dependencies by the way? except the chance it will change license / break / bloat your code base, even maven itself has tons of dependencies... anything good out there actually has many dependencies, that's part of the nice thing about open source, isn't it? – Eran Medan Aug 09 '12 at 15:25
-
If something in one of my dependencies breaks I'd like to be able to go into it and fix it myself. I don't know ruby so I'd prefer something that wasn't build on it. – Sam Hasler Aug 09 '12 at 16:44
-
Ruby is wonderful :) I'm a Java person, and learning Ruby was one of the most fun things I've done recently... – Eran Medan Aug 09 '12 at 16:50
-
I'm not the only person here though, we don't want to add ruby to the list of skills our developers have to support. – Sam Hasler Aug 09 '12 at 17:08
-
1Author of sass-maven-plugin here. Unless someone did a complete re-write of SASS in Java jRuby will always be needed. This is significantly more stable as the same SASS code used in the provided tools are being used by the maven plugin. – Eric Aug 27 '12 at 01:58
-
@Eric - thanks, a little off-topic, couldn't make it work, wasn't sure what to put in the skin param, will open a GH issue... – Eran Medan Aug 27 '12 at 04:19
-
I agree that a plugin built specifically for Sass is the best solution and I don't think the dependencies matter much. That said, I haven't been able to get this plugin to work and I think it's a JRuby problem. – Snekse Nov 19 '12 at 17:23
Given that SASS has to be converted to CSS to be usable, what is wrong with using sass2css that distributes with Ruby SASS?

- 2,930
- 3
- 23
- 29
You can try this filter I just put together - https://github.com/darrinholst/sass-java

- 53
- 1
- 4
I know I'm late to the party, but this is the approach I took to using SASS in a Java project with an Ant build: http://workingonthecoolstuff.blogspot.com/2011/02/using-sass-in-ant-build.html To summarize, I'm doing the build on a machine with Ruby and Sass installed so it was as easy as calling the sass command/application using the Ant "apply" task with a fileset that includes all the SCSS/SASS files.

- 6,524
- 7
- 52
- 65
I am using Compass & Sass in my eclipse project using Ant.
I followed this tutorial here:
http://caueguerra.com/tutorial-using-sass-in-a-java-project
Compass extends Sass and allows for my other extras.

- 1,676
- 3
- 16
- 18
Sassc is a command-line implementation in C, which should be easy to add to the build process. Based on the authors I'd say it should stay up to date with sass:
Sass was originally created by the co-creator of this library, Hampton Catlin (@hcatlin). The extension and continuing evolution of the language has all been the result of years of work by Nathan Weizenbaum (@nex3) and Chris Eppstein (@chriseppstein).

- 12,344
- 10
- 72
- 106