我有相当复杂的蚂蚁任务要写。我不得不制作单独的构建文件,剩下 1 个。有4个文件:
buildpackage.bat, buildpackage.xml, buildpackage.txt, structure.xml
buildpackage.bat 只是一个调用buildpackage.xml "ant -f buildpackage.xml"
. 那也行。
buildpackage.txt 很简单,并且有逗号分隔的行:
server1,client1
server2,client2
genericserver,client[x]
server[x],client[x]
总是有服务器名称和客户端名称:
Server name
只能是字母、数字和字母,但长度可变。Client name
- 仅限字母。
我需要将这些文件拆分为 2 个单独的变量:server.name
和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>
我得到的结果是:
[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 为 parameter1,client_02 为 parameter2。
我将按顺序执行其他任务。我会将文件从 client[x] 目录复制到 server[x] (这很简单,但是如何将该字符串拆分为 2 个单独的变量?)。
现在是非常复杂的任务。
我也有 structure.xml 文件。结构是:
<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 个品牌,但可以有多个解决方案。
基于先前的顺序任务(获取服务器和名称),我必须遍历该 xml 文件并获取每个匹配的客户端(来自先前的任务)
- branding
- application
- solutions (multiple or one)
我可以使用 XML 读取,<xmlproperty>
但我不知道如何按顺序(迭代)并获得匹配的属性。
我知道这很复杂。总结是:
- 构建脚本开始
- 构建脚本从 buildpackage.txt 中提取数据作为 client.name 和 server.name
- 构建脚本
${client.name}
从第 2 步获取作为参数并循环通过 structure.xml。它找到匹配的品牌/应用程序/解决方案并将其输出给每个匹配的客户。
输出:
server.name
client.name
branding.name
application name
solution [X]
solution [y]
必须为每个服务器/客户端处理 Ant。如果我有这些属性,我可以完成其余的构建和编译。我无法触摸结构.xml。我可以做所有我需要/想要的 buildpackage.cml 和 buildpackge.txt。Buildpackage.txt 可以替换为 .xml 文件或任何文件(例如 .properties)。它必须包含这两个值服务器和客户端。
谢谢您的帮助。
编辑
现在我有一个问题。如果我将相同的应用程序 2x 放入构建文件中,它只会构建第一个。这是我的build.xml
文件
<?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,但是......没有client1到server2
编辑2
都修好了。scp任务的解决方案是:
<!-- 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>
所有作品。谢谢您的帮助