【问题标题】:How can I add an AfterBuild event to a project when installing my NuGet package?安装 NuGet 包时,如何将 AfterBuild 事件添加到项目中?
【发布时间】:2011-11-11 15:05:04
【问题描述】:

我有一个 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 对象)

我将不胜感激任何帮助。

谢谢

理查德

【问题讨论】:

    标签: powershell nuget-package


    【解决方案1】:

    从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>
    

    【讨论】:

      【解决方案2】:

      这是一个添加后构建目标的脚本。它还使用上面提到的 NugetPowerTools。

      $project = Get-Project
      $buildProject = Get-MSBuildProject
      
      $target = $buildProject.Xml.AddTarget("MyCustomTarget")
      $target.AfterTargets = "AfterBuild"
      $task = $target.AddTask("Exec")
      $task.SetParameter("Command", "`"PathToYourEXe`" $(TargetFileName)")
      

      最困难的部分是正确引用。这就是它在我的 powertools 脚本中的样子。

      【讨论】:

        【解决方案3】:

        您可以直接使用 MSBuild Api,如 blog post。

        Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
        $msbProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1
        # use $msbProject to add/change AfterBuild target
        

        或者您可以使用 NugetPowerTools 添加 Powershell 命令 Get-MSBuildProject 来做同样的事情。

        另外,请参阅forum post,了解有关添加新目标的更多详细信息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-28
          • 1970-01-01
          • 2015-11-16
          • 1970-01-01
          相关资源
          最近更新 更多