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



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

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

我是这个话题的新手,可能错过了一些东西。我在阅读第 3 方博客时第一次意识到它,然后在文档中也找到了它。我已经用谷歌搜索了更多帮助,但信息太多,我找不到相关的答案。

主项目文件:

...
<!-- 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 ProjectApplication.myapp
My ProjectSettings.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 2008SolutionsMsBuildCustomTargetTesterMsBuildCustomTargetTesterMsBuildCustomTargetTester.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:WINDOWSMicrosoft.NETFrameworkv3.5Microsoft.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 2008SolutionsMsBuildCustomTargetTesterMsBuildCustomTargetTesterMsBuildCustomTargetTester.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版本。

另请参阅:

"依赖目标">
  • 和"后目标"有什么区别?
  • https://blogs.msdn.microsoft.com/msbuild/2006/02/10/how-to-add-custom-process-at-specific-points-during-build-method-2/

最新更新