MSBuild,OutputPath到lib目录不受尊重

时间:2017-10-07 17:02:48

标签: msbuild

我现在花了几个小时,但我根本没有得到它:

为什么 lib 子目录不受VS“快速最新检查”的尊重? 如果设置了库的lib输出目录,则始终重建解决方案 - 如果已进行更改无关紧要。如果删除 \ lib 子目录,则可以使用。为什么?

这是我到目前为止测试的内容:

请参阅下一个代码段。那一个很完美。如果要求多个依赖项目多次构建,如果没有进行任何更改,它们实际上只构建一次。 Visual Studio FastUpToDateCheck开始使用。

但是如果你改变了行

<OutputPath>$(SolutionDir)bin\$(Configuration)\$(Platform)</OutputPath>

<OutputPath>$(SolutionDir)bin\$(Configuration)\$(Platform)\lib\</OutputPath>

它不断重建。有什么想法吗?

ComponentBuild.props位于.sln文件旁边

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <IntermediateOutputPath>$(SolutionDir)obj\$(Configuration)\$(MSBuildProjectName)\</IntermediateOutputPath>
    <UseCommonOutputDirectory>False</UseCommonOutputDirectory>
    <DisableFastUpToDateCheck>false</DisableFastUpToDateCheck>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(OutputType)' == 'Library' ">
    <!-- To distinguish by \lib\ does not work, a rebuild is triggered since the up-to-date check fails -->
    <!-- <OutputPath>$(SolutionDir)bin\$(Configuration)\$(Platform)\lib\</OutputPath> -->
    <OutputPath>$(SolutionDir)bin\$(Configuration)\$(Platform)</OutputPath>   
    <OutDir>$(OutputPath)</OutDir>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(OutputType)' == 'Exe' ">
    <OutputPath>$(SolutionDir)bin\$(Configuration)\$(Platform)\</OutputPath>
    <OutDir>$(OutputPath)</OutDir>
  </PropertyGroup>
</Project>

该文件包含在Import Microsoft.CSharp.targets之前的csproj文件中:

.csproj文件:

    <!-- position of include is important, OutputType of project must be defined already -->
    <Import Project="$(SolutionDir)ComponentBuild.props" Condition="Exists('$(SolutionDir)ComponentBuild.props')" /> 
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    <PropertyGroup>
      <PostBuildEvent>
      </PostBuildEvent>
    </PropertyGroup>

行为变得更加奇怪,我测试得越多。 我创建了两个简单的库项目A和B. B依赖于A.我添加了上面提到的导入和FastUpToDateCheck工作。 将lib路径添加到库输出类型后,无需更改任何其他内容时,它将起作用。但是当清理lib B项目时,每个后续构建都会重建项目B.

将exe路径添加到exe输出类型时也是如此。 FastUpToDateCheck再次运行。

然后我再次从输出类型exe中删除了lib路径,但FastUpToDateCheck仍然令人惊讶地工作 - 总是如此。即使清理构建,更改类或删除所有obj和bin文件夹。

但是一旦我从lib输出类型中删除了lib路径,即我将所有设置恢复到原始状态,它就会失败。它每次都会重建。诊断输出的第一行是

  

Project'ClassLibrary1'不是最新的。缺少输出文件   'c:\ Users \ hg348 \ Documents \ Visual Studio   2015 \项目\ BuildTest \ BIN \调试\ AnyCPU \ lib中\ ClassLibrary1.dll'

它仍然会查看lib路径,即使它不再设置。 我认为有一些讨厌的缓存。

有人可以验证一下吗?

1 个答案:

答案 0 :(得分:1)

好吧,我上面描述的测试得出了答案: 它是在Visual Studio(VS)中缓存,它在更改输出路径后触发构建。在更改了outputpath并且可能还有outdir后,Visual Studio仍会在旧目录中查找其FastUpToDateCheck。

关闭并重新打开解决方案有助于清除VS缓存。在某些情况下,有必要删除隐藏文件夹.suo中的隐藏文件.vs 这解决了上述问题中给出的示例文件中陈述的所有问题。

我的最终导入文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
  <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

      <!--  Note that VS caches settings, to be sure the FastUpToDateCheck works
                * reopen the solution after 
                    - changing properties
                    - after adding a platform config
                    - after adding references to projects
                * close VS and remove the hidden file 
                  <solution folder>\.vs\<solution name>\v14\.suo   after changing IntermediateOutputPath,
                  (You have to enable "how hidden files" in windows file explorer)
                * After updating App.config do a random change in any .cs source file too, 
                  otherwise FastUpToDateCheck fails
      -->

      <PropertyGroup>
        <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

        <IntermediateOutputPath>$(SolutionDir)obj\$(Configuration)\$(Platform)\$(MSBuildProjectName)\</IntermediateOutputPath>

        <!-- if true, don't copy output files of referenced assemblies, since everything builds to the same folder. -->
        <UseCommonOutputDirectory>true</UseCommonOutputDirectory>
        <DisableFastUpToDateCheck>false</DisableFastUpToDateCheck>
      </PropertyGroup>

      <PropertyGroup Condition=" '$(OutputType)' == 'Library' ">

        <OutputPath>$(SolutionDir)bin\$(Configuration)\$(Platform)\lib\</OutputPath>
        <OutDir>$(OutputPath)</OutDir>
      </PropertyGroup>

      <PropertyGroup Condition=" '$(OutputType)' == 'Exe' ">
        <OutputPath>$(SolutionDir)bin\$(Configuration)\$(Platform)\</OutputPath>
        <OutDir>$(OutputPath)</OutDir>
      </PropertyGroup>


      <!-- sets "Copy Local" property of references to false on reopen of solution
           don't copy output files of referenced assemblies, since everything builds to the same folder -->
      <ItemDefinitionGroup>
        <Reference>
            <Private>False</Private>
        </Reference>
        <ProjectReference>
            <Private>False</Private>
        </ProjectReference>
      </ItemDefinitionGroup>

  </Project>