msbuild - How to pass ItemGroup with metadata between targets? -
from understanding, using dependsontargets
necessary pass itemgroup between targets. not sure if there other ways pass targets without dependsontargets
.
i have tested itemgroup cannot pass calltarget
or msbuild
task. workaround solution convert itemgroup property (flatten it) , use properties
pass over.
i define itemgroup
of file
. file
has value
metadata. execute target , remove 1 file
item each recursive loop. here script:
<target name="mygroup"> <itemgroup> <file include="5"> <value>5a</value> </file> <file include="4"> <value>4a</value> </file> <file include="3"> <value>3a</value> </file> <file include="2"> <value>2a</value> </file> <file include="1"> <value>1a</value> </file> </itemgroup> </target> <target name="recursive" dependsontargets="mygroup" condition="$(value) > 0"> <itemgroup> <file remove="$(value)" /> </itemgroup> <propertygroup> <value>$([msbuild]::subtract($(value), 1))</value> </propertygroup> <message text="file: @(file->'%(value)') value=$(value)" importance="high" /> <msbuild projects="$(msbuildprojectfile)" targets="recursive" properties="value=$(value)"/> </target> <target name="build" dependsontargets="mygroup"> <msbuild projects="$(msbuildprojectfile)" targets="recursive" properties="value=5" /> </target>
the output is:
file: 4a;3a;2a;1a value=4 file: 5a;3a;2a;1a value=3 file: 5a;4a;2a;1a value=2 file: 5a;4a;3a;1a value=1 file: 5a;4a;3a;2a value=0
i expect output be:
file: 4a;3a;2a;1a value=4 file: 3a;2a;1a value=3 file: 2a;1a value=2 file: 1a value=1 file: value=0
is there solution?
not really, trying circumvent idea of msbuild build target once (unless inputs\outputs changes). , can't pass itemgroups between msbuild contexts (when call msbuild task - new context created). in script - itemgroup scoped target - need define globally it'll available other targets. can is:
- define global property value condition use 1 passed script if exists.
- change itemgroup mygroup target global scope.
- define conditions on each element of itemgroup emitted depending on value of value property
e.g. script this:
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <propertygroup> <value condition="$(value) == ''">5</value> </propertygroup> <itemgroup> <file include="5" condition="$(value) >5"> <value>5a</value> </file> <file include="4" condition="$(value) >4"> <value>4a</value> </file> <file include="3" condition="$(value) >3"> <value>3a</value> </file> <file include="2" condition="$(value) >2"> <value>2a</value> </file> <file include="1" condition="$(value) >1"> <value>1a</value> </file> </itemgroup> <target name="recursive" condition="$(value) >0"> <propertygroup> <value>$([msbuild]::subtract($(value), 1))</value> </propertygroup> <message text="file: @(file->'%(value)') value=$(value)" importance="high" /> <msbuild projects="$(msbuildprojectfile)" targets="recursive" properties="value=$(value)"/> </target> <target name="build"> <msbuild projects="$(msbuildprojectfile)" targets="recursive" properties="value=5" /> </target> </project>
as alexey shcherbak points out, can't pass itemgroup
msbuild
task. resolve problem introducing new property done
keep list of item has processed.
<target name="mygroup"> <itemgroup> <file include="5"> <value>5a</value> </file> <file include="4"> <value>4a</value> </file> <file include="3"> <value>3a</value> </file> <file include="2"> <value>2a</value> </file> <file include="1"> <value>1a</value> </file> </itemgroup> </target> <target name="recursive" dependsontargets="mygroup" condition="$(value) > 0" > <itemgroup> <file remove="$(value)" /> <file remove="$(done)" /> </itemgroup> <message text="file: @(file->'%(value)') value=$(value)" importance="high" /> <propertygroup> <done>$(done);$(value)</done> <value>$([msbuild]::subtract($(value), 1))</value> </propertygroup> <msbuild projects="$(msbuildprojectfile)" targets="recursive" properties="value=$(value);done=$(done)" /> </target> <target name="build" dependsontargets="mygroup"> <msbuild projects="$(msbuildprojectfile)" targets="recursive" properties="value=5" /> </target>
Comments
Post a Comment