0

I have a question based from this question Replacing characters in Ant property

I want to build a variable (i can't use a property because i'm in a loop) that is pretty much StringA - StringB. (maybe this is a misunderstanding of properties on my part but they can only be assigned once correct?)

I guess I could build a script function to calculate that, but my guess is that it must be possible to do it in an already existing function, probably something i'm missing.

this would be an example of the code

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

in general I want to deploy several wars. this works fine when I run it once but if I want to make it re-runnable I need to undeploy them first.

I'm just a consumer of this interfaces, Ideally deployToLiferay would auto undeploy but it does not.

thanks for an feedback

edit: if I use something similar to what is define on the linked page i get:

<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

line 178 is my loadresource part

Community
  • 1
  • 1
Miguel Costa
  • 627
  • 1
  • 12
  • 30

1 Answers1

1

ANT is not a programming language. Personally I'd recommend embedding a scripting language like Groovy to process a group of files:

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

Example

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

Example

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

Update

The following working example shows how Apache ivy can be used to manage build dependencies. This is a capability that exists in other Java build tools like 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>
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • thanks for the feedback, I know ANT is not a programming language but I want to script my processes without that much of external tools and I really want to use those interfaces because they are "official" API's from the product I use. I don't mind using a script language like javascript if needed but at this point I really just want to build a String based on the other strings so I can do whatever i feel like – Miguel Costa Jan 25 '17 at 02:55
  • @MiguelCosta You already have external tools, for example ant-contrib which is not part of standard ANT ("for" task). The approach you take is completely up to you, my advice is to consider using Groovy or Javascript when you need to do programming. Groovy especially has really nice integration with ANT. Groovy was actually the inspiration for Gradle a more modern java build tool. Finally I have expanded my answer to show how build dependencies can be managed as part or your ANT script using the ivy plugin – Mark O'Connor Jan 25 '17 at 21:47
  • I understand that i already use some external tools but those are inherit from my project, groovy is not and i don't really want to include it unless i have to. – Miguel Costa May 08 '17 at 06:56