除了其他答案之外,当VS或MSBuild执行构建操作时,MSBuild的作用可以在随.Net一起交付的目标文件中找到。这些文件可以在系统上的FrameWork目录中找到。在我的情况下:
C:\Windows\Microsoft.NET\Framework64\v3.5
包含Microsoft.Common.targets
等文件。此文件包含以下代码片段:
<!--
============================================================
Build
The main build entry point.
============================================================
-->
<PropertyGroup>
<BuildDependsOn>
BeforeBuild;
CoreBuild;
AfterBuild
</BuildDependsOn>
</PropertyGroup>
<Target
Name="Build"
Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
DependsOnTargets="$(BuildDependsOn)"
Outputs="$(TargetPath)"/>
这意味着重新定义此目标,您可以使MSBuild和VS做任何您想要的事情。提到文件的顶部包含了一个重要的信息:
Microsoft.Common.targets
WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.
This file defines the steps in the standard build process for .NET projects. It
contains all the steps that are common among the different .NET languages, such as
Visual Basic, C#, and Visual J#.
我的建议是尽可能多地阅读有关MSBuild及其构建文件语法的内容,尝试重新定义项目中的Build目标。我认为,在了解MSBuild后,您可能会发现更简单的方式来满足您的要求。您可以在
这个问题的一个答案中找到重新定义目标的示例。
编辑:
如何重新定义目标?
重新定义实质上是在已定义相同目标之后再次定义。因此,在您的.*proj文件中,在导入所有所需目标以构建C#项目的<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
行之后定义一个Build
Task
。例如:
<Target
Name="Build"
Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
DependsOnTargets="BeforeBuild"
Outputs="$(TargetPath)">
<Exec Command="nmake" />
</Target>