这则教程是使用Ant的JavaScript和XSLT的汇总,阅读对象适合于中高级的WEB界面开发者。简述之,见笑了。
这则教程来自于Ant使用的邮件列表和手册,社区和论坛提供了更多的和更新的资讯,如果有一些涉及到不公开和版权的问题,请联系作者修改内容。
本文档中所有的邮件地址都做了反网络爬虫处理,替换at为@就是真正的邮件地址。
警告:不对本文的正确性、及时性以及由本文造成的损失负责。本文版权属于开源社区,可以自由拷贝、修改、转帖,请对自己复制和转发等行为负全责。
最新文档参见:http://www.sitepen.com/ant/javascript.html.
啥是Ant?
Apache Ant是一个构建工具,有N多用户使用。总的来说是非常适用于经常需要客户端界面开发的人,特别是处理巨多页面的开发者,比如某君经常需要处理SSI、php、asp或者jsp页面。当然了,也很适合构建一个XSLT的站点(比如XALAN)。如果你对Ant不熟的话,先读手册,看看咋安装、咋使用。
为啥要说Ant里面的JavaScript?
Ant并不具备向递归这样的标准程序指令,不过包含了你可以使用你熟悉语种的预构建任务。JavaScript很容易使用,广为人知,也就很容易添加到Ant扩展里面去。
添加JavaScript到Ant里面去
从mozilla上下载Ant可选jar包:bsf.jar、rhino.jar(重命名成js.jar和rhino.jar),添加到classPath里面去,注意老版本Ant可能不支持,所以版本号要搞对,不要混搭。
添加Ant的XSLT
要添加一个XSLT解析引擎,比如Xalan-J-2,你要下载并配置一下:要添加xalan的jar包到ant的classPath下面。
Hello World例子
看看这个Hello World例子,必须的:
<project name="helloWorld" default="main" basedir=".">
<target name="setup">
<script language="javascript"> <![CDATA[
echo = helloWorld.createTask("echo");
main.addTask(echo);
echo.setMessage("Hello World!");
]]>
</script>
</target>
<target name="main" depends="setup"/>
</project>
generates:
setup:
main:
Hello World!
BUILD SUCCESSFUL
要把JavaScript引入到Ant里面去,就要像使用XML标签一样使用一个script标签。要定义脚本语言,使用老式的语言定义方法也可以,比如设置一个src属性在script标签里面。
生成对象
要让Ant做更有去的事情,就要访问properties、targets、和references,怎么搞,看手册。
project.getProperty(string propertyName);
project.getUserProperty(string propertyName);
project.getTarget(string targetId);
project.getReference(string refereceId);
project可以通过getProject()取得,返回这个可以打日志和其他使用扩展的script task。
如果你搞了重名或者重ID的东西,那么只有最后一个才生效。
XSLT Task
要使用标准的XSLT task,需要一个输入和一个输出文件,把参数传给样式表,见下:
<style in="src/webProject.xml" out="temp/index.html"
extension="html" style="webSite.xsl" processor="trax">
<param name="pageId" expression="com.sitepen.pages.home"/>
</style>
要处理整个文件夹的文档,就要这么搞:
<style basedir="." destdir="temp"
extension="html" style="style/webSite.xsl"/>
也可以传N多参数:
<project name="siteBuilder" default="deploy" basedir=".">
<target name="init">
<property name="runDir" value="." />
<property name="varsDir" value="vars" />
<property name="sourceDir" value="src" />
<property name="outputDir" value="www" />
<property name="tempDir" value="temp" />
<property name="tokenValues" value="tokenValues.properties" />
<property file="${varsDir}/${tokenValues}" />
</target>
<target name="clean" depends="init">
<delete includeEmptyDirs="true" >
<fileset dir="${outputDir}" />
</delete>
</target>
<target name="prepare" depends="clean">
<mkdir dir="${outputDir}" />
</target>
<target name="compile" depends="prepare">
<copy todir="${tempDir}">
<fileset dir="${sourceDir}">
<exclude name="**/*.html"/>
</fileset>
</copy>
<script language="javascript">
<![CDATA[
importClass(java.io.File)
xslt = siteBuilder.createTask("style");
runDirectory = siteBuilder.getProperty("runDir");
tempDirectory = siteBuilder.getProperty("tempDir");
inFile = new File(runDirectory + "/webProject.xml");
outFile = new File(tempDirectory+"/index.html");
xslt.setStyle("webSite.xsl");
xslt.setIn(inFile);
xslt.setOut(outFile);
xslt.setProcessor("trax");
idParameter = xslt.createParam();
idParameter.setName("pageId");
idParameter.setExpression("com.sitepen.pages.home");
xslt.execute();
]]>
</script>
</target>
<target name="deploy" depends="compile,init">
<copy todir="${outputDir}">
<fileset dir="${tempDir}">
<exclude name="**/*.xml"/>
</fileset>
</copy>
<delete includeEmptyDirs="true" >
<fileset dir="${tempDir}" />
</delete>
</target>
</project>
用JavaScript去遍历一个properties文件
*** 我还没试过,因为看起来property文件必须要有一定的特征(NND,这什么玩意,看不懂) ***
用Task来循环
Ant本身是不支持循环机制的,所以很多人就用JavaScript来做到循环。比如可以创建一堆文件夹:
<![CDATA[
importClass(java.io.File); //Java class needed for file creation
var directoryNames = new Array();
directoryNames[0] = "directoryName0";
directoryNames[1] = "directoryName1";
directoryNames[2] = "directoryName2";
// … and so on
// in this example, the directory path is stored as part of the directory name.
for (i=0;i<directoryNames.length;i++)
{
// projectName is the name of your project
makeDirectory = projectName.createTask("mkdir");
directory = new File(directoryNames[i]);
makeDirectory.setDir(directory);
makeDirectory.execute();
}
]]>
原文在:http://www.sitepen.com/blog/2001/09/25/javascript-and-ant/
分享到:
相关推荐
外文翻译:学用JavaScript设计模式
Key topics include programming fundamentals, JavaScript language elements and how to use them effectively, and how to easily and efficiently add powerful new functionality to HTML documents and Java ...
目前为止可以说是公认的最好的javascript教材,从入门到深入,十分经典。这本书网上可以说很少有中文版下载的。分两部分
单页Web应用:JavaScript从前端到后端 单页Web应用:JavaScript从前端到后端
Instead of learning Swift for ios and Java for Android, just learn JavaScript and make apps for ALL platforms using Apache Cordova. This book also covers everything you need to know in order to use ...
Common Uses of JavaScript: Image and Windows JavaScript and User Integration: Navigation and Forms Back-End Interaction with Ajax and Node.js Data Validation Techniques Modern JavaScript Case Study: A...
JavaScript and Open Data invites the reader on a tour of JavaScript with a focus on accessing and presenting data. The book details the features introduced by JavaScript standards since 2015. The ...
翻译招聘 :loudspeaker: 我们需要您的帮助: https : //github.com/ant-design/ant-design-pro/issues/120 特征 :gem_stone: 整洁的设计:遵循蚂蚁设计规范 :triangular_ruler: 通用模板:企业应用程序的典型...
单页Web应用:JavaScript前端到后端的源码下载,提供了全部的源代码供javascript及前端爱好者们研究!
《程序天下:JavaScript实例自学手册》附书光盘
TypeScript: JavaScript Development Guide by Nicholas Brown English | 7 Aug 2016 | ASIN: B01JZPDM1Y | 86 Pages | ... The first part of the book explains to you what TypeScript is and what it is used for.
Since 1996, JavaScript: The Definitive Guide has been the bible for JavaScript programmers—a programmer's guide and comprehensive reference to the core language and to the client-side JavaScript APIs...
JavaScript and JSON Essentials is a step-by-step guide that will introduce you to JSON and help you understand how the lightweight JSON data format can be used in different ways either to store data ...
Chapter 1: What Is JavaScript? .1 Chapter 2: JavaScript in HTML 13 Chapter 3: Language Basics .23 Chapter 4: Variables, Scope, and Memory 79 Chapter 5: Reference Types 97 Chapter 6: Object-Oriented ...
JavaScript中数据结构的游乐场。 这是的子项目,是的双子项目。 :newspaper: 描述 该自述文件重新组合了许多项目,这些项目侧重于使用JavaScript实现数据结构。 该项目本身不包含任何代码。 :baby: 孩子们 :oden: ...
Node.js: JavaScript based framework. Easy Guide Book by Rick L. English | April 13, 2016 | ASIN: B01E8KVNVW | 75 Pages | AZW3/MOBI/EPUB/PDF The following topics are discussed in this book: A ...
Title: Beginning ...Beginning JavaScript, 5th Edition shows you how to work effectively with JavaScript frameworks, functions, and modern browsers, and teaches more effective coding practices using ...
单页Web应用:JavaScript前端到后端的源码下载,提供了全部的源代码供javascript及前端爱好者们研究! 新增了第二章javascript知识温故代码,主要有类与原型,作用域、原型链、运行环境、函数和闭包的定义和实例使用...