简体   繁体   中英

Include build directory in nuget package using visual studio pack

I'm attempting to create a nupkg with Visual Studio using the built in nuget package building and include the build directory from my project in the nupkg. It seems like it should be a fairly simple task but I can't get it to work. From my googling adding either of these to my csproj file should work, but both create an empty 'build' directory in the nupkg:

 <ItemGroup>
    <None Include="build\**">
      <Pack>true</Pack>
      <PackagePath>build\</PackagePath>
      <IncludeInPackage>true</IncludeInPackage>
    </None>
  </ItemGroup>

Using nuget pack to create the package with the following in my nuspec does work:

  <files>
        <!-- Include everything in \build -->
    <file src="build\**" target="build" />
  </files>

Include build directory in nuget package using visual studio pack

According to the document Including content in a package , you should use the properties <Pack>true</Pack> and <PackagePath>build\\</PackagePath> :

If you want to copy all your content to only a specific root folder(s) (instead of content and contentFiles both), you can use the MSBuild property ContentTargetFolders, which defaults to "content;contentFiles" but can be set to any other folder names.

PackagePath can be a semicolon-delimited set of target paths. Specifying an empty package path would add the file to the root of the package.

So, you can change your ItemGroup like following:

  <ItemGroup>
    <None Include="build\**" Pack="True" PackagePath="build\" />
  </ItemGroup>

Update:

I believe this is the same as what I added but in a different XML structure and without the Pack attribute

The Pack attribute is the key point. It works fine with your XML structure and the Pack attribute. You should make sure you have the files in the build folder in your project folder :

在此处输入图片说明

Check my test demo below:

在此处输入图片说明

Update2:

Ah! You are using the .net framework project!! That the reason for this issue. This method is used for .net standard and .net core project by default and it not work for .net framework . To resolve this issue you have to use the .nupsec file, like you post in the question.

If you still want to include build directory in nuget package using visual studio pack , you need change your project type to SDK type:

Check this document for some more details.

Then you can use the method, which we talked about before.

Hope this helps.

The solution to this issue was to upgrade the project to SDK type (Xamarin binding projects by default use the old format but seem to work with the new type) and then use:

<ItemGroup>
    <None Update="build\**">
        <IncludeInPackage>true</IncludeInPackage>
    </None>
</ItemGroup>

To include the build directory. The alternative is using nuget pack .

When converting the project make sure to leave in the Xamarin import:

<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.ObjCBinding.CSharp.targets" />

Here's how my project file looks afterwards:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
  <PropertyGroup>
    <PackageId></PackageId>
    <PackageVersion>3.3.2</PackageVersion>
    <ReleaseVersion>$(PackageVersion)</ReleaseVersion>
    <AssemblyVersion>$(PackageVersion)</AssemblyVersion>
    <Authors>Nick Brook</Authors>
    <Description></Description>
    <Copyright></Copyright>
    <PackageProjectUrl></PackageProjectUrl>
    <Summary></Summary>
    <PackageTags></PackageTags>
    <Title></Title>
    <PackageReleaseNotes>Initial Release</PackageReleaseNotes>
    <OutputType>Library</OutputType>
    <IPhoneResourcePrefix>Resources</IPhoneResourcePrefix>
    <OutputPath>bin\$(Configuration)</OutputPath>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <Optimize>true</Optimize>
    <PackageOutputPath>packed</PackageOutputPath>
    <PackOnBuild>true</PackOnBuild>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="Xamarin.iOS" />
  </ItemGroup>
  <ItemGroup>
    <ObjcBindingApiDefinition Include="ApiDefinition.cs" />
  </ItemGroup>
  <ItemGroup>
    <ObjcBindingCoreSource Include="Structs.cs" />
  </ItemGroup>
  <ItemGroup>
    <Compile Remove="Structs.cs" Condition=" '$(EnableDefaultCompileItems)' == 'true' " />
    <Compile Remove="ApiDefinition.cs" Condition=" '$(EnableDefaultCompileItems)' == 'true' " />
  </ItemGroup>
  <ItemGroup>
    <None Remove="packed\**" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Xamarin.Build.Download" Version="0.4.11" />
    <PackageReference Include="NuGet.Build.Packaging" Version="0.2.2" />
  </ItemGroup>
  <ItemGroup>
    <None Update="build\**">
      <IncludeInPackage>true</IncludeInPackage>
    </None>
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.ObjCBinding.CSharp.targets" />
</Project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM