Compile SASS with Compass in Ant build.xml

假如想象 提交于 2019-12-04 14:13:42
partizan

Ok, I got it finally working with a lot of effort and working with out of date documentation. Here is what worked or us:

Bundling gems into an executable jar

  1. Download jruby-complete-1.7.4.jar (working version for me) (from http://jruby.org/files/downloads/1.7.4/)
  2. C:\jruby\complete>java -jar jruby-complete-1.7.4.jar -S gem install -i ./compass compass
  3. C:\jruby\complete>jar uf jruby-complete-1.7.4.jar -C compass .
  4. C:\jruby\complete>java -jar jruby-complete-1.7.4.jar -S gem list
  5. After successfully running the cmd above, your jruby-complete-1.7.4.jar will be modified. Now it should contain the bundled gems (including Compass). This works with other gems that you want to bundle and use in your Compass project as well.

compile.rb

Dir.entries(ARGV[0]).each do |lib|     
    $LOAD_PATH.unshift "#{ARGV[0]}/#{lib}/lib" 
end 

require 'rubygems' 
require 'compass' 
require 'compass/exec' 

command_line_class = Compass::Exec::SubCommandUI.new([ARGV[1], ARGV[2], "-q"]).run!

config.rb

http_path = "/"
css_dir = "."
sass_dir = "."
images_dir = "../images"
javascripts_dir = "../js"

output_style = :compressed
line_comments = false

build.xml

    <path id="jruby.classpath">
        <fileset dir="${env.LIB}/jruby/complete">
            <include name="${jruby-complete.build.jar}"></include>  
        </fileset>
    </path>

    <java fork="true" failonerror="true" classpathref="jruby.classpath" classname="org.jruby.Main">
        <arg line="${stylesheetFolder}/compass/compile.rb ${rubyCompleteFolder}/compass/gems compile ${stylesheetFolder}"></arg>
    </java>  

Here is the supporting documentation that helped me with Compass and Ant integration:

I hope this helps and saves you lots of time :)

Octavian Fedorovici

Here is my solution that supports batch compile of multiple different SASS projects in one single java fork, by searching for multiple config.rb files in your java project (based on this answer: https://stackoverflow.com/a/21051968/6440953):

For each argument representing a different sass project path (containing a config.rb file), run the compile command:

compile.rb

require 'rubygems'
require 'compass'
require 'compass/exec'

ARGV.each do |arg|
  Compass::Exec::SubCommandUI.new(["compile", arg, "--force"]).run!
end

Create a similar config.rb file in all SASS project paths:

config.rb

http_path = "/"
css_dir = "."
sass_dir = "."
images_dir = "../images"
javascripts_dir = "../js"

output_style = :compressed
line_comments = false

Set up path of your ruby-complete jar with compass gem bundled inside (see https://stackoverflow.com/a/21051968/6440953).

Search for all config.rb files in your Java project and concatenate their paths to a property. Send this property as argument line to the java fork performing the SASS compilation.

build.xml

<path id="jruby.classpath">
    <fileset dir="${ext.path}/lib/jruby/complete">
        <include name="jruby-complete*.jar"></include>
    </fileset>
</path>

<macrodef name="sassToCss">
    <sequential>
        <dirset id="configRubyDirSet" dir="${ext.path}\web\webroot\_ui\">
            <present targetdir="${ext.path}\web\webroot\_ui\">
                <mapper type="glob" from="*" to="*/config.rb" />
            </present>
        </dirset>

        <pathconvert property="config.rb.dirs.str" pathsep=" " refid="configRubyDirSet"/>

        <java fork="true" failonerror="true" classpathref="jruby.classpath" classname="org.jruby.Main">
            <arg path="${ext.path}\compile.rb"></arg>
            <arg line="${config.rb.dirs.str}"></arg>
        </java>
    </sequential>
</macrodef>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!