10
votes

I have a C# project that uses the Project Dependencies in a sln file to make sure that the build order is correct.

So I have in my sln file that ProjectB depends on ProjectA.

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectB", "ProjectB.csproj", "{E24EAC46-1563-4E73-9411-3F9D2645F77C}"
    ProjectSection(ProjectDependencies) = postProject
        {4A7D6720-4AA1-4F0B-A796-A0436DB3D7D7} = {4A7D6720-4AA1-4F0B-A796-A0436DB3D7D7}
    EndProjectSection
EndProject

ProjectA has some content that is set to CopyIfNewer.

When I build this with Visual studio, ProjectA goes to its own bin folder and ProjectB goes to its own bin folder.

But when I build it with MSBuild, the content of ProjectA somehow appears in the output folder in ProjectB as well!

The build log shows that [ProjectB.csproj] _CopyOutOfDateSourceItemsToOutputDirectory copies the files over.

My question is: How can I tell MSBuild that the files do not belong to that project and don't have to be copied?

As a workaround I added ProjectA as a ProjectReference with <Private>False</Private> and that seems to work, but it is not my desired solution.

2
While chasing down this same issue, it made me realize that even though the project wasn't a reference, it was set as dependent. It would build in VS as expected, but msbuild would pull in the CopyIfNewer files. For me, the dependency was not required so I removed it.DanTheMan

2 Answers

0
votes

I Solve this trouble overriding MSBuild Task in ProjectB .csproj file. (I have only dependency on ProjectA in .sln file, not adding project as ProjectReference to ProjectB)

Add this targets to your .csproj file:

<Project>
.....
<Target Name="_CopyOutOfDateSourceItemsToOutputDirectory" Condition=" '@(_SourceItemsToCopyToOutputDirectory)' != '' " Inputs="@(_SourceItemsToCopyToOutputDirectory)" Outputs="@(_SourceItemsToCopyToOutputDirectory->'$(OutDir)%(TargetPath)')">
        <Message Importance="Normal" Text="$(MSBuildProjectName) Skip copy _CopyOutOfDateSourceItemsToOutputDirectory" />
      </Target>
      <Target Name="_CopyOutOfDateSourceItemsToOutputDirectoryAlways" Condition=" '@(_SourceItemsToCopyToOutputDirectoryAlways)' != '' ">
        <Message Importance="Normal" Text="$(MSBuildProjectName) Skip copy _CopyOutOfDateSourceItemsToOutputDirectoryAlways" />
      </Target>
</Project>

For myself files, needed to copy to output directory i do it by XCOPY in postbuild like:

<PropertyGroup>
    <PostBuildEvent>xcopy "$(ProjectDir)SomeSubFolder\SomeContentFile.cfs" "$(TargetDir)SomeSubFolder\" /Y /F</PostBuildEvent>
  </PropertyGroup>

Maybe there are has smart variant of overriding _CopyOutOfDateSourceItemsToOutputDirectory* tasks like this. But my variant satisfied me now. Because i dont have dependency of other content files in my project.

0
votes

I think that the workaround you have described is currently the best solution you can find today.

As a workaround I added ProjectA as a ProjectReference with False and that seems to work, but it is not my desired solution.