dotnet publish does not have some Nuget dependencies that are present in dotnet build

Rueckert, Andreas 6 Reputation points
2021-08-20T07:27:37.837+00:00

Let's assume I have the following project structure:

ExecutableProject
|
__ Project A
|
__ Project B
|
__ ContentFilesNugetPackage

The NuGet package contains some assets which are required by Library B at runtime. The nuspec file for the package looks like this:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>test.package</id>
<version>1.0.0</version>
<description>Test content</description>
<authors>demo</authors>
<contentFiles>
<files include="**/*" buildAction="content" copyToOutput="false" flatten="false" />
</contentFiles>
</metadata>
</package>

The problem was: The assets will not only be copied to the ExecutableProject's output path, but also to the output paths of Project A and Project B. This is a problem in case there are a lot of nuget packages, there are a lot of asset files and the project dependency graph is deep, because it increases the build time.

I got it to work by adding a buildTransitive .targets file which copies the content files only if the project is an executable project:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"ToolsVersion="15.0">
<!-- copy content to build output for executable projects only -->
<Target Name="AfterBuild" AfterTargets="Build" Condition="$(OutputType) == 'Exe'">
<ItemGroup>
<_ContentFiles Include="$(MSBuildThisFileDirectory)..\contentFiles\any\any***" />
</ItemGroup>
<Copy SourceFiles="@(_ContentFiles)" DestinationFolder="$(OutDir)\%(RecursiveDir)"SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" />
</Target>
</Project>

I set copyToOuptut in the .nuspec file to false to stop msbuild from copying the content files into every library project output folder. Now my problem is that when I use dotnet publish to build and deploy the application, the dependencies of the nuget package are not copied to the destination folder as well, whereas this works with dotnet build.

I have now already tried to set the "AfterTargets" value in the "buildtransitive.target" file to "Publish" but it didn't help.

Maybe someone has had the same problem as me and can tell me why dotnet publish does not work but dotnet build does?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,922 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,022 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.