2019-08-14 admin


什么是ant,Apache Ant Adapter, execute Ant tasks from node



Experimental Apache Ant adapter for node.js.

This is a proof-of-concept more than anything else. Still unsure if it will actually work on multiple environments and if it is really a good idea.


Many people been using node.js to run build scripts and everyone is writting the same tasks for the most basic stuff like copying/concateneting/deleting/ziping files and it will take a long time before someone implements all the tasks present on Ant.

Projects like grunt, gear, jake, rivet, roto and many others are all writting their own tasks to do the same things. TBH I think a better approach would be to create separate libs that could do each task (or a group of tasks) and that wasn’t tied to a specific build tool, and create tools that abused this (no need to convert 3rd party lib into a plugin or author a new module just to use the methods inside your build files).

Ant contains many advanced tasks and is battle tested so it makes sense to reuse them instead of reinventing the wheel.

Ant is also not that easy to install on Windows since you need to configure the ANT_HOME, CLASSPATH and JAVA_HOME paths. So it’s easier to use a standalone version of Ant that is distributed together with the npm package. The Java Runtime is avaialble on most computers so calling the ant executable should work out of the box on most cases.

The idea is not to write multiple ant <target>s, but to treat Ant as a standalone lib that can be called from a node.js script. Performance isn’t that great (since JVM isn’t as fast as node) but it is still better than writting error-prone tasks that gets half of the job done.

Build tools based solely on configuration are doomed to “fail” the same way as Ant did “failed”. You can’t express all edge-cases with configuration without creating an overly complex system. Some things that could be easily done with a for loop and a few if/else are a huge PITA with a descriptive syntax (XML/JSON). The main reason why I moved my build scripts to node.js is to the get freedom to write new tasks by myself without major pains, locking down your build to a system that can only be extended by writting plugins that accepts a simple config object won’t scale up. Let’s use Ant for what it is good for (broad amount of battle tested tasks with flexible config options) and use plain JavaScript for those tasks that aren’t covered by Ant.

Maybe this project will motivate someone to port the most important Ant tasks to plain JavaScript. I still haven’t found a single node task that is as complete the ones provided by Ant, see for instance all the options available on the copy task (multiple include/exclude, filter, globmapper, download internet files, flatten, etc…).


The idea is to use the Ant as if it was a standalone library that can be called from node.js, that way you can reuse them in your custom node.js build files when necessary.

It will simply convert JSON-like objects into a temporary XML file and execute the standalone version of Ant passing the custom arguments. This tool is just a “bridge” between Node.js and the Ant JAR file.


Execute the test file to see a very basic example:

node test

You can run it from another node.js program:

var ant = require('ant');

    // concat task (
    concat : {
        fileset : {
            '@dir' : '.',
            include : {
                '@name' : '*.js'
}, function(err, stdout, stderror){
    // it doesn't throw any errors and also doesn't log the stdout by
    // default that way you can control what you want to do.
    if (stdout) console.log(stdout);
    if (stderror) console.log(stderror);
    if (err) throw err;


Right now it contains a single method exec() that accepts an object with each task and a callback. The tasks are just JSON-like representation of the XML markup needed to execute each task, node-ant will convert it back to XML during the exec.

ant.exec(tasks, [args], callback)

  • tasks:
    • JSON-like object containing Ant tasks to be executed.
  • [args]:
    • Command line arguments passed to the ant executable.
  • callback
    • Function executed after exec finishes. Will receive the following arguments: (err, stdout, stderr).

JXON syntax

Attributes starts with @.

> { foo : { '@bar' : 123 }}
<foo bar="123" />

The XML node text value is stored as the actual key value if node doesn’t contain attributes and/or child nodes or on a special property keyValue.

> {
    foo : 'Lorem Ipsum'
<foo>Lorem Ipsum</foo>

> {
    foo : {
        '@bar' : 123,
        keyValue : 'Lorem Ipsum'
<foo bar="123">Lorem Ipsum</foo>

> {
    concat : {
        fileset : {
            '@dir' : '.',
            include : {
                '@name' : '*.js'
<concat><fileset dir="."><include name="*.js" /></fileset></concat>

Use the Ant tasks documentation as reference.


This project is on early experimental phase. The way that ant.exec is called might change in the future, specially since the JXON format currently used doesn’t allow XML nodes with same name that aren’t adjacent to each other (object can’t have multiple properties with the same key). So I might end up changing the format to something closer to JSONML or use some sort of special token to differentiate duplicates (eg. echo#1, echo#2).


  • Test on a computer that doesn’t have JDK installed.
  • Check if it is possible to ship with a standalone version of JDK for the advanced tasks.
  • Make it possible to log to console without waiting the whole task to finish.


  • node.js 0.8.0+
  • Java
  • JDK 1.4+ (depending on which task you use)


v0.2.0 (2012/08/03)

  • add support to functions on tasks (for script task). Thanks to @Diullei
  • small refactor to jxon to improve readability and autoclose empty tags.

v0.1.0 (2012/08/02)

  • initial release.


node-ant is distributed under the MIT license.

Apache Ant is distributed under the Apache License.


转载请注明:文章转载自 JavaScript中文网 []



在实际的业务场景中,form表单里面提交upload是一个非常普遍的应用场景。 这里记录下,自己使用form表单,upload图片的代码。 话不多说 ,直接上代码。 封装一下upload组件 import * as React from &...
原文链接 项目开始前,我们先聊一聊关于项目的一些说明。该项目起始于2017年初,当时公司主要技术栈为gulp+angular,鉴于react的火热的生态,在公司决定研发bss管理系统时选用react开发,目的也是为react native打...
antd报错Cannot read property &apos;filter&apos; of undefined的解决方法
技术栈: react + dva + antd 问题描述: 在状态组件中书写_state_下的_columns_,其中涉及点击某处出现弹框modal的操作,在modal中有select选框,点击取消或X清空所有表单信息。 起初是将_set...
ant design pro 新增页面
ps:参考资料 1.ant design pro 1.在 src/routes/ 下面的任意一个文件夹下面创建一个页面 &#x2F;&#x2F; 填写如下内容 &#x2F;** NewPage.js内容 *&#x2F; import ...
&lt;button type=“button” id=“mainLike” data-id=“1190000014897318” class=&quot;btn btn-success btn-lg mr15 &quot;&gt;赞  |...
Vant 一套基于Vue的移动端UI框架,有赞出品。 因为UI设计的够漂亮,源码结构也比较清晰,插件定位也比较明确,重要是实战过程中的使用体验不错。在最近的项目当中就使用 Vant 作为移动端的基础UI框架,但在实践过程中发现该框架和其他框...
前几天看到有赞的vant,顿时对做一个商城项目产生了兴趣。说干就干,让我们来一步步实现。 预览地址:… 已实现购物车逻辑,商城首页等功能。 商城首页: 使用vuex搭建的购物车,将...
说明 React官方脚手架工具Create-react-app 用于快速创建React应用,但依旧有局限性,我们根据项目需求需要对Create-react-app的配置进行修改。这里针对引入Antd的两种配置方式进行配置。 方案 一. Re...
从零开始React + Dva + ant.design开发项目—路由
前言 之前发现了蚂蚁金服的,感觉这个框架有完整的设计交互规范,组件库丰富,交互友好,代码质量高,稳定性好,然后就一直想要在项目中使用下这个框架,但是之前一直都是用VUE的不怎么会React。今天学习了下怎么去使用Reac...
React ant design表格导出数据为EXCEL表格数据
一、由于公司项目需要,需要将表格数据导出为EXCEL表格数据。环境React+Ant Design 二、安装插件js-export-excel yarn安装-记得以管理员身份执行 yarn add js-export-excel npm安...