【问题标题】:overwriting ANT properties file覆盖 ANT 属性文件
【发布时间】:2011-09-10 16:48:39
【问题描述】:

如何用新创建的属性文件覆盖一些现有的属性?

这是所需的结构:

initially load Master.properties
generate new.properties


load new.properties and master.properties
run master.xml (ANT script)

这个想法是 Master.properties 生成一些应该被 new.properties 替换的产品版本。但是,Master.properties 中的其他属性应保持不变。

阅读this 无济于事,因为我不知道如何加载 new.properties 文件

编辑这里是 ANT 脚本:

<project name="nightly_build" default="main" basedir="C:\Work\NightlyBuild">
    <target name="init1">
        <sequential>
                    <property file="C:/Work/NightlyBuild/master.properties"/>
            <exec executable="C:/Work/Searchlatestversion.exe">
                <arg line='"/SASE Lab Tools" "${Product_Tip}/RELEASE_"'/>
            </exec>
            <sleep seconds="10"/>
            <property file="C:/Work/new.properties"/>

        </sequential>
    </target>
    <target name="init" depends="init1">
        <sequential>
            <echo message="The product version is ${Product_Version}"/>
            <exec executable="C:/Work/checksnapshot.exe">
                <arg line='-NightlyBuild ${Product_Version}-AppsMerge' />
            </exec> 
            <sleep seconds="10"/>
            <property file="C:/Work/checksnapshot.properties"/>
            <tstamp>
                <format property="suffix" pattern="yyyy-MM-dd.HHmm"/>
            </tstamp>
        </sequential>
    </target>
    <target name="main" depends="init">
            <echo message="loading properties files.." />
            <echo message="Backing up folder" />
            <move file="C:\NightlyBuild\NightlyBuild" tofile="C:\NightlyBuild\NightlyBuild.${suffix}" failonerror="false" />
                <exec executable="C:/Work/sortfolder.exe">
                    <arg line="6" />
                </exec>
                <exec executable="C:/Work/NightlyBuild/antc.bat">
                </exec> 
    </target>
</project>

在上述脚本中,&lt;exec executable="C:/Work/NightlyBuild/antc.bat"&gt; 将运行 Master.xml ANT 脚本。这个 Master.xml 将加载 Master.properties:

<project name="Master ANT Build" default="main" >               
    <taskdef name="CFileEdit" classname="com.ANT_Tasks.CFileEdit"/>
    <!-- ========================================================== -->
    <!-- init: sets global properties                               -->
    <!-- ========================================================== -->
    <target name="init">
        <property environment="env"/>
        <!-- ========================================================== -->
        <!-- Set the timestamp format                   -->
        <!-- ========================================================== -->
        <property file="Master.properties"/>
         ...
</project>

【问题讨论】:

    标签: ant


    【解决方案1】:

    您应该能够通过查看加载(或以其他方式指定)属性值的顺序来解决此问题。您可能根本不需要覆盖属性值,核心 Ant 不支持这一点。

    也许您可以将 Master.properties 拆分为两个文件 - 一个在生成 new.properties 之前加载,另一个在生成之后加载?

    也许您根本不需要生成 new.properties。

    您能否详细说明您需要做什么?

    既然你最终派生了一个新的 Ant 进程(exec antc.bat),那这不是开始一个新的环境吗?如果它只加载 Master.properties,那么它只有这些属性。

    不确定你的 antc.bat 是做什么的,但是以这种方式从 Ant 执行 Ant 是很不寻常的。有两个标准任务可能有用 - AntAntCall

    可以从您以后的 cmets 继续运行...

    让我们说,而不是这样做:

    <exec executable="antc.bat">
    

    你做了这样的事情:

    <ant file="Master.xml" inheritall="false">
       <property name="Product_Version" value="${Product_Version}"/>
    </ant>
    

    我认为这正在朝着您想要的方向发展。您可以选择性地传递通过加载 new.properties 获得的特定值。请参阅Ant task 的文档。

    如果您仍然存在在加载 new.properties 之前已经定义 Product_Version 的问题,那么我会说获取您拥有的生成 new.properties 的脚本以输出具有不同名称的版本,例如New_Product_Version。然后像这样调用你的主构建:

    <ant file="Master.xml" inheritall="false">
       <property name="Product_Version" value="${New_Product_Version}"/>
    </ant>
    

    【讨论】:

    • 请查看我的编辑以便更好地理解。作为要求的一部分,我将无法拆分此 Master.properties
    • 为什么需要在目标init1中加载Master.properties?为什么需要在new.properties之前加载?
    • 哎呀抱歉,我做了编辑 ${Product_Tip} 应该被解析
    • 你 fork 一个新的 Ant 进程 (exec antc.bat)。
    • 哦,你的意思是如果antc.bat需要master.properties,它不会是“init1”中的那个?
    【解决方案2】:

    可能这是一个老问题。希望OP正在阅读此内容。

    您可以只使用 ant 任务“propertyfile”。 reference

    它可以从文件中读取属性并将更新后的值写回它们。

    【讨论】:

    • 这是这部分所说的更正确的版本:`' 编辑:更正和谁认为按 Enter 是发表评论的好方法!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2012-09-05
    • 1970-01-01
    相关资源
    最近更新 更多