【发布时间】:2019-02-16 19:37:56
【问题描述】:
我正在尝试创建一个在构建之前运行外部应用程序的 NuGet 包。该应用程序会创建一个 compilationtime.h 文件以了解编译时间并在 About 对话框中显示它。
我的 NuGet 包有一个带有 CreateCompilationTimeFile.exe 的 Tools 文件夹和一个带有 {packagename}.propsBuild 文件夹/em> 文件。
我尝试了很多组合来在 {packagename}.props 文件中创建 Pre-Build 事件,但它们都不起作用。
我试过这个:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemDefinitionGroup>
<PreBuildEvent>
<Command>"$(MSBuildThisFileDirectory)..\tools\CreateCompilationTimeFile.exe"</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
</Project>
还有这个:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="BeforeBuild2" BeforeTargets="Build">
<Exec Command="$(MSBuildThisFileDirectory)..\tools\CreateCompilationTimeFile.exe"/>
</Target>
</Project>
如果我在没有 NuGet 包的情况下执行此操作并使用下一个属性表执行此操作,则它可以正常工作:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup>
<PreBuildEvent>
<Command>"$(SolutionDir)Tools\CreateCompilationTimeFile.exe"</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemGroup />
</Project>
但我想使用 NuGet 包而不是属性表。
我做错了什么? 谢谢。
【问题讨论】:
-
您的第二种方法
<Target Name="BeforeBuild2" BeforeTargets="Build">应该可以工作,为什么它不适合您?你对这个方法有什么错误吗? -
不,我没有任何错误,但预构建事件永远不会运行,并且文件“compilationtime.h”永远不会创建。
-
预构建事件永远不会运行,你让我感到困惑。如果您使用第二种方法,则没有任何预构建事件,它被目标
<Target Name="BeforeBuild2" BeforeTargets="Build">替换,对吗?当您构建项目时,VS/MSBuild 将在构建之前执行此目标,将执行 CreateCompilationTimeFile.exe 并生成文件“compilationtime.h”。我有什么误解吗? -
或者在 NuGet 包中创建 Pre-Build 事件是您的目标,而不是在构建之前创建运行外部应用程序的 NuGet 包?
-
第二种方法实际上没有“预构建事件”,确实程序应该被执行,但它没有被执行。很抱歉造成误会。
标签: visual-studio msbuild nuget nuget-package pre-build-event