MSBuild copies dependent project files during _CopyOutOfDateSourceItemsToOutputDirectory

时间:2015-12-03 10:37:01

标签: c# visual-studio msbuild solution

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 个答案:

答案 0 :(得分:0)

我解决了在ProjectB .csproj文件中覆盖MSBuild任务的麻烦。 (我只在.sln文件中依赖ProjectA,而不是将项目作为ProjectReference添加到ProjectB)

将此目标添加到.csproj文件中:

<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>

对于我自己的文件,需要复制到输出目录我是通过XCOPY在postbuild中做的,如:

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

也许存在重写_CopyOutOfDateSourceItemsToOutputDirectory * tasks like this的智能变体。但我的变体现在让我满意。因为我的项目中没有其他内容文件的依赖项。

答案 1 :(得分:0)

我认为您所描述的解决方法是目前您可以找到的最佳解决方案。

作为一种解决方法,我添加了ProjectA作为带有False的ProjectReference,这似乎可行,但这不是我想要的解决方案。