如何将netstandard2.0项目的所有依赖项打包到一个包中?

时间:2018-12-28 20:52:42

标签: .net nuget .net-standard .net-standard-2.0

我有一个支持插件API的工具-它在运行时加载给定的程序集。

现在,我正在为此工具实现一个插件。目标框架是.NET Standard 2.0。该插件依赖于其他两个NuGet软件包。

我想创建一个包含插件及其所有依赖项的NuGet包,因为否则该工具将无法加载它。但是bin \ Debug \ netstandard2文件夹不包含任何依赖项。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

愚蠢的我。 dotnet publish就是这样做的。

现在,我必须找出包装它的最佳方法。

我最终得到了以下脚本:

param($BuildNumber = $env:BUILD_BUILDNUMBER, [switch]$NoPublish = $($env:BUILD_REASON -eq 'PullRequest'))

$ErrorActionPreference = "Stop"

$Properties = @{
    BuildNumber = $BuildNumber
    SourceRevisionId = $(git rev-parse --short HEAD)
    RepositoryUrl = $(git remote get-url origin)
}
$BranchName = $env:BUILD_SOURCEBRANCHNAME
if (!$BranchName)
{
    $BranchName = git rev-parse --abbrev-ref HEAD
}

$MSBuildProperties = $Properties.GetEnumerator() | Where-Object { 
    $_.Value
} | ForEach-Object { 
    "/p:{0}={1}" -f $_.Key,$_.Value 
}

Write-Host "Building ..."
dotnet publish -v:q -nologo -c Release $MSBuildProperties
if ($LastExitCode)
{
    exit $LastExitCode
}

Write-Host "Packing ..."
$MainAssemblyName = (Get-Item *.sln).BaseName
$MainAssemblyPath = "$PSScriptRoot\src\bin\Release\netstandard2\publish\$MainAssemblyName.dll"
$NuspecFile = $($MainAssemblyPath.Replace('.dll', '.nuspec'))
$VersionInfo = (Get-Item $MainAssemblyPath).VersionInfo
@"
<?xml version="1.0"?>
<package>
  <metadata>
    <id>$MainAssemblyName</id>
    <version>$($VersionInfo.ProductVersion)</version>
    <authors>Mark Kharitonov</authors>
    <owners>$($VersionInfo.CompanyName)</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$($VersionInfo.Comments)</description>
    <copyright>$($VersionInfo.LegalCopyright)</copyright>
    <projectUrl>$($Properties.RepositoryUrl)</projectUrl>
    <repository type="git"
                url="$($Properties.RepositoryUrl)"
                branch="$BranchName"
                commit="$($Properties.SourceRevisionId)" />
  </metadata>
  <files>
    <file src="*.*" target="tools" />
  </files>
</package>
"@ | Out-File -Encoding utf8 $NuspecFile

Remove-Item src\bin\Release\*.nupkg -ErrorAction SilentlyContinue
dotnet pack -v:q -nologo -c Release --no-build -p:NuspecFile=$NuspecFile
if ($LastExitCode)
{
    exit $LastExitCode
}

if (!$NoPublish)
{
    Write-Host "Publishing ..."
    $NuGetPkg = (Get-Item src\bin\Release\*.nupkg).FullName
    dotnet nuget -v Error push $NuGetPkg -s http://devstatic/NuGetServer/nuget
}

源代码具有以下Directory.Build.props

<Project>
  <PropertyGroup>
    <NoWarn>NU5105</NoWarn>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>

    <Version>$([System.Text.RegularExpressions.Regex]::Match($(BuildNumber), `\d+\.\d+(?:.\d+)?(?:.\d+)?`))</Version>
    <Company>YabaDabaDoo, Inc.</Company>
    <CurrentYear>$([System.DateTime]::Now.Year)</CurrentYear>
    <CopyrightYears Condition="$(CurrentYear) == 2018">2018</CopyrightYears>
    <CopyrightYears Condition="$(CurrentYear) != 2018">2018-$(CurrentYear)</CopyrightYears>
    <Copyright>Copyright © $(CopyrightYears) by $(Company) All rights reserved.</Copyright>
    <Product>Log DbUpgrade Progress</Product>
    <AssemblyTitle>Log DbUpgrade Progress</AssemblyTitle>
    <Description>Logs the progress of the DbUpgrade tool to a file, which can later be used to send AppInsights metrics.</Description>
    <NeutralLanguage>en-US</NeutralLanguage>
    <Title>$(AssemblyTitle)</Title>
    <!-- Setting nuspec Owners property from msbuild is currently not supported. At the moment it is set to be equal Authors. -->
    <Authors>$(Company)</Authors>
  </PropertyGroup>
</Project>

这是我最简单的方法,无需重复任何配置。