visual-studio - MSBuild:在 Visual Studio 中构建解决方案后运行目标

标签 visual-studio msbuild msbuild-task

我们有一个包含多个项目的解决方案,需要在编译所有代码后对编译后的代码运行一个工具。该工具必须采用从解决方案构建的所有 .dll 和 .exe 文件作为输入(因此对于增量构建,仅提供刚刚重建的二进制文件是不够的),并且所有这些二进制文件都必须达到日期。

现在,我们使用从命令行运行的 MSBuild .proj 文件执行此操作,该文件包含所有项目并使用 MSBuild 的 Output 元素来查找解决方案中二进制文件的路径。

这有两个问题使这个解决方案不太适合我们的需要。该工具实际上不需要在所有项目上运行,只需要在其中一些项目上运行。并且要求我们能够从 Visual Studio 中进行构建。

似乎我需要的是,在某些项目的 .csproj 文件中,定义“在我身上运行该工具”的内容,收集这些项目的路径,并在该路径集合上运行该工具。

但我看不出如何做到这一点。看起来当我从 Visual Studio 构建时,我无法在项目构建之间共享状态,并且解决方案构建后似乎没有 Hook 。

在命令行中,有 before.*.sln.targets 和 after.*.sln.targets,但这些在 Visual Studio 中不起作用。

看起来我想要的太简单了,我只是想要一个工具来在构建后自动查看解决方案的二进制文件,所以我认为这应该是不可能的。但是我从文档中看不到如何做到这一点。我是 MSBuild 的新手,所以我可能遗漏了一些明显的东西。

最佳答案

Msbuild 具有可扩展性目标。这些空目标可用于项目文件。见 following article and section Build process extensibility points .

For instance, if you wanted to deploy some related files alongside the assembly once it was built, overriding the AfterBuild target would be the way to go, as shown below.



我将他们的示例更改为使用 exec 任务,如下所示。本示例在生成二进制文件后使用 YourCustomTasks.exe。
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!-- Properties and items are deleted for clarity -->
    <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
    <ItemGroup>
        <Binaries Include="*.dll;*.exe"/>
    </ItemGroup>
    <Target Name="AfterBuild">
        <Exec Command="YourCustomTasks.exe %(Binaries.Identity)" />
    </Target>
</Project>

如果您希望您的目标在您的解决方案之后工作,请使用 following answer .

To execute a solution-wide Before and After targets, you would create two MSBuild project files named "after..sln.targets" and "before..sln.targets" in the same folder as your solution.

关于visual-studio - MSBuild:在 Visual Studio 中构建解决方案后运行目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29726722/

相关文章:

git - 我可以在 Visual Studio 中运行 Git 命令吗?

visual-studio-2010 - 暂时禁用 Visual Studio 中的构建事件

ios - Xamarin MSBuild - 错误 MSB3323 : Unable to find manifest signing certificate in the certificate store

.net - .csproj 文件中 <SolutionDir> 元素的用途是什么?

entity-framework - 使用 MSBuild 构建 Entity Framework 模型而不使用项目文件?

deployment - MSBuild ReadLinesFromFile 一行上的所有文本

visual-studio - Visual Studio 2019 中的 TypeScript 版本不匹配

python - 当从 python 使用 TSQL(SQL Server 上的 mssql)时,如何自动生成 SQLAlchemy 的 ORM 代码?

visual-studio-2012 - MSBuild 在任务中引用 DLL

从 Windows 中的命令提示符编译 C 代码?