ant 在 scriptdef 中存储变量或在脚本 def 中调用任务

ant store variable in scriptdef or call task in script def

我对蚂蚁几乎一无所知。 我知道它不应该用作编程语言,但我是某个 ant 项目的消费者,并且想在使用该项目提供给我的库的同时修改我自己项目中的某些内容。

我想做的重点是我有一个字符串,需要在将它发送到父项目目标之前修改它。

我会尝试提供一个易于理解的代码,但目前我剩下的部分是: 将值存储在变量而不是 属性 中(不知道该怎么做) 直接从我的 javascript 函数调用另一个目标。

所以这是代码:

  <target name="deploy-custom" depends="init">
    <scriptdef name="replaceString" language="javascript">
      <attribute name="fileIn" />
      <attribute name="directoryFile" />
      <![CDATA[echo = project.createTask("echo");
          var fileName = attributes.get("filein"); //get attribute for scriptdef
          var directoryIn = attributes.get("directoryfile"); //get attribute for scriptdef
          echo.setMessage("file name: " + fileName );
          echo.perform( );
          echo.setMessage("dir in " + directoryIn );
          echo.perform( );
          var fileOut = fileName.replace(directoryIn, "");
          echo.setMessage("replace " + fileOut );
          echo.perform( );
          project.setProperty("undeploy_name", fileOut);]]>
    </scriptdef>
        <echo message="executing target deploy-custom" />
        <for param="file">
          <path>
            <fileset dir="${mydir}/content/custom-deploy">
              <include name="*.war" />
            </fileset>
          </path>
          <sequential>
            <replaceString fileIn="@{file}" directoryFile="${mydir}/content/custom-deploy/" />
            <JBossCLI port="${jboss.port.management-native}">
              <undeploy namePattern="${undeploy_name}" />
            </JBossCLI>
            <deployToLiferay file="@{file}" />
          </sequential>
        </for>
        <echo>ABRS custom banklets deployed!</echo>
  </target>

所以我的问题是当时我尝试保存 undeploy_name 属性 我可以只调用目标 deployToLiferay 吗?如果没有,我可以将其保存在变量中而不是 属性?

我不介意使用其他语言而不是 javascript,但不确定我该怎么做。

根据我在这里找到的信息,我现在正试图专注于直接使用脚本。这是我得到的信息:

https://coderanch.com/t/108191/call-ant-macrodef-groovy-script

我试图将我的脚本修改成这样:

  <macrodef name="undeploy">
    <attribute name="undplPattern" />
    <sequential>
      <echo message="undeploy undplPattern @{undplPattern}" />
      <JBossCLI port="${jboss.port.management-native}">
        <undeploy namePattern="@{undplPattern}" />
      </JBossCLI>
    </sequential>
  </macrodef>


<scriptdef name="undeploy-pattern" language="javascript">
  <attribute name="fileIn" />
  <attribute name="directoryFile" />
  <![CDATA[
      var echo = project.createTask("echo");
      var fileName = attributes.get("filein"); //get attribute for scriptdef
      var directoryIn = attributes.get("directoryfile"); //get attribute for scriptdef
      echo.setMessage("file name: " + fileName );
      echo.perform( );
      echo.setMessage("dir in " + directoryIn );
      echo.perform( );
      var fileOut = fileName.replace(directoryIn, "");
      fileOut = fileOut.replace(/\d+/g, "");
      fileOut = fileOut.replace("..",".*");
      fileOut = fileOut.replace(/[.]/g,"\.");
      fileOut = fileOut.replace("web-\.*\.war","web.*");
      echo.setMessage("undeploy pattern transformation: " + fileOut );
      echo.perform( );
      var undeploy_t = project.createTask("undeploy");
      undeploy_t.setDynamicAttribute("undplPattern", fileOut);
      undeploy_t.perform( );
      ]]>
</scriptdef>

呼叫方:

<echo message="item @{file}" />
<undeploy-pattern fileIn="@{file}" directoryFile="${currentScriptDirectory}/content/custom-banklets/" />
<deployToLiferay file="@{file}" />

修改后,当我尝试设置 setDynamicAttribute 并执行该任务时,它现在失败了。

08:01:18.492: item /data/com.client-dshbrd-banklet-web-0.0.1.war
08:01:18.509: file name: /data/com.client-dshbrd-banklet-web-0.0.1.war
08:01:18.510: dir in /data/
08:01:18.520: undeploy pattern transformation: com\.client-dshbrd-banklet-web.*
08:01:18.528: COMMAND 'deploy-custom-banklets' FAILED (execution time: 2 seconds)
08:01:18.528:  * /data/contribution.xml:250: The following error occurred while executing this line:
08:01:18.528:  * /data/contribution.xml:259: required attribute undplpattern not set

我认为您不需要嵌入式脚本。我回顾了逻辑,我认为使用 ANT basename task 来获取文件名会更简单。

例子

├── build.xml
└── src
    └── files
        └── file1.war

项目运行如下

$ ant

build:
     [echo] file1.war

build.xml

<project name="demo" default="build">

  <target name="build">

    <basename property="undeploy_name" file="src/files/file1.war"/>

    <echo>${undeploy_name}</echo>

  </target>

</project>