Search code examples
visual-studiomsbuildnugetcsproj

How to add some lines to the project's csproj file after installing a nuget


I created a library which will be turned into a nuget that will run on the target assemblies. Let's call it NugLib.

Goal:

What I want is to add2,3 lines to the target assembly after it installed the NugLib. NugLib (the library) had a .targets file which copied some files into the target assembly.

I want to be able to add some lines to the project (the .csproj file) Not sure if it can be done via the .targets .props .nuspec or the lib's csproj during/after the installing nuget automatically.

Want to add these 3 lines to the target assembly (the nuget consumer):

  <Content Include=".pack\*.*;.pack\**;.pack\**\*" />
  <Import Project="build\build.targets" />
  <Import Project=".pack\package.csproj" Condition="Exists('.pack\package.csproj')" />

Solution

  • How to add some lines to the project's csproj file after installing a nuget

    Since you use a net standard 2.1 class library, you can use an easier way.

    You can follow these steps:

    1) create such project structure:

    enter image description here

    Note: you cannot use import="xxxx.csproj", it is illegal and one main csproj file cannot import another csproj file of the same level. Instead, you should change to use package.targets rather than packages.csproj.

    2) you should create a file named <package_id>.targets in the build folder so that it can import such node in your main project.csproj when you install such nuget package.

    In NugLib.targets file:

    <Project>
      <Import Project="..\contentFiles\any\any\build\build.targets" />
      <Import Project="..\contentFiles\any\any\package.targets" Condition="Exists('..\contentFiles\any\any\package.targets')" /> 
    </Project>
    

    3) upload NugLib project and then add these in NugLib.csproj file:

      <ItemGroup>  
         <Content Update=".pack\**" Pack="true" IncludeInPackage="true" PackagePath="content\any\any\;contentFiles\any\any\;;">
        </Content>
        <Content Update="build\build.targets" Pack="true" IncludeInPackage="true" PackagePath="content\any\any\;contentFiles\any\any\;;">    
        </Content>
        <None Update="build\NugLib.targets" Pack="true"  PackagePath="build\NugLib.targets" />
      </ItemGroup>
    

    4) Right-click on your project and select Pack, then the new NugLib nuget package will exist under bin\Debug\xxx.nupkg.

    Besides, if you want to rename the package, you can refer to this document.

    These files will automatically add in your main project. In net framework project, like this:

    enter image description here

    In net core project, it is under obj\ConsoleApp1.csproj.nuget.g.targets

    enter image description here

    To add project's ID/Title, you can set these properties in NugLib.csproj file directly:

    <PropertyGroup>
      <PackageId>AppLogger</PackageId>
      <Version>1.0.0</Version>
      <Authors>your_name</Authors>
      <Company>your_company</Company>
    </PropertyGroup>