Nuget packages .Net Core 3.1

Fragen die in diesen Artikel beantwortet werden:

  • Wie kann ich automatisch (on build) nuget-Packages erstellen?
  • Wie kann ich Parameter an den nuget build Task übergeben?
  • Wie kann ich code duplizierung vermeiden?
  • Wie kann ich weitere Dateien in mein nuget packages einfügen?
  • Was muss ich tun um SourceLink in den nuget packages zu aktivieren?

Visual Studio 2019 hat für die Erstellung der NuGet Packages eine eingebaute Möglichkeit NuGet-Pakete zu erstellen. Die Möglichkeit wurde nicht mit den ersten Versionen des Visual Studios  bereitgestellt und ist zum aktuellen Zeitpunkt (anfang 2021) nicht voll umfänglich verfübar. Es gibt keine Möglichkeit, je nach Configuration (Debug/Release) unterschiedliche packages oder dependencies hinzuzufügen. Ebenso können keine Build-Dateien und Targets hinzugefügt werden. Aus diesen Grund wird hier die realisierung mit einer „nuspec“-Datei betrachtet.

Um die wiederverwendung zu vereinfachen, wurde innerhalb der benötigten Projekte in Visual Studio ein include verwendet, welches via <Import Project=“..\Default.csproj“ /> im project hinzugefügt werden.

Hier ein Beispiel einer csproj-Datei eines Projektes:

<Project Sdk="Microsoft.NET.Sdk">
    <!-- Modify properties for project here -->
    <PropertyGroup>
        <!-- You can modify your nuget package name here. -->
        <PackageId>$(MSBuildProjectName)</PackageId>
        <!-- If not added here, is default 1.0.0 is used. -->
        <Version>1.0.2</Version>
    </PropertyGroup>
       <!-- Include default project -->
    <Import Project="..\default.csproj" />
    <ItemGroup>
        <!-- Add package references. -->
    </ItemGroup>
    <ItemGroup>
        <!-- Add project references. -->
    </ItemGroup>
</Project>

Hier der Inhalt der default.csproj

<Project DefaultTargets="Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!-- Change or update values in this Property group -->
    <PropertyGroup>
        <Authors></Authors>
        <Company></Company>
        <RepositoryUrl></RepositoryUrl>
        <!-- E.g. git -->
        <RepositoryType></RepositoryType>
        <Description></Description>
        <!-- If you want to publish on *every* build change this to true, consider that nuget packages needs to be unique and once published they should not be changed. This might make sense on the CI-System. -->
        <PublishRepositoryUrl>false</PublishRepositoryUrl>
    </PropertyGroup>
    <PropertyGroup>
<Deterministic>true</Deterministic>
<IncludeSymbols>true</IncludeSymbols> <SymbolPackageFormat>snupkg</SymbolPackageFormat> <EmbedUntrackedSources>true</EmbedUntrackedSources> <ContinuousIntegrationBuild>true</ContinuousIntegrationBuild> <OutputPath>bin\$(Configuration)</OutputPath> <DocumentationFile>bin\$(Configuration)\$(MSBuildProjectName).xml</DocumentationFile> <TargetFramework>netcoreapp3.1</TargetFramework> <Nullable>enable</Nullable> <GeneratePackageOnBuild>false</GeneratePackageOnBuild> </PropertyGroup> <ItemGroup Label="NugetAndDefaults"> <PackageReference Include="Microsoft.SourceLink.AzureDevOpsServer.Git" Version="1.0.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <!-- Add your Server variables: $(GitServerDomain) and $(GitServerPath) --> <SourceLinkAzureDevOpsServerGitHost Include="$(GitServerDomain)" VirtualDirectory="$(GitServerPath)" /> </ItemGroup> <Target Name="PostBuild" AfterTargets="PostBuildEvent"> <Exec Command="..\Nuget\nuget.exe pack ..\Nuget\$(PackageId).nuspec -Properties Version=$(Version);Id=$(PackageId);Title=$(PackageId);ProjectName=$(MSBuildProjectName);Configuration=$(Configuration) -Output bin\$(Configuration) " /> </Target> </Project>

Nun wird beim Build des Projectes eine nuspec-Datei benötigt: $(PackageId).nuspec

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>$id$</id>
        <title>$title$</title>
        <version>$version$</version>
        <!-- Add the authors -->
        <authors></authors>
        <!-- Add the owners -->
        <owners></owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <developmentDependency>false</developmentDependency>
        <!-- Add the description -->
        <description></description>
        <!-- All you required package dependencies -->
        <dependencies></dependencies>
    </metadata>
    <files>
        <file src="..\README.md" target="README.md" />
        <file src="..\$ProjectName$\bin\$configuration$\netcoreapp3.1\$ProjectName$.dll" target="lib\netcoreapp3.1\$ProjectName$.dll" />
        <file src="..\$ProjectName$\bin\$configuration$\netcoreapp3.1\$ProjectName$.pdb" target="lib\netcoreapp3.1\$ProjectName$.pdb" />
        <file src="..\$ProjectName$\bin\$configuration$\$ProjectName$.xml" target="Build\$ProjectName$.xml" />
        <file src="$id$.targets" target="Build\$id$.targets" />
    </files>
</package>

In this example, there is an additional file added for the xml documentation file, which later linked in the target project. This is done in a Build-Target within the nuget package, this file must be named $(PackageId).targets:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <!-- Replace Contoso.xml with the documentation file needed. -->
        <None Include="$(MSBuildThisFileDirectory)\Contoso.xml">
            <Link>Contoso.xml</Link>            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
    </ItemGroup>
</Project>

Folgende inhalte folgen hier noch:

  • git links
  • t4 um das  nuspec file zu erstellen.
  • Lösung als nuget package
  • Eigene GUI um packages zu erzeugen
Print Friendly, PDF & Email