Ant任务 - 多个文件 - 通过循环 [英] ANT task - multiple files - loop through

查看:375
本文介绍了Ant任务 - 多个文件 - 通过循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相当复杂的Ant任务来写。我不得不做出单独建立文件和1左侧。
有4个文件:

I have quite complicated ant task to write. I had to make separate builds files and 1 left. There are 4 files:

 buildpackage.bat, buildpackage.xml, buildpackage.txt, structure.xml

buildpackage.bat只是调用 buildpackage.xml蚂蚁-f buildpackage.xml Ant文件。没事儿。

buildpackage.bat is just an ant file that calls buildpackage.xml "ant -f buildpackage.xml". That is fine.

buildpackage.txt简单,得到了逗号分隔的行:

buildpackage.txt is simple and has got comma-separated lines:

server1,client1
server2,client2
genericserver,client[x]
server[x],client[x]

有始终是服务器名称和客户端名称:

There is always server name and client name:


  • 服务器名称可以是字母只,数字和字母,但具有可变长度。

  • Server name can be letters only, numbers and letters but has variable length.

客户端名称 - 只有字母。

Client name - letters only.

我需要将这些文件分割成两个独立的变量: server.name client.name

I need to split those files into 2 separate variables: server.name and client.name.

我所做的是:

<loadfile property="buildpackage" srcFile="buildpackage.txt"/>    
<for list="${buildpackage}" param="depl.det" delimiter="${line.separator}">
 <sequential>
  <echo>
   Input:   @{deploy.details}
  </echo>
 </sequential>
</for>

我得到的结果是:

The results I get are:

[echo] Input:   server_01,Client_01
[echo] Input:   server_02,Client_02
[echo] Input:   server_03,Client_03
[echo] Input:   clientserver,client_04

我想server_01是参数1和client_02是参数2

I would like server_01 to be parameter1 and client_02 to be parameter2.

我将在顺序执行其他任务。我会从客户端[X]目录中的文件复制到服务器[X](说起来很简单,但我如何拆分字符串转换成2个独立的变量?)。

I will perform other tasks in a sequence. I will copy files from client[x] directory to server[x] (that is simple but how do I split that string into 2 separate variables?).

和现在的非常复杂的任务。

And now the very complicated task.

我也有structure.xml文件。的结构是:

I also have structure.xml file. The structure is:

 <core>
   <clients>
     <client>
       <name>Client_01</name>
       <branding>brand_01</branding>
       <applications>
         <application>application_01</application>
       </applications>
       <solutions>
         <solution>solution01</solution>
       </solutions>
   </client>
   <client>
     <name>Client_07</name>
     <branding>brand_09</branding>
     <applications>
       <application>application_03</application>
     </applications>
     <solutions>
       <solution>solution01</solution>
       <solution>solution07</solution>
       <solution>solution08</solution>
     </solutions>
   </client>
  <clients>
 <core>

我有很多的客户,应用,烙印,解决方案。每个客户端只能有1个应用程序和1个品牌,但多种解决方案。

I have plenty of clients, applications, brandings, solutions. Each client can have only 1 application and 1 branding but multiple solutions.

- branding
- application 
- solutions (multiple or one) 

我可以通过读取XML &LT;&XMLProperty中GT; ,但我不知道该怎么办呢顺序(迭代),并获得相匹配的属性。

I can read XML using <xmlproperty> but I do not know how to do it sequentially (iterate) and get matching properties.

我知道这是复杂的。总结是:

I know it is complicated. The summary is:


  1. 构建脚本启动

  2. 构建脚本buildpackage.txt提取数据client.name和server.name

  3. 构建脚本步骤2作为参数可获得 $ {client.name} 并通过structure.xml循环。它找到匹配的品牌/应用/解决方案,并将其输出为每个匹配的客户端。

  1. build script starts
  2. build script extracts data from buildpackage.txt as client.name and server.name
  3. build script gets ${client.name} from step 2 as a parameter and loops through structure.xml. It finds matching branding/application/solutions and outputs it for each matching client.

输出:

server.name
client.name
branding.name
application name
solution [X]
solution [y]

蚂蚁具有用于每个服务器/客户端以进行处理。我可以做构建的休息和编译,如果我有属性。我不能碰structure.xml。我可以做所有我需要/想buildpackage.cml和buildpackge.txt。 Buildpackage.txt可以通过.xml文件或任何文件(例如的.properties)所取代。它必须包含这些2个值服务器和客户端。

Ant has to be processed for each server/client. I can do the rest of the build and compile if I have the properties. I can not touch structure.xml. I can do all I need/want to buildpackage.cml and buildpackge.txt. Buildpackage.txt could be replaced by .xml file or any file (for example .properties). It has to contain those 2 values server and client.

感谢您的帮助。

现在我有一个问题。
如果我把相同的应用2倍到构建文件它建立只有第一个。
这里是我的的build.xml 文件

Now i have a problem. If I put same application 2x into a buildfile it builds only first one. Here is my build.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project name="DoTheJob" basedir="." xmlns:ac="antlib:net.sf.antcontrib" xmlns:if="ant:if" xmlns:unless="ant:unless">
    <!-- Define classpath and default locations-->
    <property name="root.dir" location="../.."/>
    <property name="dest.dir" location="packages"/>
    <property name="ant.lib.dir" location="../lib"/>
    <property name="repository_url" value="http://repository-url"/> 
    <path id="project.classpath">
        <fileset dir="${ant.lib.dir}">
        <include name="*.jar"/>
        </fileset>
        <fileset dir="${root.dir}/IIT/lib">
            <include name="*.jar"/>
        </fileset>
        <pathelement location="../savedjar/compiled.jar"/>
    </path>
    <path id="svnant.path">
        <fileset dir="../lib/">
            <include name="svnant.jar"/>
            <include name="svnClientAdapter.jar"/>
        </fileset>
    </path>
    <typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svnant.path" /> 
    <tstamp>
        <format property="build_time" pattern="YYYY MMMM dd HH:mm:ss"/>
    </tstamp>   
    <!-- Change characters to lowercase -->
    <scriptdef language="javascript" name="lowercase">
        <attribute name="string" />
        <attribute name="to" />
        project.setProperty(attributes.get("to"),attributes.get("string").toLowerCase());
    </scriptdef>
    <!-- Import XMLTask for XML transformations-->
    <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpathref="project.classpath"/>
    <!-- Define buildfiles locations-->
    <property name="structure.file" location="../structure.xml"/>
    <property name="buildpackage.file" location="buildpackage.txt"/>
    <!-- load client/server file-->
    <property file="${buildpackage.file}"/>
    <!-- loop over all clients --> 
    <xmltask source="${structure.file}">
    <call path="//client">
        <param path="name/text()" name="client"/>
        <param path="branding/text()" name="branding"/>
        <param path="applications/application/text()" name="application"/>
            <actions>
                <!-- loop through all XML properties only when @{client} condition is matched --> 
                <ac:if>
                    <isset property="@{client}"/>
                    <then>
                        <echo>=====================================================</echo>
                        <echo>Building package for @{client} on ${@{client}}       </echo>
                        <echo>=====================================================</echo>
                        <!-- creating destination directories -->                           
                        <delete dir="${build.dir}/@{client}"/>
                        <mkdir dir="${dest.dir}/@{client}"/> 
                        <echo>=====================================================</echo>
                        <echo>Copy application files code/temaplates to destination</echo>
                        <echo>=====================================================</echo>                      
                        <!-- creating code build directory -->
                        <mkdir dir="${dest.dir}/@{client}/code"/> 
                        <!-- copy application code --> 
                        <echo>copy application code</echo>
                        <copy todir="${dest.dir}/@{client}/code/application/@{application}" overwrite="yes">
                            <fileset dir="${root.dir}/client-source/application/@{application}" includes="**/*.*"/>
                        </copy>
                        <!-- copy application templates --> 
                        <mkdir dir="${dest.dir}/@{client}/app/@{client}"/> 
                        <echo>copy application templates</echo>
                        <copy todir="${dest.dir}/@{client}/app/@{client}" overwrite="yes">
                            <fileset dir="${root.dir}/applications/apps/@{application}" includes="**/*.*"/>
                        </copy>
                        <echo>===================================================</echo>
                        <echo>copy Solutions files code/temaplates to destination</echo>
                        <echo>===================================================</echo>    
                        <!-- nested xmltask for loop over solutions -->
                        <xmltask source="${structure.file}">
                            <call path="/core/clients/client[name/text()='@{client}']/*/solution">
                                <param path="text()" name="solution"/>
                                    <actions>
                                        <!-- making sure that solution is lowercase -->
                                        <lowercase string="@{solution}" to="solution.lowercase" />
                                        <echo>copy solutions code</echo>
                                        <!-- copy solutions code -->
                                        <copy todir="${dest.dir}/@{client}/code/solutions/${solution.lowercase}" overwrite="yes">
                                            <fileset dir="${root.dir}/client-source/solutions/${solution.lowercase}" includes="**/*.*"/>
                                        </copy>
                                        <!-- copy solutions templates -->
                                        <echo>copy Solutions templates</echo>
                                        <copy todir="${dest.dir}/@{client}/app/@{client}" overwrite="yes">
                                            <fileset dir="${root.dir}/applications/solutions/${solution.lowercase}" includes="**/*.*"/>
                                        </copy>
                                    </actions>
                            </call>
                        </xmltask>
                        <echo>==============================================</echo>
                        <echo>copy Client files code/temaplates to destination</echo>
                        <echo>==============================================</echo> 
                        <!-- copy branding directory --> 
                        <copy todir="${dest.dir}/@{client}/app/@{client}" overwrite="yes">
                            <fileset dir="${root.dir}/applications/brandings/@{branding}" includes="**/*.*"/>
                        </copy>                         
                        <!-- copy client code --> 
                        <copy todir="${dest.dir}/@{client}/code/client/@{client}" overwrite="yes">
                            <fileset dir="${root.dir}/client-source/client/@{client}" includes="**/*.*"/>
                        </copy>                         
                        <!-- set templates directory --> 
                        <copy todir="${dest.dir}/@{client}/app/@{client}" overwrite="yes">
                            <fileset dir="${root.dir}/applications/clients/@{client}" includes="**/*.*"/>
                        </copy>         
                        <echo>==============================================</echo>
                        <echo>compiling code                                </echo>
                        <echo>==============================================</echo> 
                        <!-- making classes directory --> 
                        <mkdir dir="${dest.dir}/@{client}/classes"/>
                        <javac destdir="${dest.dir}/@{client}/classes" classpathref="project.classpath" debug="on" fork="true" includeantruntime="false" memoryinitialsize="256m" memorymaximumsize="1024m">
                            <src path="${dest.dir}/@{client}/code/"/>
                        </javac>
                        <echo>==============================================</echo>
                        <echo>creating Client.jar                           </echo>
                        <echo>==============================================</echo> 
                        <!-- jar classes directory --> 
                        <jar jarfile="${dest.dir}/@{client}/app/Client.jar" basedir="${dest.dir}/@{client}/classes"/>
                        <!-- reaplce text in files --> 
                        <replaceregexp byline="true">
                            <regexp pattern="@ReleaseDate@"/>
                            <substitution expression="${build_time}"/>
                            <fileset dir="${dest.dir}/@{client}/app/@{client}/templates/">
                                <include name="*.htm"/>
                                <include name="*.html"/>            
                                <include name="*.ini"/>
                            </fileset>
                        </replaceregexp>
                        <!-- get svn info --> 
                        <svn username="********" password="************" javahl="false" svnkit="false">
                            <info target="${repository_url}" />
                        </svn>
                        <!-- creating Client.ini file --> 
                        <touch file="${dest.dir}/@{client}/app/Client.ini"/>
                        <concat destfile="${dest.dir}/@{client}/app/Client.ini" append="true">releaseDate=${build_time}${line.separator}</concat>
                        <concat destfile="${dest.dir}/@{client}/app/Client.ini" append="true">svnrevision=${svn.info.lastRev}${line.separator}</concat>
                        <!-- ziping package --> 
                        <zip destfile="${dest.dir}/@{client}/@{client}.zip" basedir="${dest.dir}/@{client}/app/" />
                        <echo>==============================================</echo>
                        <echo>sending @{client}.zip to the server via scp      </echo>
                        <echo>==============================================</echo> 
                        <!-- scp file to remote server -->
                        <property name="scp_username" value="**********"/>
                        <property name="scp_password" value="**********"/>
                        <property name="scp_server" value="${@{client}}"/>
                        <property name="scp_remote_directory" value="*********"/>
                        <scp trust="true" verbose="true" localFile="${dest.dir}/@{client}/@{client}.zip" remoteTodir="${scp_username}:${scp_password}@${scp_server}:${scp_remote_directory}"/>  
                        <echo>${line.separator}${line.separator}${line.separator}</echo>
                    </then>
                </ac:if> 
            </actions>
        </call>
    </xmltask>
</project>

如果我尝试哟构建 buildpackage.txt 文件

client1=server1
client2=server1
client1=server2

我做得到
client1-> server1的,client2-> server1的,但是......没有客户端服务器2 1到

I do get client1->server1, client2->server1 but ...... no client 1to server2

所有固定。
对于SCP任务的解决方案是:

All fixed. The solution for scp task is:

       <!-- scp file to remote server -->
        <property name="scp_username" value="XXX"/>
        <property name="scp_password" value="YYY"/>
        <property name="scp_remote_directory" value="path"/>
        <for list="${@{client}}" delimiter="=" param = "val">
            <sequential>
            <property name="token" value="@"/>
            <scp trust="true" verbose="true" localFile="${dest.dir}/@{client}/@{client}.zip" remoteTodir="${scp_username}:${scp_password}${token}@@{val}:${scp_remote_directory}"/> 
        </sequential>
        </for>

所有的作品。
感谢您的帮助。

All works. Thank you for your help

推荐答案

有关的任​​何xmldriven构建我建议使用ant插件的 xmltask ,所以像:结果(假设clientnames在buildpackage.txt和structure.xml相似 - 你的片段有好几个名字Client_01,客户端1 ..!?)

For any xmldriven build i recommend using the ant addon xmltask, so something like :
(assuming that clientnames are similar in buildpackage.txt and structure.xml - your snippet has several names Client_01, client1 .. !?)

<project>
 <!-- Import XMLTask -->
 <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>

 <!-- make buildpackage.txt a valid propertyfile to get the job done
      f.e. server1,client1 will get client1=server1 etc. ..
      to make server easily accessible within xml loop
 -->
 <replaceregexp
   file="buildpackage.txt"
   match="(.+),(.+)"
   replace="\2=\1"
   byline="true" />

 <!-- now load that file -->
 <property file="buildpackage.txt"/>

 <!-- loop over all clients --> 
 <xmltask source="structure.xml">
  <call path="//client">
   <param path="name/text()" name="client"/>
   <param path="branding/text()" name="branding"/>
   <param path="applications/application/text()" name="application"/>

   <!-- inside action adress params with @{..} syntax ! -->
   <actions>
    <!-- the copy (or whatever) action
         here demonstrated with echoxml -->
   <echoxml>
    <!-- access the corresponding server (set via buildpackage.txt)
         with nested ${@{..}} syntax ! -->
    <copy todir="//${@{client}}/share">
     <fileset dir="@{client}/somedir"/>
    </copy>
   </echoxml>

   <echo>${@{client}}${line.separator}@{client}${line.separator}@{application}${line.separator}@{branding}</echo>

   <!-- another nested xmltask for loop over solutions -->
   <xmltask source="structure.xml">
    <call path="//client[name/text()='@{client}']/*/solution">
     <param path="text()" name="solution"/>
      <actions>
       <echo>@{solution}${line.separator}</echo>
      </actions>
     </call>
   </xmltask>

   </actions>
  </call>
 </xmltask>
</project>

如果有可能,你应该创建buildpackage.txt为有效propertyfile结果
与格式=>客户端[X] =服务器[X]的时候了。结果然后,所有你需要的是xmlt​​ask。把你所有的步骤来进行到嵌套操作部分与echoxml /复印展示搜索结果输出:结果

If possible you should create buildpackage.txt as valid propertyfile
with the format => Client[x]=Server[x] right away.
Then all you need is the xmltask. Put all your steps to be done into the nested action section as demonstrated with echoxml/copy.

Output :

<?xml version="1.0" encoding="UTF-8"?>
<copy todir="//server1/share">
  <fileset dir="client1/somedir" />
</copy>
     [echo] server1
     [echo] client1
     [echo] application_03
     [echo] brand_01
     [echo] solution01
<?xml version="1.0" encoding="UTF-8"?>
<copy todir="//server4/share">
  <fileset dir="client4/somedir" />
</copy>
     [echo] server4
     [echo] client4
     [echo] application_03
     [echo] brand_09
     [echo] solution01
     [echo] solution07
     [echo] solution08
BUILD SUCCESSFUL

- 有关条件操作的问题后编辑,仅当$ {@ {客户}}设置 -

如果propertyfile buildpackage.txt包含错误的性质,意味着服务器=客户端而不是客户端=服务器,F.E. :结果

if propertyfile buildpackage.txt contains wrong properties, means server=client instead of client=server, f.e. :

Sol-e-45-D=PRODUCTION1
CONNECTION=PRODUCTION3
PRODUCTION4=DFHH
PRODUCTION5=MEGA-5

您需要一些如果使用isset逻辑。最好的是,如果你使用新的if /除非功能(带ant191介绍)重新蚂蚁> = 1.9.3,FE

you need some if isset logic. The best is to use the new if/unless feature (introduced with ant191) if you're on ant >= 1.9.3, f.e. :

<!-- you need to activate that feature via namespaces -->
<project
  xmlns:if="ant:if"
  xmlns:unless="ant:unless"
>

 <!-- Import XMLTask -->
 <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>

 <!-- load propertyfile with client=server mappings -->
 <property file="buildpackage.txt"/>

 <!-- loop over all clients -->
 <xmltask source="structure.xml">
  <call path="//client">
   <param path="name/text()" name="client"/>
   <param path="branding/text()" name="branding"/>
   <param path="applications/application/text()" name="application"/>

   <!-- inside action adress params with @{..} syntax ! -->
   <actions>
    <!-- the copy (or whatever) action
         here demonstrated with echoxml -->
   <echoxml if:set="@{client}">
    <!-- access the corresponding server (set via buildpackage.txt)
         with nested ${@{..}} syntax ! -->
    <copy todir="//${@{client}}/share">
     <fileset dir="@{client}/somedir"/>
    </copy>
   </echoxml>

   <echo if:set="@{client}">${@{client}}${line.separator}@{client}${line.separator}@{application}${line.separator}@{branding}</echo>

   <!-- another nested xmltask for loop over solutions -->
   <xmltask source="structure.xml">
    <call path="//client[name/text()='@{client}']/*/solution">
     <param path="text()" name="solution"/>
      <actions>
       <echo if:set="@{client}">@{solution}${line.separator}</echo>
      </actions>
     </call>
   </xmltask>

   </actions>
  </call>
 </xmltask>
</project>

请注意=>在xmltask通话也有一个,如果属性,但CLIENTNAME不知道开始循环的时候,不能使用&LT;调用路径=//客户端如果=@ {}客户&GT;
结果输出:结果

Note => the xmltask call has also an if attribute, but the clientname is not known when starting the loop, can't use <call path="//client" if="@{client}">
output :

<?xml version="1.0" encoding="UTF-8"?>
<copy todir="//PRODUCTION1/share">
  <fileset dir="Sol-e-45-D/somedir" />
</copy>
     [echo] PRODUCTION1
     [echo] Sol-e-45-D
     [echo] Sol-e-45-D
     [echo] Branding-BLUE
     [echo] Sol-e-45-D
<?xml version="1.0" encoding="UTF-8"?>
<copy todir="//PRODUCTION3/share">
  <fileset dir="CONNECTION/somedir" />
</copy>
     [echo] PRODUCTION3
     [echo] CONNECTION
     [echo] Sol-e-45-D
     [echo] Branding-BLUE
     [echo] Sol-e-45-D

当结合到蚂蚁&LT

否则= 1.9.3如果任务中,你可以使用antcontrib,因为你已经在你的代码片段的taskdef:结果

Otherwise when bound to ant <= 1.9.3 you may use antcontrib if task, as you're already have that taskdef in your snippet :

<if>
 <isset property="@{client}"/>
  <then>
   <!-- ... -->
  </then>
</if> 

当心,您antcontrib的taskdef定义是错误 !!时,antcontrib.properties只包含蚂蚁&LT任务定义; 1.6,声明它像(假设antcontrib.jar已经在路径):

Beware, your antcontrib taskdef definition is wrong !!, the antcontrib.properties contains only taskdefs for ant < 1.6, declare it like that (assuming antcontrib.jar already on path) :

  <project xmlns:ac="antlib:net.sf.antcontrib">

    <ac:if>
    ...

或没有命名空间:结果

  <project>

  <!-- Import AntContrib -->
  <taskdef resource="net/sf/antcontrib/antlib.xml"/>
  ...

这篇关于Ant任务 - 多个文件 - 通过循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆