0

My knowledge of ant is close to nothing. i do understand it should not be used as a programming language but i'm a consumer of a certain ant project and want to modify something in my own project while using the libraries that project offers me.

the main point i want to do is I have a string and need to modify it before sending it to the parent project target.

i'll try providing a code easy to understand, but at the moment the part i have left is: either store the value in a variable instead of a property (not sure how to do this) directly call the other target from my javascript function.

so this is the code:

  <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>

so my question is at the time i try to save the undeploy_name property can I just call the target deployToLiferay? if not is there a way i can save that in a variable instead a property?

i don't mind using other language instead of javascript but not really sure how can i do what i need to do.

based on the info i found in here i'm now trying to focus on the using the script directly. this is the info i get:

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

i tried to modify my script to something like this:

  <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>

called from:

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

after this modifications it now fails when i try to set setDynamicAttribute and perform that task.

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
Miguel Costa
  • 627
  • 1
  • 12
  • 30

1 Answers1

0

I don't think you need an embedded script. I reviewed the logic and I think it would be simpler to use the ANT basename task in order to obtain the file name.

Example

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

Project run as follows

$ 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>
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • hi Mark, that would solve this specific problem, but i kind of need to do also some regexp expressions in that value before using it (remove version number, replace special chars, etc...). this example was more to have an idea, if i could actually store this has a variable or if i could somehow from the script call directly the custom macros. I will try and see if i can do them directly before undeploying. – Miguel Costa May 15 '17 at 02:20
  • i guess i wanted something like this? https://coderanch.com/t/108191/call-ant-macrodef-groovy-script if not possible to store on variables – Miguel Costa May 15 '17 at 03:26
  • ANT is not a programming language so only has the concept of immutable properties, not variables. I suspect what you should do instead is embed a script that does the scanning and iteration logic for you. For example I use groovy, which excellent integration with ANT. For example: http://stackoverflow.com/questions/9255127/find-all-directories-in-which-a-file-exists-such-that-the-file-contains-a-searc/9269730#9269730 and http://stackoverflow.com/questions/15511382/preserve-original-filenames-in-groovy-ant-task/15536129#15536129 – Mark O'Connor May 15 '17 at 09:29
  • yeah with the error i just got i'm trying to do it with javascript instead of groovy because i don't know groovy to be honest. if i get time today or tomorrow i can try using groovy instead and see how it goes – Miguel Costa May 15 '17 at 10:19