更改nuget包的输出目录

change output directory of nuget package

我从我的项目创建了一个 NuGet 包。包的输出目录是解决方案目录。我想将它输出到特定目录。我在 csproj 文件和 nuspec 文件中尝试了一个目标。 None 成功了。如何获取指定文件夹中生成的包?

在我的 .csproj 中:

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
  <PropertyGroup>
    <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  </PropertyGroup>
  <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

在我的 .nuspec 中:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyPackage.dll</id>
    <version>1.0.0</version>
    <authors>me</authors>
    <owners>me</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2016</copyright>
    <files>
      <file src="bin\MyPackage.dll" target="C:\LocalPackageRepository" />
    </files>
  </metadata>
</package>

您不能从 .nuspec 文件执行此操作。您可以创建一个 NuGet.Config 文件并为所有解决方案定义一个全局包目录:

<configuration>
  <config>
    <add key="repositoryPath" value="C:\myteam\teampackages" />
  </config>
  ... 
</configuration>

这是在 .nupkg 文件之外完成的,并存储在您的配置文件下或子目录中,该子目录是包含您的解决方案的所有目录的父目录。有关此功能的更多详细信息,请参阅 NuGet documentation

在 NuGet 的 'old' 方式中(您似乎使用它,检查 this 以了解新旧信息)这可以通过使用 .nuget\NuGet 中的命令来实现.targets 你提到的文件。如果您将带有 PackageOutputDir 的行更改为下面,它将起作用。

<PackageOutputDir Condition="$(PackageOutputDir) == ''">C:\LocalPackageRepository</PackageOutputDir>

更好的方法是在 .csproj 中的 PropertyGroup 上设置一个 属性,如下所示:

<PackageOutputDir>C:\LocalPackageRepository</PackageOutputDir>

在 NuGet 的新方式中,您可以将此密钥添加到 NuGet.config 文件:

<add key="repositoryPath" value="C:\LocalPackageRepository" />

不确定为什么全局配置设置对我不起作用,但添加以下解决方案解决了我的问题:

  • 在用户变量下创建环境变量:
Variable name: MyNugetsOutput
Variable value: D:\myteam\teampackages
  • 然后将以下设置添加到 .csproj 文件:
<Target Name="CopyPackage" AfterTargets="Pack">
  <Copy SourceFiles="$(OutputPath)$(PackageId).$(PackageVersion).nupkg"
        DestinationFolder="$(MyNugetsOutput)$(PackageId).$(PackageVersion).nupkg" />
</Target>

参考:Target build order

更新

目前我正在使用下面的代码,它更简单但它会将所有 .nupkg 文件复制到“本地”路径

<Target Name="AfterPack" AfterTargets="Pack">
    <Exec Command="dotnet nuget push $(PackageOutputPath)*.nupkg --source Local" />
</Target>