I have a custom msbuild task that is generating some output files to the output directory ($(TargetDir)) of a ProjectA. Current code is something like this:
<MyCustomTask ...>
<Output TaskParameter="OutputFiles" ItemName="FileWrites"/>
</MyCustomTask>
A ProjectB is referencing ProjectA but the problem is that when building ProjectB, generated files by MyCustomTask are not copied to the output directory of the ProjectB.
How can we get dynamically generated additional files to be copied as part of project dependency with MSBuild?
I have finally managed to perform automatically the copy from Project B without having to modify it. IIya was not so far from the solution, but the fact is that I cannot generate statically as the list of files to generate from Project A with MyCustomTask is dynamic. After digging more into
Microsoft.Common.targets, I have found that ProjectB will get the list of output from Project A by calling the targetGetCopyToOutputDirectoryItems. This target is dependent fromAssignTargetPathswhich itself is dependent on the target list propertyAssignTargetPathsDependsOn.So in order to generate dynamically content and to get this content being copied automatically through standard project dependency, we need to hook Project A at two different places:
AssignTargetPathsDependsOnas it is called indirectly by Project B on Project A through GetCopyToOutputDirectoryItems. And also it is indirectly called by Project A whenPrepareResourceis called. Here, we are just outputing the list of files that will be generated (by Project A) or consumed by Project B. AssignTargetPathsDependsOn will call a custom taskMyCustomTaskListwhich is only responsible to output the list of files (but not to generate them), this list of files will create dynamic “Content” withCopyOutputDirectory.BuildDependsOnin order to actually generate the content in Project A. This will callMyCustomTaskthat will generate the content.All of this was setup like this in ProjectA:
This method is working with anykind of C# projects that is using Common.Targets (so It is working with pure Desktop, WinRT XAML App or Windows Phone 8 projects).