Msbuild v15 can't resolve the variables of metadata of nuspec file
Asked Answered
F

1

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?.

Fussbudget answered 18/10, 2018 at 19:53 Comment(1)
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 github.com/Microsoft/msbuild/issuesInigo
A
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>
Absolve answered 20/10, 2018 at 8:11 Comment(4)
It works like a charm; i think this target should be part of msbuild because there is many reasons for using nuspec.Fussbudget
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.Absolve
This didn't help me. I was only able to get a build working when i put the values in the nuspecBentinck
@DonRhummy i suggest you create a new question with a reproducible example to see which issue you are running into.Absolve

© 2022 - 2024 — McMap. All rights reserved.