converting pom.xml version to number format

173 views Asked by At

I have a requirement to increment to read and increment the pom.xml version by 1 using powershell script.

I was able to fetch the version value as for example: 1.0.123, but the type given here is string, when I try to convert it into Decimal or Double I am getting below error:

Code:

PS C:\Users\XXXX\Downloads> $finale
1.0.153

PS C:\Users\XXXX\Downloads> $finale.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                      
-------- -------- ----                                     --------                                                                                                      
True     True     String                                   System.Object 

Error:

PS C:\Users\XXXX\Downloads> $finale1 = [Double]::Parse($finale)

Exception calling "Parse" with "1" argument(s): "Input string was not in a correct format." At line:1 char:1 + $finale1 = [Double]::Parse($finale) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FormatException

2

There are 2 answers

1
lit On BEST ANSWER

Using a [version] type is nice, but it is immutable. This code splits it into an array, increments the third (Build) number, and produces a string in $newfinale.

Note that this does not check to see if there is a third (Build) value. It will produce an exception if the $finale is '1.2'.

PS C:\> $finale = '2.3.4.5'
PS C:\> $a = $finale.split('.')
PS C:\> $a[2] = [int]$a[2] + 1
PS C:\> $newfinale = $a -join '.'
PS C:\> $newfinale
2.3.5.5
0
Leon Evans On

The reason is 1.0.123 is not math. It is nether an integer, nor a double. It is simply a string that contains numbers and symbols. This is why you are getting the error.

See the following Help files: About_Arithmetic_Operators .NET Math Class