.targets file within nuget package - how to includ

2019-08-18 18:30发布

问题:

I have a nuget package like:

/build/SharedTargets.targets
/content/File.cs

SharedTargets.targets looks like:

<Compile Include="$(MSBuildThisFileDirectory)\..\content\File.cs">
  <Link>File.cs</Link>
  <Visible>True</Visible>
</Compile>

Is there a way how to reference the content files in more elegant way ? I.E. how to construct relative path to content properly.

回答1:

That's basically how to do it.

I'd suggest not putting .cs files in the content subfolder since they might end up in the project directory on legacy packages.config projects.

If you are only using PackageReference to reference the package, you could use contentFiles instead - a NuGet feature that lets you add files with an associated build action (Compile, EmbeddedResource,...) logically to the build process without needing a .targets file. See Using the contentFiles element for content files for the underlying NuGet mechanism.



标签: msbuild nuget