我最近开始在一个基于 Java 的项目中使用 SASS [ http://sass-lang.com/ ],并希望创建一个 Ant 任务:
- 对于每个 .scss 文件 + 顶级 scss 目录中包含 .scss 文件的子目录:
- 在主 CSS 目录中创建适当的目录
- 编译 .scss 文件并将 .css 文件放在它所属的位置
我该怎么做呢?
我最近开始在一个基于 Java 的项目中使用 SASS [ http://sass-lang.com/ ],并希望创建一个 Ant 任务:
我该怎么做呢?
它最终花了我一些时间来弄清楚,所以我想我会发布我是如何完成它的。这是我的设置:
build.properties
css.dir=./template/ver1-0/css/v3
sass.dir=./template/ver1-0/css/v3/scss
scss目录结构:
/template/ver1-0/css/v3/scss
+ widgets
- widgettest.scss
- widgettest2.scss
+ global
- globaltest.scss
- globaltest2.css
- file1.scss
- file2.scss
- _partial1.scss
- _partial2.scss
这是Ant 任务:
<!-- scss to CSS -->
<!-- via: http://workingonthecoolstuff.blogspot.com/2011/02/using-sass-in-ant-build.html -->
<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
via: https://stackoverflow.com/questions/536511/how-to-create-directories-specified-by-a-mapper-in-ant -->
<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" />
<srcfile />
<targetfile />
<fileset dir="${sass.dir}" includes="**/*.scss" excludes="**/_*" />
<mapper type="glob" from="*.scss" to="*.css"/>
</apply>
<echo message="Done compiling scss files!" />
</target>
运行任务后,结果如预期:.scss 文件被编译到它们创建时所在的相同路径。如果文件父目录尚不存在,${css.dir}
则相应地创建它。