C# NUGET包自动复制依赖项

在csproj中配置如下内容:

<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>

以及这个:

<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
    <ItemGroup>
        <!-- Filter out unnecessary files -->
        <_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')-&gt;WithMetadataValue('PrivateAssets', 'All'))" />
    </ItemGroup>

    <!-- Print batches for debug purposes -->
    <Message Text="Batch for .nupkg: ReferenceCopyLocalPaths = @(_ReferenceCopyLocalPaths), ReferenceCopyLocalPaths.DestinationSubDirectory = %(_ReferenceCopyLocalPaths.DestinationSubDirectory) Filename = %(_ReferenceCopyLocalPaths.Filename) Extension = %(_ReferenceCopyLocalPaths.Extension)" Importance="High" Condition="'@(_ReferenceCopyLocalPaths)' != ''" />

    <ItemGroup>
        <!-- Add file to package with consideration of sub folder. If empty, the root folder is chosen. -->
        <BuildOutputInPackage Include="@(_ReferenceCopyLocalPaths)" TargetPath="%(_ReferenceCopyLocalPaths.DestinationSubDirectory)" />
    </ItemGroup>
</Target>

    以下是我用的一个nuget包的完整配置,可供参考:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
      <TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
      <LangVersion>preview</LangVersion>
      <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
      <Title>bodong.Avalonia.PropertyGrid</Title>
      <Version>11.0.4.0</Version>
      <Authors>bodong</Authors>
      <Description>A  PropertyGrid control implementation for Avalonia</Description>
      <PackageProjectUrl>https://github.com/bodong1987/Avalonia.PropertyGrid</PackageProjectUrl>
      <RepositoryUrl>https://github.com/bodong1987/Avalonia.PropertyGrid.git</RepositoryUrl>
      <RepositoryType>git</RepositoryType>
      <PackageTags>Avalonia;PropertyGrid;AvaloniaControls</PackageTags>
      <ProduceReferenceAssembly>False</ProduceReferenceAssembly>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="Assets\Localizations\en-US.json" />
    <None Remove="Assets\Localizations\ru-RU.json" />
    <None Remove="Assets\Localizations\zh-CN.json" />
  </ItemGroup>

  <ItemGroup>
    <AvaloniaResource Include="Assets\Localizations\en-US.json" />
    <AvaloniaResource Include="Assets\Localizations\ru-RU.json" />
    <AvaloniaResource Include="Assets\Localizations\zh-CN.json" />
  </ItemGroup>

  <ItemGroup>
    <None Include="..\README_NUGET.md">
      <Pack>True</Pack>
      <PackagePath>\</PackagePath>
    </None>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Avalonia" Version="11.0.4" />
    <PackageReference Include="Avalonia.Controls.ColorPicker" Version="11.0.4" />
    <PackageReference Include="Avalonia.Diagnostics" Version="11.0.4" />
    <PackageReference Include="System.Reactive" Version="6.0.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\PropertyModels\PropertyModels.csproj" PrivateAssets="All">
    </ProjectReference>
  </ItemGroup>

  <ItemGroup>
    <Compile Update="Controls\BindiListElementPlaceholderEdit.axaml.cs">
      <DependentUpon>BindiListElementPlaceholderEdit.axaml</DependentUpon>
    </Compile>
  </ItemGroup>
    
    <PropertyGroup>
        <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
        <PackageId>bodong.Avalonia.PropertyGrid</PackageId>
        <Product>bodong.Avalonia.PropertyGrid</Product>
        <PackageLicenseExpression>MIT</PackageLicenseExpression>
        <IncludeSymbols>False</IncludeSymbols>
        <PackageReadmeFile>README_NUGET.md</PackageReadmeFile>
        <GenerateDocumentationFile>True</GenerateDocumentationFile>
        <Nullable>disable</Nullable>
    </PropertyGroup>

    <Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
        <ItemGroup>
            <!-- Filter out unnecessary files -->
            <_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')-&gt;WithMetadataValue('PrivateAssets', 'All'))" />
        </ItemGroup>

        <!-- Print batches for debug purposes -->
        <Message Text="Batch for .nupkg: ReferenceCopyLocalPaths = @(_ReferenceCopyLocalPaths), ReferenceCopyLocalPaths.DestinationSubDirectory = %(_ReferenceCopyLocalPaths.DestinationSubDirectory) Filename = %(_ReferenceCopyLocalPaths.Filename) Extension = %(_ReferenceCopyLocalPaths.Extension)" Importance="High" Condition="'@(_ReferenceCopyLocalPaths)' != ''" />

        <ItemGroup>
            <!-- Add file to package with consideration of sub folder. If empty, the root folder is chosen. -->
            <BuildOutputInPackage Include="@(_ReferenceCopyLocalPaths)" TargetPath="%(_ReferenceCopyLocalPaths.DestinationSubDirectory)" />
        </ItemGroup>
    </Target>

</Project>

 

posted @ 2023-09-15 10:18  bodong  阅读(288)  评论(0编辑  收藏  举报