ant:对变量进行字符串操作

ant: string manipulation on variable

我有一个基于这个问题的问题 Replacing characters in Ant property

我想构建一个几乎是 StringA - StringB 的变量(我不能使用 属性,因为我在循环中)。 (也许这是我对属性的误解,但它们只能在正确的情况下分配?)

我想我可以构建一个脚本函数来计算它,但我的猜测是它必须可以在一个已经存在的函数中完成,可能是我缺少的东西。

这将是代码示例

    <for param="file">
      <path>
        <fileset dir="${mydir}" >
          <include name="*.war"/>
        </fileset>
      </path>
      <sequential>
        <var name="undeploy_name" value="@{file} function_here ${mydir}" />
        <JBossCLI port="${jboss.port.management-native}">
          <undeploy namePattern="${undeploy_name}" />
        </JBossCLI>
        <deployToLiferay file="@{file}" />
      </sequential>
    </for>

总的来说我要部署几场战争。当我 运行 它一次时这工作正常但是如果我想让它重新 运行nable 我需要先取消部署它们。

我只是这个接口的使用者,理想情况下 deployToLiferay 会自动取消部署,但它不会。

感谢反馈

编辑:如果我使用类似于链接页面上定义的东西,我会得到:

<loadresource property="file-to-deploy">
  <propertyresource name="@{file}"/>
  <filterchain>
    <tokenfilter>
      <filetokenizer/>
      <replacestring from="${mydir}" to=""/>
    </tokenfilter>
  </filterchain>
</loadresource>

10:52:49.541:  * /data/contribution.xml:171: The following error occurred while executing this line:
10:52:49.541:  * /data/contribution.xml:178: null doesn't exist

第 178 行是我的加载资源部分

ANT 不是一种编程语言。我个人建议嵌入脚本语言,如 Groovy 来处理一组文件:

  <target name="process-files" depends="resolve">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

    <fileset id="wars" dir="src/wars" includes="*.war"/>

    <groovy>
      project.references.wars.each { 
        ant.echo(message: "I want to do something with this ${it} file")
      }
    </groovy>
  </target>

例子

├── build.xml
└── src
    └── wars
        ├── app1.war
        ├── app2.war
        └── app3.war

例子

process-files:
     [echo] I want to do something with this /../src/wars/app1.war file
     [echo] I want to do something with this /../src/wars/app2.war file
     [echo] I want to do something with this /../src/wars/app3.war file

更新

以下工作示例展示了如何使用 Apache ivy 来管理构建依赖项。这是其他 Java 构建工具(例如 Maven)中存在的功能。

<project name="demo" default="process-files" xmlns:ivy="antlib:org.apache.ivy.ant">

  <available classname="org.apache.ivy.Main" property="ivy.installed"/> 

  <!--
  ==================
  Normal ANT targets
  ==================
  -->

  <target name="process-files" depends="resolve">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

    <fileset id="wars" dir="src/wars" includes="*.war"/>

    <groovy>
      project.references.wars.each { 
        ant.echo(message: "I want to do something with this ${it} file")
      }
    </groovy>
  </target>


  <!--
  =============================
  Dependency management targets
  =============================
  -->
  <target name="resolve" depends="install-ivy">
    <ivy:cachepath pathid="build.path">
      <dependency org="org.codehaus.groovy" name="groovy-all" rev="2.4.7" conf="default"/>
    </ivy:cachepath>
  </target>

  <target name="install-ivy" unless="ivy.installed">
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/>
    <fail message="Ivy has been installed. Run the build again"/>
  </target>

</project>