Willy Van den Driessche
Willy Van den Driessche

Reputation: 1759

Create nuget containing shared project properties - automatic references

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

Upvotes: 2

Views: 1254

Answers (2)

Mr Qian
Mr Qian

Reputation: 23808

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.

Upvotes: 3

Willy Van den Driessche
Willy Van den Driessche

Reputation: 1759

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?

Upvotes: 0

Related Questions