Satyajit
Satyajit

Reputation: 2061

Build target reference from .nuspec file

I am creating a build target in .csproj file in VS 2017

<Target Name="CopyPackage" AfterTargets="Pack">
    <Copy
        SourceFiles="$(OutputPath)..\$(PackageId).$(PackageVersion).nupkg"
        DestinationFolder="\\myshare\packageshare\"
        />
</Target>

The "PackageId" and "PackageVersion" need to be mentioned in the .csproj file to accomplish the goal.

<PackageId>My Package</PackageId>
<PackageVersion>1.0.0</PackageVersion>

But I have variables defined in .nuspec file.

Is it possible to access any variable from .nuspec file inside the .csproj?

Upvotes: 0

Views: 544

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100581

If you already have a .nuspec file and want to query its content, you can use MSBuild's XmlPeek task:

<Target Name="PrintVersions" AfterTargets="Pack">
  <XmlPeek Namespaces="&lt;Namespace Prefix='nu' Uri='http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd'/&gt;"
           XmlInputPath="$(NuspecFile)" 
           Query="/nu:package/nu:metadata/nu:id/text()">
    <Output TaskParameter="Result" PropertyName="MyPackageId" />
  </XmlPeek>
  <XmlPeek Namespaces="&lt;Namespace Prefix='nu' Uri='http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd'/&gt;"
           XmlInputPath="$(NuspecFile)" 
           Query="/nu:package/nu:metadata/nu:version/text()">
    <Output TaskParameter="Result" PropertyName="PackageVersion" />
  </XmlPeek>
  <Message Importance="high" Text="PackageId: $(PackageId)" />
  <Message Importance="high" Text="PackageVersion: $(MyPackageVersion)" />
</Target>

Upvotes: 1

Related Questions