<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:webfeeds="http://webfeeds.org/rss/1.0" xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>NuGet on Mart&#39;s blog</title>
    <link>https://blog.martdegraaf.nl/tags/nuget/</link>
    <description>Recent content in NuGet on Mart&#39;s blog</description>
    <webfeeds:analytics id="G-ZFQGR29JJQ" engine="GoogleAnalytics"/>
    <webfeeds:accentColor>0078D3</webfeeds:accentColor>
    <webfeeds:related layout="card" target="browser"/>
    <webfeeds:icon>https://gravatar.com/userimage/91624600/fb51d6d9172e5043a1e59d59f3f84e62.jpeg?v=1666815123000&amp;size=200</webfeeds:icon>
    <generator>Hugo -- 0.166.0</generator>
    <language>en-us</language>
    <managingEditor>blog@martdegraaf.nl (Mart de Graaf)</managingEditor>
    <webMaster>blog@martdegraaf.nl (Mart de Graaf)</webMaster>
    <copyright>Mart de Graaf</copyright>
    <lastBuildDate>Fri, 25 Nov 2022 17:22:17 +0100</lastBuildDate>
    <atom:link href="https://blog.martdegraaf.nl/tags/nuget/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Add project properties for consuming apps to a NuGet package</title>
      <link>https://blog.martdegraaf.nl/posts/add-properties-for-consuming-apps-to-a-nuget-package/</link>
      <pubDate>Wed, 19 Oct 2022 00:00:00 +0200</pubDate><author>blog@martdegraaf.nl (Mart de Graaf)</author>
      <guid>https://blog.martdegraaf.nl/posts/add-properties-for-consuming-apps-to-a-nuget-package/</guid>
      <description><![CDATA[This article explains how to add consuming project properties to a NuGet package. Those project properties will be used in the consuming apps.]]></description><content:encoded><![CDATA[<h1 id="introduction">Introduction</h1>
<p>For a recent project, I wanted to add a property to the consuming applications from within my NuGet package. This prevents  making a pull request for every consuming application with a .csproj change.</p>
<div style="
border: #0078D3 1px solid;
padding: 15px;
background: var(--entry);
border-left-width: 3px;">
   &#x1f4d3; Please note in this example the NuGet package has the ID: <code>MyProject.ExampleNuGet</code>, so replace that value for your nuget package. The consuming application is <code>MyProject.ConsumingWebApi</code>.
</div>
<h2 id="nuget-file-structure">NuGet file structure</h2>
<div class="codecontainer" hi="mart"><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">MyProject.ExampleNuGet  (Repository level)
</span></span><span class="line"><span class="cl"> ┣ MyProject.ExampleNuGet
</span></span><span class="line"><span class="cl"> ┃ ┣ Extensions
</span></span><span class="line"><span class="cl"> ┃ ┃ ┗ MySpecialThing.cs
</span></span><span class="line"><span class="cl"> ┃ ┣ MyProject.ExampleNuGet.csproj
</span></span><span class="line"><span class="cl"> ┃ ┗ MyProject.ExampleNuGet.props
</span></span><span class="line"><span class="cl"> ┣ MyProject.ExampleNuGet.sln
</span></span><span class="line"><span class="cl"> ┗ nuget.config</span></span></code></pre></td></tr></table>
</div>
</div></div><h2 id="myprojectexamplenugetprops">MyProject.ExampleNuGet.props</h2>
<p>The NuGet package has a .props-file to enforce some properties to the consumers.</p>
<div class="codecontainer" hi="mart"><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="line"><span class="cl"><span class="nt">&lt;Project&gt;</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&lt;PropertyGroup&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="c">&lt;!-- Enable output XML Docs for Swagger. --&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;GenerateDocumentationFile&gt;</span>true<span class="nt">&lt;/GenerateDocumentationFile&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="c">&lt;!-- Disable missing XML comment warnings. --&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;NoWarn&gt;</span>$(NoWarn);1591<span class="nt">&lt;/NoWarn&gt;</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&lt;/PropertyGroup&gt;</span>
</span></span><span class="line"><span class="cl"><span class="nt">&lt;/Project&gt;</span></span></span></code></pre></td></tr></table>
</div>
</div></div><h2 id="myprojectexamplenugetcsproj">MyProject.ExampleNuGet.csproj</h2>
<p>Important is to set the build action of the <code>MyProject.ExampleNuGet.props</code> file to package it to the build directory. See the example below:</p>
<div class="codecontainer" hi="mart"><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-xml" data-lang="xml"><span class="line"><span class="cl"><span class="nt">&lt;Project</span> <span class="na">Sdk=</span><span class="s">&#34;Microsoft.NET.Sdk&#34;</span><span class="nt">&gt;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="nt">&lt;PropertyGroup&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;TargetFramework&gt;</span>net6.0<span class="nt">&lt;/TargetFramework&gt;</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&lt;/PropertyGroup&gt;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="nt">&lt;ItemGroup&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;None</span> <span class="na">Update=</span><span class="s">&#34;MyProject.ExampleNuGet.props&#34;</span> <span class="na">Pack=</span><span class="s">&#34;true&#34;</span> <span class="na">PackagePath=</span><span class="s">&#34;build&#34;</span><span class="nt">&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&lt;/None&gt;</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&lt;/ItemGroup&gt;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nt">&lt;/Project&gt;</span></span></span></code></pre></td></tr></table>
</div>
</div></div><h2 id="conclusion">Conclusion</h2>
<p>When installing this package on for example <code>MyProject.ConsumingWebApi</code> a file is generated in de build folder <code>MyProject.ConsumingWebApi.csproj.nuget.g.targets</code>.
This ensures the setting is on when building <code>MyProject.ConsumingWebApi</code>.</p>
<h2 id="references">References</h2>
<p>I used the following resources to fix my problem.</p>
<ul>
<li><a href="https://stackoverflow.com/questions/67263924/create-nuget-containing-shared-project-properties-automatic-references" target="_blank" rel="noopener">https://stackoverflow.com/questions/67263924/create-nuget-containing-shared-project-properties-automatic-references</a></li>
<li><a href="https://learn.microsoft.com/en-us/nuget/concepts/msbuild-props-and-targets#packagereference-projects" target="_blank" rel="noopener">https://learn.microsoft.com/en-us/nuget/concepts/msbuild-props-and-targets#packagereference-projects</a></li>
</ul>
]]></content:encoded>
      <category>NuGet</category>
      <category>.NET</category>
      <category>csproj</category>
    </item>
  </channel>
</rss>
