SASS implementation for Java?

user213225 picture user213225 · Nov 17, 2009 · Viewed 33.4k times · Source

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?

Answer

Joep picture Joep · Dec 15, 2010

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>