2

<j:set var="maxEachOSTime" value="0" />
<j:if test="${xmlval &gt; maxEachOSTime}">
    ${xmlval} &gt; ${ maxEachOSTime}
    <j:set var="maxEachOSTime" value="${xmlval}"/>
    <j:set var="finalTotalDuration" value="${total_duration}"/>
</j:if>

if ${xmlval} is compared with a number ( 1, 200, etc) it works. But when compared with ${eachMaxOSTime} it doesnt work. (condition is always working as true) .intValue() also didnt help. What is the solution for this?

Azee77
  • 100
  • 2
  • 8

2 Answers2

2

The following structure is used to define a string variable with Jelly script:

<j:set var="stringVariable"  value="This is a string!"/>
<j:set var="maxEachOSTime"   value="0"/>

Therefore, the type of the maxEachOSTime variable is string. You are trying to equate an integer on the left side of the equation with a string using the escape character &gt;.

Sercan
  • 4,739
  • 3
  • 17
  • 36
  • How do I declare an int value? the variable (xmlval) is fetched by reading an xml Eg: duration_int="45" basically I want to find max number after reading multiple xmls (if you have any other workable/simple solution, I'd accept that too – Azee77 Dec 19 '21 at 04:03
  • When applying the `toInteger()` method to the String variable, chances are the comparison will work successfully. Would you try: `` – Sercan Dec 19 '21 at 04:26
  • the condition is getting bypassed with above change (same happens with intValue() too) – Azee77 Dec 19 '21 at 04:41
  • 2
    I thought that in the comparison in the above `` block, the left side is an `int` but the right side is not working because it is a `string`. That's why I thought it might work when he applies the `toInteger()` method to the string variable on the right side of the equation. If you want to use advanced features using variables, you need to do these operations in the **pipeline**. The keyword you should search for is `"Jenkins Pipeline: How To Define a Variable"`. – Sercan Dec 19 '21 at 04:47
  • 1
    I was avoiding to read multiple xml files in jenkins pipeline. But if we see no other option, I will try that out, thanks – Azee77 Dec 19 '21 at 04:51
  • 1
    It worked from jelly itself, your above comments helped me to google for correct context!! – Azee77 Dec 19 '21 at 05:51
  • 1
    I've never worked with Jelly before. I just searched to help you. – Sercan Dec 19 '21 at 05:53
1

This is what that finally helped me in my specific use case.

xmlval is variable which has integer value by reading from xml file

maxEachOSTime var was set as string by default via <j:set>

Converted that into float and used its intValue()

Earlier intValue() was not working as String didnt have attribute intValue, hence the if condition was getting skipped.

This can be improvised by working directly into integers

<j:new className="java.lang.Float" var="tempFloat">
    <j:arg value="${maxEachOSTime}" type="float"/>
</j:new>
<j:set var="intThing" value="${tempFloat.intValue()}"/>
<j:if test="${xmlval &gt; intThing}">
    <j:set var="maxEachOSTime" value="${xmlval}"/>
    <j:set var="finalTotalDuration" value="${total_duration}"/>
</j:if>
Azee77
  • 100
  • 2
  • 8