SASS implementation for Java?

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?

35499 次浏览

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.

Given that SASS has to be converted to CSS to be usable, what is wrong with using sass2css that distributes with Ruby SASS?

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/

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.

With ANT:

  1. Download JRuby complete jar file (JRuby Complete jar download page)
  2. Download the latest HAML/SASS code (HAML/SASS tarball), and extract it. Put it in "/libs/sass-[VERSION]"
  3. Add the following to an ant build file.
  4. Replace [VERSION] in the script to the corresponding versions of JRuby and SASS
  5. 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>

You can try this filter I just put together - https://github.com/darrinholst/sass-java

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.

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.

http://compass-style.org/

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.

It seems there is one after all (developed probably a while after this question was asked)

https://github.com/Jasig/sass-maven-plugin

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).

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 ...