简体   繁体   中英

add .cs and .json files into a multi platform nuget package

I'm migrating my applications from the .net framework to .net6 using a sdk project and package references instead of the package config.

Now I have a project that is packaged as a nuget that contains json schema files and generated C# class files. This nuget is distributed over several applications making sure everybody uses the same files. This means that the cs files need to be available design time and the json files are used for validating during runtime and therefor need to be copied to the output during build.

I changed the nuget project to sdk and I want to use the project file to specify the nuget contents. This is all working fine for .net framework 4.8 projects that are using the nuget. Both the cs and json files are added to the project. I see in the nuget both content and contentFiles folder structures appearing.
When I make the project multi platform I see that the contentfiles folder gets a new platform folder with the same files in it. From that perspective it all seems oke. But using the nuget in applications gives really strange behaviour. the Json or the CS files are in the project or neighter, but never both.

i moved back to an nuspec file to get more control and the only way I get some expected consistant behaviour is to move all files under the the contentFiles\any\any directory.

Obviously I'm doing something wrong can somebody give me a working example of their nuspec or even better csproj file that works for .net48 and .net6

the project stucture is nothing more than:

在此处输入图像描述

the csproj that I used looks like this

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFrameworks>net48;net6.0</TargetFrameworks>
        <Version>0.0.1</Version>
        <Authors>x</Authors>
        <Company>x</Company>
        <Copyright>X</Copyright>
        <Description>x</Description>
        <PackageReleaseNotes>First Release</PackageReleaseNotes>
    </PropertyGroup>

    <PropertyGroup>
        <IncludeBuildOutput>false</IncludeBuildOutput>
    </PropertyGroup>

    <ItemGroup>
        <Content Include="Messages/**/*.cs" buildAction="Compile">
        </Content>
    </ItemGroup>

    <ItemGroup>
        <Content Include="Json/**/*.json" buildAction="Content" copyToOutput="true">
        </Content>
    </ItemGroup>
</Project>

this creates the below output in the nuget package 在此处输入图像描述

the nuspec that i created

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
    <metadata>
        <id>x</id>
        <version>0.0.31</version>
        <title>x</title>
        <authors>x</authors>
        <description>x</description>
        <dependencies>
            <group targetFramework=".NETFramework4.8" />
            <group targetFramework="net6.0"/>
        </dependencies>
        <frameworkAssemblies>
            <frameworkAssembly assemblyName="System.Configuration" targetFramework="" />
            <frameworkAssembly assemblyName="System.Configuration.Install" targetFramework="" />
            <frameworkAssembly assemblyName="System" targetFramework="" />
            <frameworkAssembly assemblyName="System.ServiceProcess" targetFramework="" />
        </frameworkAssemblies>
        <contentFiles>
            <files include="any\any\Json\**\*.json" buildAction="Content" copyToOutput="true"/>
            <files include="cs\net48\Messages\**\*.cs" buildAction="Compile"/>
            <files include="cs\net6.0\Messages\**\*.cs" buildAction="Compile"/>
        </contentFiles>
    </metadata>
    <files>
        <file src="Json\**\*.json" target="content\Json"/>
        <file src="Messages\**\*.cs" target="content\Messages"/>
        
        <file src="Json\**\*.json" target="contentFiles\any\any\Json"/>
        <file src="Messages\**\*.cs" target="contentFiles\cs\net48\Messages"/>
        <file src="Messages\**\*.cs" target="contentFiles\cs\net6.0\Messages"/>
    </files>
</package>

this creates the below output in the nuget package 在此处输入图像描述

after a lot of trial and error I found a workable solution by manually adding all the files in the content/any/any folder for the nuget from the csproj both the directories are visible and the build action can be set for a specific file extension.

  <ItemGroup>
    <Content Include="Json/**/*.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <PackageCopyToOutput>true</PackageCopyToOutput>
      <IncludeInPackage>true</IncludeInPackage>
      <BuildAction>Content</BuildAction>
      <Pack>true</Pack>
      <PackagePath>contentFiles/any/any/Json;content/Json</PackagePath>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <Content Include="Messages/**/*.cs">
      <Pack>true</Pack>
      <PackagePath>contentFiles/any/any/Messages;content/Messages</PackagePath>
      <IncludeInPackage>true</IncludeInPackage>
      <BuildAction>Compile</BuildAction>
    </Content>
  </ItemGroup>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM