0
votes

I am trying to do create a nuget package by combining .nuspec and .csproj file.

My .csproj file looks like below:

<PropertyGroup>    
    <TargetFramework>netstandard2.0</TargetFramework>
    <Authors>Author name</Authors>
    <Company>Company name</Company>    

    <RepositoryUrl>RepositoryURL</RepositoryUrl>
    <VersionPrefix>0.0.1</VersionPrefix>
    <PackageId>SomeID</PackageId>
    <Description>Description</Description>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>    
    <NuspecFile>Projectname.nuspec</NuspecFile>     
  </PropertyGroup>

My .nuspec file looks like following:

<?xml version="1.0"?>
<package>   
   <files>
      <file src=" somefile.json" target="lib" />    
    </files>
</package>

Now, when I try dotnet pack on .csproj file it fails with folllowing error:

The required element 'metadata' is missing from the manifest.

I have also tried couple of things here.

  1. Tried to map properties explicitly as mentioned here:Unable to pack a NuGet package using dotnet CLI and nuspec file but it failed with the same errors

  2. Tried to put explicit metadata tag into the .nuspec file. like <metadata>$PackageId</metadata> hoping that it will fetch $package from .csproj file but that fails with following error:

The required element 'id' is missing from the manifest.

How can I combine both .csproj and .nuspec to do dotnet pack without running into this errors? I am pretty sure it is possible just that I am missing something.

Any suggestions?

The required element 'id' is missing from the manifest.

2

2 Answers

0
votes

When using NuGet's MSBuild pack target (dotnet pack is an alias for dotnet msbuild -t:pack), you can't mix-and-match a nuspec file and csproj. If you provide your own nuspec file, it must be complete, but the NuGet team doesn't recommend this. It's better to pretend nuspec no longer exists and is just some historic artifact of old versions of NuGet.

(Almost) All metadata can be specified in the csproj with the appropriate MSBuild property. MSBuild items of type Content automatically get put in the content/ and contentFiles/ folders. When you need to add additional files, you use the PackagePath metadata.

On a side note, putting a config file in the lib/ folder probably won't work as you want. Well, it might work when debugging .NET Core apps because assemblies are loaded from the global packages folder, where the packages are extracted. However, when publishing the app for deployment, the file won't be copied. But that's out of scope for this question.

0
votes

.nuspec has to have meta-data element. It would not build without it. While trying to combine .csproj and .nuspec make sure that the mandatory fields like

<Authors>Author name</Authors> <Company>Company name</Company>
<VersionPrefix>0.0.1</VersionPrefix> <PackageId>SomeID</PackageId>

are always set in .nuspec. inside meta-data tag Not in .csproj.