创建仅包含msbuild.targets的nuget包

问题描述 投票:0回答:1

我正在尝试创建一个仅包含 MSBuild *.targets 文件的 nuget 包。我可以创建一个 C# 类库并添加我的目标文件,但我不需要 C# 构建过程的开销。有没有办法编写仅包含静态内容或构建文件的 nuget 包?

我看到的答案建议只使用原始 nuspec 文件并使用

nuget pack
创建包。如果可能的话,我想使用
dotnet pack
,但它似乎不直接支持 nuspec 文件。有谁知道这是否可能?

.net msbuild nuget
1个回答
0
投票

页面列出了在各种(非典型)构建场景中使用的目标。

Microsoft.Build.NoTargets
...

支持不编译程序集的实用程序项目。

这听起来正是我想要的。如果您查看此页面,它提供了有关此项目 Sdk 具体功能的更多详细信息,基本上是将文件 @(FilesToCopy) 复制到 $(OutDir) 目录。

我并不是真的想要那样,但是...我创建了一个项目,my-project,具有以下文件结构。

my-project   # base project folder
|-- PackageLayout
|   |-- buildTransitive
|   |   |-- my-project.props
|   |   |-- my-project.targets
|   |-- icon.png
|   |-- README.md
|-- my-project.msbuildproj

PackageLayout目录是我想要在我的nuget包中的文件和布局。 my-project.msbuildproj 文件是我定义所有包元数据、PackageId、PackageVersion 等的地方。

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.Build.NoTargets/3.7.56">
  <PropertyGroup>
    <TargetFramework>netstandard1.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    
    <!-- Don't warn about no binaries, there aren't supposed to be any -->
    <NoWarn>NU5128</NoWarn>
  </PropertyGroup>

  <PropertyGroup>
    <!-- Package metadata -->
    <PackageId>my-project</PackageId>
    <PackageVersion>1.0.0</PackageVersion>
    <DevelopmentDependency>true</DevelopmentDependency>
    <IsTool>true</IsTool>

    <PackageReadmeFile>README.md</PackageReadmeFile>
    <PackageIcon>icon.png</PackageIcon>
  </PropertyGroup>

  <ItemGroup>
    <!--
    * Display project root files in the project         : `Include="*"`
      * But don't include them in the nuget package     : `Pack="false"`
      * Exclude the msbuildproj file as it is displayed
        at the project level                            : `Exclude="$(MSBuildProjectFile)"`
    -->
    <None Include="*" Pack="false" Exclude="$(MSBuildProjectFile)"/>
  </ItemGroup>

  <ItemGroup>
    <!--
    Include everything in the PackageLayout directory in the package
    and maintain the same directory structure (but from the package
    root)
    -->
    <None Include="PackageLayout\**\*" Pack="true" PackagePath="\" />

    <!--
    Anything in buildTransitive directory, also pack in the build directory
    for backwards compatability.
    
    https://github.com/NuGet/Home/wiki/Allow-package%2d%2dauthors-to-define-build-assets-transitive-behavior
    -->
    <None Include="PackageLayout\buildTransitive\**\*" Pack="true" PackagePath="build\" />
  </ItemGroup>
</Project>

如果我在这个项目上运行 dotnet build ,它会在 $(OutDir) 中生成

my-project.1.0.0.nupkg
以及正确的 icon.png、自述文件和目标/属性文件。

© www.soinside.com 2019 - 2024. All rights reserved.