2

In Ant, how can I test if a property ends with a given value?

For example

 <property name="destdir" 
     value="D:\FeiLong Soft\Essential\Development\repository\org\springframework\spring-beans" />

how can I test if ${destdir} ends with "spring-beans"?

additional:

In my ant-contrib-1.0b3.jar, without 'endswith' task~~

feilong
  • 721
  • 1
  • 12
  • 25

4 Answers4

4

You can test if ${destdir} ends with "spring-beans" like this (assuming you have ant-contrib, and are using Ant 1.7 or later).

<property name="destdir" value="something\spring-beans" />
<condition property="destDirHasBeans">
  <matches pattern=".*spring-beans$" string="${destdir}" /> 
</condition>
<if>
  <equals arg1="destDirHasBeans" arg2="true" />
  <then>
      $destdir ends with spring-beans ...
  </then>
  <else> ...
  </else>
</if>

The '$' in the regex pattern ".*spring-beans$" is an anchor to match at the end of the string.

Tim
  • 966
  • 1
  • 13
  • 22
3

As Matteo, Ant-Contrib contains a lot of nice stuff, and I use it heavily.

However, in this case can simply use the <basename> task:

<basename property="basedir.name" file="${destdir}"/>
<condition property="ends.with.spring-beans">
   <equals arg1="spring-beans" arg2="${basedir.name}"/>
<condition>

The property ${ends.with.spring-beans} will contain true if ${destdir} ends with string-beans and false otherwise. You could use it in the if or unless parameter of the <target> task.

Community
  • 1
  • 1
David W.
  • 105,218
  • 39
  • 216
  • 337
  • This works for testing if the last component of a path equals "spring-beans". It will not work to test if a property strictly ends with that substring. – luis.espinal Apr 04 '17 at 17:53
0

You can use the EndWith condition from Ant-Contrib

<endswith string="${destdir}" with="spring-beans"/>

For example

<if>
    <endswith string="${destdir}" with="spring-beans"/>
    <then>
        <!-- do something -->
    </then>
</if>

Edit

<endswith> is part of the Ant-Contrib package that has to be installed and enabled with

<taskdef resource="net/sf/antcontrib/antlib.xml"/>
Matteo
  • 14,696
  • 9
  • 68
  • 106
  • 1
    https://sourceforge.net/projects/ant-contrib/forums/forum/113701/topic/3635945?message=8209729 I have the same questions and this – feilong Nov 30 '12 at 18:38
  • 2
    Does not work! got error message "if doesn't support the nested "endswith" element." – SJunejo Feb 06 '14 at 11:17
  • @SJunejo did you install and enable ant-contrib? Which version of ants contrib are you using? – Matteo Feb 06 '14 at 14:08
  • I am using ant-contrib1.0b3 in my project. I have solved it by writing my own endswith as javascript and called but offcourse not inside 'if' :( – SJunejo Feb 07 '14 at 15:22
  • See http://stackoverflow.com/questions/13670197/use-ant-contrib-how-to-use-endswith – aliteralmind Jan 05 '15 at 00:34
0

The JavaScript power can be used for string manipulation in the ANT:

<script language="javascript"> <![CDATA[

        // getting the value for property sshexec.outputproperty1
        str = project.getProperty("sshexec.outputproperty1");

        // get the tail , after the separator ":"
       str = str.substring(str.indexOf(":")+1,str.length() ).trim();

        // store the result in a new property
        project.setProperty("res",str);


    ]]> </script>
<echo message="Responce ${res}" />
DejanR
  • 441
  • 4
  • 11