3

I am currently using a ruby gem SASS in order to convert my *.scss files to *.css files on a large project. Here is a mockup of the code I am using:

<?xml version="1.0"?>
<!-- scss to CSS -->
<project name="StackOverflowScssCss" default="sass-compile-to-css" basedir=".">
    <property file="build.properties" />

    <target name="sass-compile-to-css">
        <echo message="Compiling scss files to css..." />

        <!-- create the css destination dir if it doesn't already exist -->
        <property name="css-dest" location="${css.dir}" />
        <echo message="Creating directory at ${css.dir} [if it doesn't yet exist]" />
        <mkdir dir="${css-dest}" />


        <!-- create subdirs if necessary -->
        <echo message="Creating css directories (and temporary .css files) for .scss to be compiled..." />
        <touch mkdirs="true">
            <fileset dir="${sass.dir}" includes="**/*.scss" excludes="**/_*" />
            <mapper type="glob" from="*.scss" to="${css.dir}/*.css" />
        </touch>
        <echo message="Running sass executable against sass files and compiling to CSS directory [${css-dest}] " />

        <!-- run sass executable -->

        <apply executable="sass" dest="${css-dest}" verbose="true" force="true" failonerror="true">
            <arg value="--unix-newlines" />
            <!-- Disable creation of map file. THIS SHOULD BE A FLAG  -->
            <arg value="--sourcemap=none" />
            <srcfile />
            <targetfile />
            <fileset dir="${sass.dir}" includes="**/*.scss" excludes="**/_*" />
            <mapper type="glob" from="*.scss" to="*.css" />
        </apply>
        <echo message="Done compiling scss files!" />
    </target>
</project>

Ultimately I want to remove the ruby dependency so I have been looking at this libsass maven plugin. I know that there are many options for libsass but I am trying to stick strictly to Java. Does anyone have any experience with doing this? I don't want to run Node.js, Sass.js ot anything, and I have racked my brain all day on how to do this. Any help is much appreciated!

2 Answers2

0

There is a Java wrapper that can be used with Maven.

If you really want to stick with Java you could either

  • Use that wrapper and run the mvn task from inside your ant script (see here)
  • Adapt that wrapper and build your own Ant Task out of it
alejo
  • 317
  • 3
  • 10
0

I ended up using a JRuby dependency in order to run on the JVM. My code is as follows:

<!-- Jruby Dependent SCSS to CSS conversion -->
<path id="JRuby">
    <fileset file="packages/jruby-complete-1.7.20.1.jar"/> <!-- Location of JRuby jar file -->
</path>  
<target name="compileSass" depends="cleanSass">
    <echo message="Compiling scss files..." />

    <!-- JRuby Script to convert files into new directory -->
    <property name="filesIn" value="${dir.scss}/**/[^_]*.scss" />
    <property name="projectDirectory" value="${user.dir}"/>
    <script language="ruby" classpathref="JRuby">
        <![CDATA[
            require ($project.getProperty('projectDirectory')) + '/packages/sass-3.4.14/lib/sass'
            require ($project.getProperty('projectDirectory')) + '/packages/sass-3.4.14/lib/sass/exec'

            files = Dir.glob($project.getProperty('filesIn'))
            files.each do 
                | file |
                newOutDir = File.dirname(file).sub! 'scss', 'css'
                FileUtils::mkdir_p newOutDir
                puts "[sass compiler] From:" + file
                puts "[sass compiler]   To:" + newOutDir + "/" + File.basename(file, ".*") + ".css"

                opts = Sass::Exec::SassScss.new(["--load-path", File.dirname(file), file, File.join(newOutDir, File.basename(file, ".*") + ".css")], 'scss')
                opts.parse
            end
        ]]>
    </script>
    <echo message="Done compiling scss files!" />
</target>


<target name="cleanSass">
    <echo message="removing .css files..." />
    <delete includeemptydirs="true" failonerror="false">
        <fileset dir="${dir.css}" includes="**/*.css" />
    </delete>
    <echo message="removing .css.map files..." />
    <delete includeemptydirs="true" failonerror="false">
        <fileset dir="${dir.css}" includes="**/*.css.map" />
    </delete>
</target>