首页 > 解决方案 > 使用困难模块化 Visual Studio 项目文件

问题描述

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

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

我是这个话题的新手,可能遗漏了一些东西。我在阅读 3rd-party博客时首先意识到它,然后在文档中也发现了它。我已经用谷歌搜索了更多帮助,但信息太多,我无法找到相关答案。

主项目文件:

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

标签: visual-studiovisual-studio-2008msbuild

解决方案


问题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 中引入了任何目标上的 BeforeTargetsandAfterTargets属性,但如果我没记错的话,上面的语法也应该适用于 Visual Studio 2008 附带的 MsBuild 版本。

也可以看看:


推荐阅读