【问题标题】:Ant Script to Automate the build process自动化构建过程的 Ant 脚本
【发布时间】:2012-11-26 23:34:40
【问题描述】:

我想自动化 ANT 构建过程以部署应用程序。

我想编写一个 ANT 脚本,它会反复查找 build.xml 文件夹中的文件并运行它们,如果子构建失败 它应该通过写入日志跳过并继续执行其他脚本。 任何人都可以发布可以提供帮助的想法或示例。

根文件夹 | |-----文件夹1 | | | |--子文件夹1 | |构建.xml | |--子文件夹2 | |构建.xml |-----文件夹2 |构建.xml | |-----文件夹3 构建.xml

【问题讨论】:

    标签: ant build build-automation antbuilder


    【解决方案1】:

    我建议使用subant 任务

    <project name="Subant demo" default="deploy-everything">
        <target name="deploy-everything">
            <subant>
                <fileset dir="." includes="**/build.xml" excludes="build.xml"/>
                <target name="clean"/>
                <target name="deploy"/>
            </subant>
        </target>
    </project>
    

    这将找到所有“build.xml”文件并在每个文件上调用“干净部署”目标。

    虽然自动提取子文件夹构建很整洁,但它很少在大型项目中工作,除非构建相互独立(构建顺序很重要)。

    以下示例使用显式文件列表,而不是无序的文件集:

    <project name="Subant demo" default="deploy-everything">
        <target name="deploy-everything">
            <subant>
                <filelist dir=".">
                    <file name="Folder1/SubFolder1/build.xml"/>
                    <file name="Folder1/SubFolder2/build.xml"/>
                    ..
                </filelist>
                <target name="clean"/>
                <target name="build"/>
            </subant>
        </target>
    </project>
    

    最后,最先进的解决方案是使用像ivy 这样的依赖管理器在“ivy.xml”文件中声明每个模块的依赖。正确设置,这使得每个子模块构建更加独立。为了解决构建“一切都只运行问题”,ivy 提供了一个buildlist 任务,可以自动确定正确的构建顺序:

    <target name="deploy-everything">
        <ivy:buildlist reference="build-path">
            <fileset dir="." includes="**/build.xml" excludes="build.xml"/>
        </ivy:buildlist>
    
        <subant buildpathref="build-path">
            <target name="clean"/>
            <target name="build"/>
        </subant>
    </target>
    

    【讨论】:

    • 是的,在大项目中确实有效,构建顺序也很重要,你能看看我的解决方案有没有机会提供优先级...:)
    • @Azhar 使用 ivy 的 buildlist 任务来自动确定构建顺序的唯一方法。它基于 ivy.xml 文件中创建的模块间依赖关系来执行此操作。
    【解决方案2】:

    我的问题已经解决了,谢谢大家的回复,我用下面的技巧来处理这种情况。

    <?xml version="1.0" ?> 
    <project name="MasterBuildPrj" default="MasterBuild">
        <macrodef name="iterate">
            <attribute name="target"/>
            <sequential>
                <subant target="@{target}">
                    <fileset dir="." 
                             includes="**/build.xml"
                             excludes="build.xml"/>
                </subant>
            </sequential>
        </macrodef>
        <target name="MasterBuild"  description="Build all sub projects">
            <iterate target="build"/>
        </target>
    
        <target name="clean"  description="Clean all sub projects">
            <iterate target="clean"/>
        </target>   
    </project>
    

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 2021-12-01
      • 2013-03-16
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2011-10-17
      相关资源
      最近更新 更多