你的位置:首页 > Java教程

[Java教程]【ANT】构建文件build.xml

 

  • 运行ant

ant 使用当前目录下的build.

ant –buildfile mybuild.

ant –buildfile mybuild.

  • 编写build.

  1. project

    例如:

    <project name="Cesium" default="combine">

    属性:

    name : 项目名称

    default:指定运行的target名称,没有指定时使用缺省的target;

    basedir:基准路径,其他的相对路径都是基于这个基准路径的;

    description:项目描述信息。 

  2. target

    <target name="combine" depends="build,combineJavaScript" description="Combines all source files into a single stand-alone script." />

    注意:一个target可以依赖于其他target。例如combine依赖于build target和combineJaveScript target,ant会按照depends中target出现的顺序依次执行,并且每个target只会被执行依次,即使有多个target依赖于他。

    target有以下属性:

    name:target名称,这个属性是必须的;

    depends:依赖目标,非必须

    if:当属性设置时才执行该target,非必须;

    unless:当属性没有设置时才执行该target,非必须;

    description:target的描述信息,非必须;

  3. task

    Task分为内置task和自定义task,每个task都是一段可执行的代码。

    1.  内置task

      <copy file="${requirejsPath}/require.min.js" tofile="${cesiumViewerOutputDirectory}/require.js" />

    2. 自定义task

      调用任务:

      <glslToJavascript minify="${build.minification}" minifystatefile="${buildDirectory}/minifyShaders.state">  <glslfiles dir="${shadersDirectory}" includes="**/*.glsl" />  <existingjsfiles dir="${shadersDirectory}" includes="**/*.js" excludes="*.profile.js" /></glslToJavascript>

      定义任务:

      <scriptdef name="glslToJavascript" language="javascript" src="${tasksDirectory}/glslToJavaScript.js" manager="bsf" classpathref="javascriptClassPath" loaderref="javascript.loader">      <attribute name="minify" />      <attribute name="minifystatefile" />      <element name="glslfiles" type="fileset" />      <element name="existingjsfiles" type="fileset" /></scriptdef>

      这是glslToJavaScript.js源代码:

      /*global importClass,project,attributes,elements,java,Packages*/importClass(Packages.org.mozilla.javascript.tools.shell.Main); /*global Main*/Main.exec(['-e', '{}']);var load = Main.global.load;load(project.getProperty('tasksDirectory') + '/shared.js'); /*global forEachFile,readFileContents,writeFileContents,File,FileReader,FileWriter,FileUtils*/importClass(java.io.StringReader); /*global StringReader*/importClass(java.util.HashSet); /*global HashSet*/importClass(Packages.org.apache.tools.ant.filters.StripJavaComments); /*global StripJavaComments*/var minify = /^true$/.test(attributes.get('minify'));var minifyStateFilePath = attributes.get('minifystatefile');writeFileContents(minifyStateFilePath, minify);var minifyStateFileLastModified = new File(minifyStateFilePath).lastModified();// collect all currently existing JS files into a set, later we will remove the ones// we still are using from the set, then delete any files remaining in the set.var leftOverJsFiles = new HashSet();forEachFile('existingjsfiles', function(relativePath, file) {  "use strict";  leftOverJsFiles.add(file.getAbsolutePath());});var builtinFunctions = [];var builtinConstants = [];var builtinStructs = [];forEachFile('glslfiles', function(relativePath, file) {  "use strict";  var glslFile = file;  var jsFile = new File(file.getParent(), file.getName().replace('.glsl', '.js'));  // identify built in functions, structs, and constants  if(glslFile.getPath().indexOf('Builtin' + File.separator + 'Functions') !== -1) {    builtinFunctions.push(file.getName().replace('.glsl', ''));  }  else if(glslFile.getPath().indexOf('Builtin' + File.separator + 'Constants') !== -1) {    builtinConstants.push(file.getName().replace('.glsl', ''));  }  else if(glslFile.getPath().indexOf('Builtin' + File.separator + 'Structs') !== -1) {    builtinStructs.push(file.getName().replace('.glsl', ''));  }  leftOverJsFiles.remove(jsFile.getAbsolutePath());  if (jsFile.exists() && jsFile.lastModified() > glslFile.lastModified() && jsFile.lastModified() > minifyStateFileLastModified) {    return;  }  var contents = readFileContents(glslFile);  contents = contents.replace(/\r\n/gm, '\n');  var copyrightComments = '';  var extractedCopyrightComments = contents.match(/\/\*\*(?:[^*\/]|\*(?!\/)|\n)[email protected](?:.|\n)*?\*\//gm);  if (extractedCopyrightComments) {    copyrightComments = extractedCopyrightComments.join('\n') + '\n';  }  if (minify) {    contents = String(FileUtils.readFully(new StripJavaComments(new StringReader(contents))));    contents = contents.replace(/\s+$/gm, '').replace(/^\s+/gm, '').replace(/\n+/gm, '\n');    contents += '\n';  }  contents = contents.split('"').join('\\"').replace(/\n/gm, '\\n\\\n');  contents = copyrightComments + '\  //This file is automatically rebuilt by the Cesium build process.\n\  /*global define*/\n\  define(function() {\n\  "use strict";\n\  return "' + contents + '";\n\});';  writeFileContents(jsFile.getAbsolutePath(), contents, true);});// delete any left over JS files from old shadersfor ( var it = leftOverJsFiles.iterator(); it.hasNext();) {  new File(it.next())['delete']();}var generateBuiltinContents = function(contents, builtins, path){  "use strict";  var amdPath = contents.amdPath;  var amdClassName = contents.amdClassName;  var builtinLookup = contents.builtinLookup;  for (var i = 0; i < builtins.length; i++) {    var builtin = builtins[i];    amdPath = amdPath + ',\n    \'./' + path + '/' + builtin + '\'';    amdClassName = amdClassName + ',\n    ' + 'czm_' + builtin;    builtinLookup = builtinLookup + ',\n    ' + 'czm_' + builtin + ' : ' + 'czm_' + builtin;  }  contents.amdPath = amdPath;  contents.amdClassName = amdClassName;  contents.builtinLookup = builtinLookup;};//generate the JS file for Built-in GLSL Functions, Structs, and Constantsvar contents = {amdPath:'', amdClassName:'', builtinLookup:''};generateBuiltinContents(contents, builtinConstants, 'Constants');generateBuiltinContents(contents, builtinStructs, 'Structs');generateBuiltinContents(contents, builtinFunctions, 'Functions');contents.amdPath = contents.amdPath.replace(',\n', '');contents.amdClassName = contents.amdClassName.replace(',\n', '');contents.builtinLookup = contents.builtinLookup.replace(',\n', '');var fileContents = '\//This file is automatically rebuilt by the Cesium build process.\n\/*global define*/\n\define([\n' +contents.amdPath +'\n  ], function(\n' +contents.amdClassName +') {\n\  "use strict";\n\  return {\n' + contents.builtinLookup + '};\n\});';var builtinFile = new File(project.getProperty('shadersDirectory') + '/Builtin', 'CzmBuiltins.js');writeFileContents(builtinFile.getAbsolutePath(), fileContents, true);

      View Code

      Scriptdef使用脚本语言定义一个ant task,ant scripting languanges支持BSF引擎和JSR_223引擎来定义脚本。

      属性:

      name : 任务名称,必需;

      language : 脚本语言,必须被BSF或者JSR引擎支持,必须;

      manager : BSF或者JSR引擎;

      src :执行任务的脚本路径

      uri :

      classpath : script引入的class的classpath路径

      classpathref : 使用refid引入classpath;

      loaderfef : 加载script的loader,从指定的classpath来构造