1
votes

I'm reading a file that contains a list of files path.
For each file, I would like to know if it contains a substring. the answer is always false, although part of it should be true.
Here is my target:

<target name="chek-file">
    <loadfile property="file" srcfile="c:\tmp\testing.txt"/>
    <for param="line" list="${file}" delimiter="${line.separator}">
        <sequential>
            <echo>@{line}</echo>
            <loadfile property="inner_file" srcfile="@{line}"/>      
            <if>
                <resourcecontains resource="${inner_file}" substring="parent" />
                <then>
                    <echo message="this is a jpa jar"/>
                </then>
                <else>
                    <echo message="this is NOT a jpa jar"/>
                </else>
            </if>
        </sequential>
    </for>
</target>

the echo is typing "this is NOT a jpa jar" for all jars.
Is 'if' not working with 'resourcecontains'?

1
scripts looks okay. just check about the file and substring. look herentshetty
I added echo to the 'loadfile' property, I can see the substring in the text (it is a property in a pom file) but still, the condition is marked as false. I saw the answer you suggested, I wish to get the task with if condition and not with additional targets. can this be done?soninob

1 Answers

1
votes

OK, found two problems:
1) The second load file is actually not needed here because resourcecontains needs to get the file name and not its value.
2) resourcecontains is inherited from <condition>
so the solution should be:

<target name="chek-file">
        <loadfile property="file" srcfile="c:\tmp\testing.txt"/>
        <for param="line" list="${file}" delimiter="${line.separator}">
            <sequential>
                <echo>@{line}</echo>
                <condition property="substring_found">
                    <resourcecontains resource="@{line}" substring="JPA>true" />
                </condition>
                <echo message="substring_found value: ${substring_found}"/>
                <if>
                    <equals arg1="${substring_found}" arg2="true" />
                    <then>
                        <echo message="this is a jpa jar"/>
                        <get_jar_name_no_version property.to.process="@{line}" output.property="linetobeadd" />
                    </then>
                    <else>
                        <echo message="this is NOT a jpa jar"/>
                    </else>
                </if>
                <var name="substring_found" unset="true"/>
            </sequential>
        </for>
    </target>