2

We would like to create a nuget that contains an msbuild properties (.props) file. We do this by creating a nuspec which as the following MIL (most important line) :

  <files>
    <file src="SharedProperties.props" target="build\SharedProperties.props" />
  </files>

How can we change our .nuspec definition so that a project (.csproj) that references this nuget will automatically include the property file ("like" line 3 in this snippet):

<?xml version="1.0" encoding="utf-8"?>
  <Project Sdk="Microsoft.NET.Sdk">
    <Import Project="..\Shared\SharedProperties.props" />

(Is this even possible ?)

2 Answers 2

3

That is a feature of the nuget package design. And nuget has the automatic import targets mechanism. See this document.

The tip is that you should name the targets or props file to <package_id>.props or targets and then pack the file into build folder. That is all.

For an example, I created a lib project and then use this nuspec file to pack:

<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    <id>test_A</id>
    <version>1.0.0</version>
    <title>me</title>
    <authors>me</authors>
      ......
  </metadata>
<files>
<file src="test_A.props" target="build" />
</files>
</package>

If my package is called test_A.1.0.0.nupkg, the file should be named as test_A.props file.

Then, when you install the nuget package into a new project, you can check the <new project>.csproj file, the props file is added automatically during the nuget installation.

enter image description here

If you use PackageReference nuget management format to install the nuget package, the file is added under obj\xxx.csproj.nuget.g.props or obj\xxx.csproj.nuget.g.targets file:

enter image description here

For new-sdk project, that also work. If you create a new-sdk class library project, you could use this into csproj file to pack it:

<None Include="<packages_id>.props" Pack="true" PackagePath="build">

When you finish it, install the new package into new-sdk main project, you will find that the props file has imported automatically under obj\xxx.csproj.nuget.g.props file.

Sign up to request clarification or add additional context in comments.

1 Comment

I finally got it to work. I splitted my "props" file into 2 scripts. The first defines the path to the rest. And then I manually add an "imports" line to the csproj that uses the nuget (this is the part I still don't like but it does work). This allows me to keep the imports line version independent. I was also inspired by the nuget.org/packages/MSBuild.Microsoft.VisualStudio.Web.targets nuget. Thanks for the help.
0

According to the documentation the ".props" file should be automatically added to the beginning of a .csproj as an import (a .targets file should go to the end). However, for the new sdk-style projects this doesn't seem to work?

2 Comments

that is also work for new-sdk project. you could have a try.
you could use this <None Include="<packages_id>.props" Pack="true" PackagePath="build"> under csproj file. And then right-click on the project Properties-->Pack to generate the nupkg(this is for new-sdk class library project). After you install this new package into a new-sdk project, the props file will be imported under obj\xxx.csproj.nuget.g.props file when you install it..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.