powershell - 安装 NuGet 包时如何向项目添加 AfterBuild 事件?

标签 powershell nuget-package

我有一个 nuget 包,它添加了一个我需要在每次构建项目后运行的可执行文件。

我可以通过向每个项目文件添加一个部分来手动添加它,如下所示:

<Target Name="AfterBuild">
    <PropertyGroup>
      <PathToOutputExe>..\bin\Executable.exe</PathToOutputExe>
      <PathToOutputJs>"$(MSBuildProjectDirectory)\Scripts\Output.js"</PathToOutputJs>
      <DirectoryOfAssemblies>"$(MSBuildProjectDirectory)\bin\"</DirectoryOfAssemblies>
    </PropertyGroup>
    <AspNetCompiler Condition="'$(MvcBuildViews)'=='true'" VirtualPath="temp" PhysicalPath="$(ProjectDir)" />
    <Exec Command="$(PathToOutputExe) $(PathToOutputJs) $(DirectoryOfAssemblies)" />
  </Target>

安装 nuget 包时如何将其添加到项目中? (即在 Install.ps1 文件中使用 DTE $project 对象)

我将不胜感激对此的任何帮助。

谢谢

理查德

最佳答案

截至 NuGet 2.5 ,按照惯例,如果您在 build\{packageid}.targets 添加目标文件(注意“构建”与内容和工具处于同一级别),NuGet 会自动向项目中的 .targets 文件添加导入。那么你不需要在 install.ps1 中处理任何事情。卸载时将自动删除导入。

此外,我认为推荐的方法是创建一个单独的目标,该目标配置为在标准的“构建”目标之后运行:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="NuGetCustomTarget" AfterTargets="Build">
        <PropertyGroup>
            <PathToOutputExe>..\bin\Executable.exe</PathToOutputExe>
            <PathToOutputJs>"$(MSBuildProjectDirectory)\Scripts\Output.js"</PathToOutputJs>
            <DirectoryOfAssemblies>"$(MSBuildProjectDirectory)\bin\"</DirectoryOfAssemblies>
        </PropertyGroup>
        <AspNetCompiler Condition="'$(MvcBuildViews)'=='true'" VirtualPath="temp" PhysicalPath="$(ProjectDir)" />
        <Exec Command="$(PathToOutputExe) $(PathToOutputJs) $(DirectoryOfAssemblies)" />
    </Target>
</Project>

关于powershell - 安装 NuGet 包时如何向项目添加 AfterBuild 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8095855/

相关文章:

visual-studio-2017 - 启用 PackageReference 时 Nuget 在哪里下载包

powershell - 用例子为初学者讲解PowerShell中的ACL对象属性 "SetAccessRuleProtection"

nuget - ASP.NET 5 无法安装 ASP.NET 5 NuGet 包

nuget-package - GitHub 操作 : automatically push NuGet package

rest - Powershell:Out-File - 访问路径被拒绝

c# - PagedList MVC 不包含 PagedListPager 的定义

c++ - 无法打开包含文件 : 'cpprest/http_client.h'

powershell - 复制项目并排除文件夹

powershell - 指定所有属性时,Get-ADUser 不返回所有可能的 AD 属性

visual-studio - 包管理器控制台上的 PowerShell 版本