8

I know Since the release of msbuild 15 (vs 2017) that NuGet is now fully integrated into MSBuild.

I have a nuspec file with defining variables of package properties like:

    <metadata>
        <id>$id$</id>
        <version>$version$</version>  
        <authors>$authors$</authors>
    ...
    </metadata> 

The nuspec file is located in the same folder of the project.

When using nuget tool to create the package , it works fine.

    nuget pack   

When using msbuild v15, it raise an exception.

run the command:

    msbuild -version

Microsoft (R) Build Engine version 15.8.168+ga8fba1ebd7 for .NET Framework 15.8.168.64424

    msbuild  /t:pack /p:configuration=release    /p:NuspecFile=mylib.nuspec

raise exception:

C:\Program Files\dotnet\sdk\2.1.402\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(199,5): error : Value cannot be null or an empty string.

The strange is that dotnet sdk version 2.1.402 raises the exception.

I tried msbuild installed with vs2017 with its path and also it raises the same exception.

When i substitute the variables with its values, msbuild is working fine.

The question

Is this a bug in msbuild version 15.8.168.64424 or i missed something ?

In other words, Can msbuild support using the metadata variables of the package?.

M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • When you run `msbuild /t:pack`, MSBuild converts your `.csproj` to a `.nuspec` file. Thus, you should no longer use your own `.nuspec`. I think it is intentional that `msbuild` works differently from `nuget`. If you want certain behaviors, you can stick to `nuget`. Of course, you can discuss with Microsoft guys via GitHub https://github.com/Microsoft/msbuild/issues – Lex Li Oct 19 '18 at 00:09

1 Answers1

12

As has been mentioned in the comments, you no longer need a Nuspec file as most aspects can be controlled via properties in the csproj file or additional metadata on items (e.g. if you need additional content).

If you do need a nuspec file for some reason, you need to provide the variables for substitution yourself. You can do this in a target inside the csproj file like this:

<Target Name="SetNuspecProperties" BeforeTargets="GenerateNuspec">
  <PropertyGroup>
    <NuspecProperties>$(NuspecProperties);id=$(AssemblyName)</NuspecProperties>
    <NuspecProperties>$(NuspecProperties);config=$(Configuration)</NuspecProperties>
    <NuspecProperties>$(NuspecProperties);version=$(PackageVersion)</NuspecProperties>
    <NuspecProperties>$(NuspecProperties);description=$(Description)</NuspecProperties>
    <NuspecProperties>$(NuspecProperties);authors=$(Authors)</NuspecProperties>
  </PropertyGroup>
</Target>
Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • It works like a charm; i think this target should be part of msbuild because there is many reasons for using nuspec. – M.Hassan Oct 20 '18 at 11:58
  • btw if you're missing a use case you thing is needed frequently, you could also file an issue on NuGet/Home on GitHub. Currently, there are extensibility targets to add all sorts of files (contentFiles, libraries, runtime-specific assets, ...) and set metadata properties so the remaining use cases for .nuspec are very rare. – Martin Ullrich Oct 20 '18 at 16:20
  • This didn't help me. I was only able to get a build working when i put the values in the nuspec – Don Rhummy May 02 '19 at 13:03
  • @DonRhummy i suggest you create a new question with a reproducible example to see which issue you are running into. – Martin Ullrich May 02 '19 at 13:17