难以使用 <Import> 模块化 Visual Studio 项目文件

Difficulty using <Import> to modularize a Visual Studio project file

我正在尝试模块化 Visual Studio 项目文件,但它不起作用。这是 Visual Studio 2008 年的 .Net 3.5。

如下所示,第一个示例有效,但第二个示例无效。我怎样才能让它工作..?

我是这个主题的新手,可能遗漏了一些东西。我也是在阅读第 3 方 blog, and then found it in the documentation 时才第一次意识到这一点。我在 Google 上搜索了更多帮助,但信息太多,我无法找到相关答案。

主项目文件:

  ...
  <!-- main project file -->
  <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />

  <Target Name="AfterBuild">
    <Message Text="This is a message from the *.vbproj file."/> ... this works
  </Target>
</Project>

...但是如果使用<Import>,在导入文件中使用相同的<Target><Message>,则不起作用。 MSBuild 似乎可以正确处理所有内容,但什么也没有发生...

主项目文件:

  ...
  <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
  <Import Project="$(MSBuildProjectDirectory)\CustomBuildEvents.targets" /> ... new tag

  <Target Name="AfterBuild">
    <Message Text="This is a message from the *.vbproj file."/> ... this still works
  </Target>
</Project>

导入的目标文件:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="AfterBuild">
    <Message Text="Hello from the imported file." Importance ="high"/> ... doesn't work
  </Target>
</Project>

和构建输出,Verbosity 设置为 Diagnostic:

### Halfway through the output, this is the only mention of the imported file. ###

None
    CustomBuildEvents.targets      ... custom file
    My Project\Application.myapp
    My Project\Settings.settings

### And then at the end, no mention of the imported file or its message. ###

Done building target "CoreBuild" in project "MsBuildCustomTargetTester.vbproj".
Target "AfterBuild" in file "C:\Visual Studio 2008\Solutions\MsBuildCustomTargetTester\MsBuildCustomTargetTester\MsBuildCustomTargetTester.vbproj":
  Task "Message"
    Hello from the *.vbproj file.      ... message from main file
  Done executing task "Message".
Done building target "AfterBuild" in project "MsBuildCustomTargetTester.vbproj".
Target "Build" in file "c:\WINDOWS\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets":
  Building target "Build" completely.
  No input files were specified.
Done building target "Build" in project "MsBuildCustomTargetTester.vbproj".

Done building project "MsBuildCustomTargetTester.vbproj".

Project Performance Summary:
      109 ms  C:\Visual Studio 2008\Solutions\MsBuildCustomTargetTester\MsBuildCustomTargetTester\MsBuildCustomTargetTester.vbproj   1 calls

AfterBuild的问题是它只能定义一次。因此,如果您导入它,然后在项目文件中再次定义它,最后的定义将成为唯一的定义。

要解决这个问题,您需要使用更高级的方法来注册事件。鉴于您使用的是 Visual Studio 2008(为什么?!),您需要为自定义目标文件使用更高级的语法:

<Project>
    <!-- 
        Redefines the original build order, includes the standard targets and 
        adds your new custom target to the end of the list.
    -->
    <PropertyGroup>
        <BuildDependsOn>
            $(BuildDependsOn);
            CustomTarget
        </BuildDependsOn>
    </PropertyGroup>

    <CustomTarget> 
        <!-- Imported after Build -->
        <Message Text="Hello from the imported file." Importance ="high"/>
    </CustomTarget>
</Project>

在 MsBuild 4 中引入了其他方法,可在任何目标上使用 BeforeTargetsAfterTargets 属性,但如果我没记错的话,上述语法也适用于Visual Studio 2008.

附带的 MsBuild 版本

另请参阅: