在Ant中,如何测试属性是否以给定值结尾?

在Ant中,如何测试属性是否以给定值结尾?,ant,ant-contrib,Ant,Ant Contrib,在Ant中,如何测试属性是否以给定值结尾 比如说 <property name="destdir" value="D:\FeiLong Soft\Essential\Development\repository\org\springframework\spring-beans" /> <if> <endswith string="${destdir}" with="spring-beans"/> <then>

在Ant中,如何测试属性是否以给定值结尾

比如说

 <property name="destdir" 
     value="D:\FeiLong Soft\Essential\Development\repository\org\springframework\spring-beans" />
<if>
    <endswith string="${destdir}" with="spring-beans"/>
    <then>
        <!-- do something -->
    </then>
</if>

如何测试
${destdir}
是否以
“Springbeans”
结尾

附加:

在我的ant-contrib-1.0b3.jar中,如果没有'endswith'任务~~~

您可以从


比如说

 <property name="destdir" 
     value="D:\FeiLong Soft\Essential\Development\repository\org\springframework\spring-beans" />
<if>
    <endswith string="${destdir}" with="spring-beans"/>
    <then>
        <!-- do something -->
    </then>
</if>

编辑

是Ant Contrib软件包的一部分,必须使用安装和启用

<taskdef resource="net/sf/antcontrib/antlib.xml"/>

就像Ant Contrib一样,它包含了很多好东西,我大量使用它

但是,在这种情况下,您可以简单地使用
任务:

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


属性
${ends.with.spring beans}
将包含
true
如果
${destdir}
string beans
结束,否则
将包含
false
。您可以在
任务的
if
参数中使用它。

您可以测试${destdir}是否以这样的“Springbeans”结尾(假设您有ant contrib,并且正在使用ant 1.7或更高版本)


$destdir以春豆结尾。。。
...

regex模式
“*springbeans$”
中的“$”是字符串末尾匹配的锚。

JavaScript功能可用于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}" />



我有同样的问题,但这不起作用!收到错误消息“if不支持嵌套的“endswith”元素。”@SJunejo是否安装并启用了ant contrib?您使用的是哪个版本的ants contrib?我在项目中使用的是ant-contrib1.0b3。我已经解决了这个问题,用javascript编写了自己的endswith,并调用了'if',当然不是在'if'中调用的(请参阅此测试,以测试路径的最后一个组件是否等于“SpringBeans”)。测试属性是否严格以该子字符串结尾是行不通的。