search
HomeWeb Front-endJS TutorialJavaScript Component Journey (3): Building Components with Ant_Javascript Skills

听起来是不是很惬意?Let's go! 我们出发啦~

这期,我们会使用 Ant 将上期编写、整理的代码文件按指定的先后顺序合并成单一的源文件,然后压缩这个文件。这是构建 JavaScript 项目的基本步骤。Ant 是 Apache 的一个顶级开源项目,网上对它的介绍和安装,已经有很多文章,这里就不再赘述了。在构建之前,我们先来看看已有的文件布局:

 smart-queue <span style="COLOR: #c0c0c0">// 组件的根目录</span>
  +--- src <span style="COLOR: #c0c0c0">// JavaScript源文件目录</span>
    +--- lang.js <span style="COLOR: #c0c0c0">// 前文提到的“外部文件”</span>
    +--- smart-queue.js <span style="COLOR: #c0c0c0">// Smart Queue 主文件</span>

现在,我们要让它“丰满”起来:

  • 组件根目录下添加:
    • README: 介绍 Smart Queue 组件
    • LICENSE: 组件的授权信息
    • build.xml: Ant 使用的配置文件
  • 组件根目录下添加 lib 子目录:存放构建过程中需要使用的外部程序和库文件
    • lib 子目录下添加 yuicompressor.jar: 我们用 YUI Compressor 压缩 JavaScript
  • 组件根目录下添加 test 子目录:存放测试组件所需的文件(下期介绍)
  • src 目录下添加 intro.js: 介绍组件的版本及说明信息

麻雀虽小,五脏俱全。现在 Smart Queue 看上去像是比较专业的 JavaScript 项目了:

 smart-queue <span style="COLOR: #c0c0c0">// 组件的根目录</span>
  +--- lib <span style="COLOR: #c0c0c0">// JavaScript外部程序和库文件目录</span>
    +--- yuicompressor.jar <span style="COLOR: #c0c0c0">// YUI Compressor</span>
  +--- test <span style="COLOR: #c0c0c0">// 测试文件目录</span>
  +--- src <span style="COLOR: #c0c0c0">// JavaScript源文件目录</span>
    +--- intro.js <span style="COLOR: #c0c0c0">// 介绍和版本信息</span>
    +--- lang.js <span style="COLOR: #c0c0c0">// 前文提到的“外部文件”</span>
    +--- smart-queue.js <span style="COLOR: #c0c0c0">// Smart Queue 主文件</span>
  +--- README <span style="COLOR: #c0c0c0">// 组件自述文件</span>
  +--- LICENSE <span style="COLOR: #c0c0c0">// 组件授权信息</span>

我们计划将构建出来的文件存放到组件根目录下的 build 子目录,还要通过构建工具创建并销毁它。首次尝试构建前,建议先大概了解一下 Ant 的配置文件——build.xml 的结构:

<span style="COLOR: #b000b0"><span style="COLOR: #c00000">project</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"MyProject"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">default</span>=<span style="COLOR: #008000">"dist"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">basedir</span>=<span style="COLOR: #008000">"."</span><span style="COLOR: #b000b0">></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">description</span><span style="COLOR: #b000b0">></span>
    simple example build file
  <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">description</span><span style="COLOR: #b000b0">></span>
 <span style="COLOR: #707070"><span style="COLOR: #707070">-- set global properties for this build --</span><span style="COLOR: #707070">></span>
 <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"src"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"src"</span><span style="COLOR: #b000b0">/></span>
 <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"build"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"build"</span><span style="COLOR: #b000b0">/></span>
 <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"dist"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"dist"</span><span style="COLOR: #b000b0">/></span>

 <span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"init"</span><span style="COLOR: #b000b0">></span>
  <span style="COLOR: #707070"><span style="COLOR: #707070">-- Create the time stamp --</span><span style="COLOR: #707070">></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">tstamp</span><span style="COLOR: #b000b0">/></span>
  <span style="COLOR: #707070"><span style="COLOR: #707070">-- Create the build directory structure used by compile --</span><span style="COLOR: #707070">></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">mkdir</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${build}"</span><span style="COLOR: #b000b0">/></span>
 <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span>

 <span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"compile"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">depends</span>=<span style="COLOR: #008000">"init"</span>
<span style="COLOR: #b000b0">    </span><span style="COLOR: #0000c0">description</span>=<span style="COLOR: #008000">"compile the source "</span><span style="COLOR: #b000b0"> ></span>
  <span style="COLOR: #707070"><span style="COLOR: #707070">-- Compile the java code from ${src} into ${build} --</span><span style="COLOR: #707070">></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">javac</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">srcdir</span>=<span style="COLOR: #008000">"${src}"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">destdir</span>=<span style="COLOR: #008000">"${build}"</span><span style="COLOR: #b000b0">/></span>
 <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span>

 <span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"clean"</span>
<span style="COLOR: #b000b0">    </span><span style="COLOR: #0000c0">description</span>=<span style="COLOR: #008000">"clean up"</span><span style="COLOR: #b000b0"> ></span>
  <span style="COLOR: #707070"><span style="COLOR: #707070">-- Delete the ${build} and ${dist} directory trees --</span><span style="COLOR: #707070">></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">delete</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${build}"</span><span style="COLOR: #b000b0">/></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">delete</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${dist}"</span><span style="COLOR: #b000b0">/></span>
 <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span>
<span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">project</span><span style="COLOR: #b000b0">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

仔细观察一下,除了 <font face="新宋体">name, description</font> 这些名字都很容易理解外,其他可以看到的规律包括:

  • <font face="新宋体">project</font> 元素的 <font face="新宋体">default</font> 属性值对应某个 <font face="新宋体">target</font> 元素的 <font face="新宋体">name</font> 属性;
  • <font face="新宋体">target</font> 元素的 <font face="新宋体">depends</font> 属性值对应其他某些 <font face="新宋体">target</font> 元素的 <font face="新宋体">name</font> 属性;
  • <font face="新宋体">${somename}</font> 可以引用 <font face="新宋体">property</font> 中定义的值。

下面我们开始写自己的 build.xml.

首先,配置项目的基本信息,以及相关目录名称,将要使用的编码等等:

<span style="COLOR: #b000b0"><span style="COLOR: #c00000">project</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"Smart Queue"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">default</span>=<span style="COLOR: #008000">"compress"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">basedir</span>=<span style="COLOR: #008000">"."</span><span style="COLOR: #b000b0">></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">description</span><span style="COLOR: #b000b0">></span>Build file for Ant<span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">description</span><span style="COLOR: #b000b0">></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"src"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"src"</span><span style="COLOR: #b000b0"> /></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"build"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"build"</span><span style="COLOR: #b000b0"> /></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"lib"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">location</span>=<span style="COLOR: #008000">"lib"</span><span style="COLOR: #b000b0">/></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"inputencoding"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">value</span>=<span style="COLOR: #008000">"utf-8"</span><span style="COLOR: #b000b0">/></span>
  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">property</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"outputencoding"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">value</span>=<span style="COLOR: #008000">"gbk"</span><span style="COLOR: #b000b0">/></span></span></span></span></span></span></span></span>

接着,定义一个用于初始化的 <font face="新宋体">target</font>, 它负责创建 build 子目录:

  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"init"</span><span style="COLOR: #b000b0">></span>
    <span style="COLOR: #b000b0"><span style="COLOR: #c00000">mkdir</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${build}"</span><span style="COLOR: #b000b0">/></span>
  <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span></span></span>

然后定义名为 <font face="新宋体">concat</font><font face="新宋体">target</font>, 负责将 src 里的 3 个 JavaScript 文件按先后顺序连接起来。运行它要先运行前面定义的 <font face="新宋体">init</font>:

  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"concat"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">depends</span>=<span style="COLOR: #008000">"init"</span><span style="COLOR: #b000b0">></span>
    <span style="COLOR: #b000b0"><span style="COLOR: #c00000">concat</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">destfile</span>=<span style="COLOR: #008000">"${build}/smart-queue.source.js"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">encoding</span>=<span style="COLOR: #008000">"${inputencoding}"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">outputencoding</span>=<span style="COLOR: #008000">"${outputencoding}"</span><span style="COLOR: #b000b0">></span>
      <span style="COLOR: #b000b0"><span style="COLOR: #c00000">filelist</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${src}"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">files</span>=<span style="COLOR: #008000">"intro.js, lang.js, smart-queue.js"</span><span style="COLOR: #b000b0"> /></span>
    <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">concat</span><span style="COLOR: #b000b0">></span>
  <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span></span></span></span>

这样,就可以得到一个可以工作的 JavaScript 文件,下面的 <font face="新宋体">target</font> 负责压缩这个文件,显然它依赖于 <font face="新宋体">concat</font>, 也依赖于 <font face="新宋体">init</font>, 但是不必显式指定对 <font face="新宋体">init</font> 的依赖——Ant 能处理这种依赖关系。这里调用 YUI Compressor 并传入适当的参数:

  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"compress"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">depends</span>=<span style="COLOR: #008000">"concat"</span><span style="COLOR: #b000b0">></span>
    <span style="COLOR: #b000b0"><span style="COLOR: #c00000">java</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">jar</span>=<span style="COLOR: #008000">"${lib}/yuicompressor.jar"</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">fork</span>=<span style="COLOR: #008000">"true"</span><span style="COLOR: #b000b0">></span>
      <span style="COLOR: #b000b0"><span style="COLOR: #c00000">arg</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">line</span>=<span style="COLOR: #008000">"--type js --charset utf-8 -o ${build}/smart-queue.js ${build}/smart-queue.js"</span><span style="COLOR: #b000b0">/></span>
    <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">java</span><span style="COLOR: #b000b0">></span>
  <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span></span></span></span>

大功告成,<font face="新宋体">compress</font> 处理后的文件就可以部署到生产系统上去了。最后我们做一下清理工作,使你在生成文件后还可以回到最初的状态:

  <span style="COLOR: #b000b0"><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">name</span>=<span style="COLOR: #008000">"clean"</span><span style="COLOR: #b000b0">></span>
    <span style="COLOR: #b000b0"><span style="COLOR: #c00000">delete</span><span style="COLOR: #b000b0"> </span><span style="COLOR: #0000c0">dir</span>=<span style="COLOR: #008000">"${build}"</span><span style="COLOR: #b000b0">/></span>
  <span style="COLOR: #b000b0"></span><span style="COLOR: #c00000">target</span><span style="COLOR: #b000b0">></span></span></span>

到此可以说基本的配置就写完了。怎么使用它呢?以命令行方式进入到组件根目录(或者说 build.xml 所在的目录),然后:

  • 运行 <font face="新宋体">ant concat</font>, 将得到 ./build/smart-queue.source.js
  • 运行 <font face="新宋体">ant</font>, 将选择 <font face="新宋体"><project></project></font><font face="新宋体">default</font> 引用的那个 <font face="新宋体">target</font>, 即 <font face="新宋体">compress</font>, 所以会得到 ./build 下的 smart-queue.source.js 和 smart-queue.js
  • 运行 <font face="新宋体">ant clean</font>, 将删除 ./build 目录,回到最初的状态

这些前提是你已经正确安装或者说设置好了 JDK 和 Ant, 如果有错误提示出来,则可能需要检查它们是否已准备妥当。

一路看下来,是不是觉得本期介绍的东西很简单?那是当然了,构建工具就应该简单易用,否则把大量的时间花在那上面岂非不值?工具的价值在于提升生产力,从而创造更多价值。

Finally, you can view Ant’s help documentation at here (there are a lot of fun stuff in it), or you can view it at here View the complete build.xml file of this issue.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools