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

时间:2019-12-27 15:01:18

标签: visual-studio visual-studio-2008 msbuild

我正在尝试将Visual Studio项目文件模块化,但无法正常工作。这适用于带有.Net 3.5的Visual Studio 2008。

如下所示,第一个示例有效,但第二个示例无效。我如何使它工作..?

我是这个主题的新手,可能还缺少一些东西。在阅读第三方blog时,我首先意识到了这一点,然后在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

1 个答案:

答案 0 :(得分:1)

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版本。

另请参阅: