【问题标题】:How do I execute an ant task for each line in a given file?如何为给定文件中的每一行执行 ant 任务?
【发布时间】:2010-08-06 12:55:54
【问题描述】:

我需要为给定文件的每一行执行一个 ant 任务。任何想法表示赞赏。

【问题讨论】:

  • 你能举例说明“给定文件中的每一行”是什么意思吗?
  • 当然,假设有这样一个文件:param1 param2 param3 param4 param5 param6 ...我希望 ant 读取该文件并将参数传递给子任务。

标签: ant


【解决方案1】:

我有一个定义需要运行的进程的属性文件。这一切都在单行上并以逗号分隔,而不是您指定的多行。这个answer 展示了如何遍历文件。

env.start=webserver:localhost, dataserver:localhost

然后在我处理应用程序执行的 ant 文件中,我有以下内容

<target name="start-all" description="Start all processes specified in target-info.properties:env.start">
        <foreach list="${env.start}" trim="yes" param="process.and.host" target="-start-process"/>
</target>

<target name="-start-process">
        <property name="colon.separated.pattern" value="([^:]*):([^:]*)"/>
        <propertyregex property="process" input="${process.and.host}" regexp="${colon.separated.pattern}" select="\1"/>
        <propertyregex property="host" input="${process.and.host}" regexp="${colon.separated.pattern}" select="\2"/>
        <condition property="start.target" value="start-${process}" else="-start-process-ssh">
            <equals arg1="${host}" arg2="localhost" trim="yes" casesensitive="no"/>
        </condition>
        <antcall target="${start.target}"/>
</target>

${start.target} 然后针对 env.start 属性中定义的进程执行,例如

<target name="start-webserver" description="Start the webserver on this machine">
        <echo>** Starting webserver **</echo>
        <run-script dir="${project.base.dir}/apache-tomcat" script="${project.base.dir}/apache-tomcat/bin/startup" spawn="yes">
            <args>
                <env key="CATALINA_HOME" value="${project.base.dir}/apache-tomcat"/>
                <env key="CATALINA_PID" value="${project.base.dir}/apache-tomcat/logs/pid_catalina"/>
            </args>
        </run-script>
</target>

<target name="start-dataserver" depends="decipher_caldb_password,check-event-seed,run-prestart-sql" description="Start the dataserver on this machine">
        <run-calypso process="dataserver" class="calypsox.apps.startup.StartNOGUI" failonerror="yes"
            args="-class com.calypso.apps.startup.StartDataServer"/>
</target>

我可以通过运行来启动 env.start 中定义的所有进程

ant -f run.xml start-all

【讨论】:

    猜你喜欢
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多