I have the following string called MasterVersion:
1.1-SNAPSHOT
I need to split it by the . and the - so it becomes a string[] array called SplitVersion, i.e.:
1
1
SNAPSHOT
I've tried everything I can think of including about a dozen variations of the below, with no joy:
<!-- doesn't work -->
<ItemGroup>
<VersionDelimiters Include="." />
<VersionDelimiters Include="-" />
<SplitVersion Include="$(MasterVersion.Split(@VersionDelimiters))" />
</ItemGroup>
<!-- doesn't work either -->
<ItemGroup>
<SplitVersion Include="$(MasterVersion.Split([`.`; `-`]))" />
</ItemGroup>
What obscure MSBuild syntax am I missing/mucking up?
MSBuild 4.0 property functions cannot handle arrays (well basically), however when you do a
You are invoking the
String.Split(params string[])overload, which requires an array (even in C# theparamskeyword will create an array behind the scene and do something likeSplit(new string[] { ',', '-' })internally).What you could do is the following:
Or you could first create the (string) array to be passed to Split:
Which is not really better in this case ;-)
Oh, and you might want to check out this MSDN blog entry for more useful information.
Update for a comment:
The "content" of
SplitVersionis technically an "array of ITaskItem", yes. You would deal with it however you would deal with Items (of ItemGroups); including things like "batching", etc.You cannot really "access things by index" in msbuild project files. Expressions like
$(SplitVersion)[0]or@(SplitVersion)[0]or@(SplitVersion[0])don't do what you'd think/like. If you really would to you could assign individual properties for "array elements" by "index".Example:
The array-indexing operator works here, because in this case you are still "in the context" of the .NET expression. Once that is assigned to a property (or item group) you cannot do that anymore.