Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 如何阻止NuGet打包包含不需要的文件_Visual Studio_Nuget_Nuget Package_Nuget Spec - Fatal编程技术网

Visual studio 如何阻止NuGet打包包含不需要的文件

Visual studio 如何阻止NuGet打包包含不需要的文件,visual-studio,nuget,nuget-package,nuget-spec,Visual Studio,Nuget,Nuget Package,Nuget Spec,我正在构建一个具有如下文件夹层次结构的NuGet包: ProjectDir Infrastructure class1.cs class2.cs Task class3.cs class3.cs StuffToInclude SampleCode sampleclass1.cs.pp sampleclass2.cs.pp Imag

我正在构建一个具有如下文件夹层次结构的NuGet包:

ProjectDir
    Infrastructure
        class1.cs
        class2.cs
    Task
        class3.cs
        class3.cs
    StuffToInclude
        SampleCode
            sampleclass1.cs.pp
            sampleclass2.cs.pp
        Images
            image1.gif
            image2.gif
        Doc
            text1.txt
            text2.txt
    Project.nuspec
content
    SampleCode
        sampleclass1.cs.pp
        sampleclass2.cs.pp
    Images
        image1.gif
        image2.gif
    Doc
        text1.txt
        text2.txt
content
    StuffToInclude
        Images
            image1.gif
            image2.gif
        Doc
            text1.txt
            text2.txt
    SampleCode
        sampleclass1.cs.pp
        sampleclass2.cs.pp
    Images
        image1.gif
        image2.gif
    Doc
        text1.txt
        text2.txt
注意:nuspec文件只需使用“nuget spec”命令生成,然后添加以下文件部分:

<files>
    <file src="StuffToInclude\**\*.*" target="content" />
</files>
然而,我得到的是一个稍大一点的内容文件夹,其中包含“StuffToInclude”文件夹的第二个副本,如下所示:

ProjectDir
    Infrastructure
        class1.cs
        class2.cs
    Task
        class3.cs
        class3.cs
    StuffToInclude
        SampleCode
            sampleclass1.cs.pp
            sampleclass2.cs.pp
        Images
            image1.gif
            image2.gif
        Doc
            text1.txt
            text2.txt
    Project.nuspec
content
    SampleCode
        sampleclass1.cs.pp
        sampleclass2.cs.pp
    Images
        image1.gif
        image2.gif
    Doc
        text1.txt
        text2.txt
content
    StuffToInclude
        Images
            image1.gif
            image2.gif
        Doc
            text1.txt
            text2.txt
    SampleCode
        sampleclass1.cs.pp
        sampleclass2.cs.pp
    Images
        image1.gif
        image2.gif
    Doc
        text1.txt
        text2.txt
请注意,不需要的StuffToInclude文件夹中没有SampleCode子文件夹——nuget打包程序不知何故发现,除非明确要求,否则不应将“.pp”文件放在内容中。但是所有其他文件(在所有其他文件夹中)都被不必要地复制,因此不希望将它们放在那里,因为当使用包时,目标项目中也会复制该文件夹

我想在nuspec中类似这样的东西可能会有所帮助:

<files>
    <file src="StuffToInclude\**\*.*" target="content" exclude="StuffToInclude\**\*.*"/>
</files>

但是我为“排除”属性尝试的所有变体似乎都没有帮助

如何将“StuffToInclude”文件排除在内容文件夹中

非常感谢

如何阻止NuGet打包包含不需要的文件

我假设您在创建包时使用的命令类似于
nuget pack xx.csproj

如果是这样的话,在创建.nuspec文件后基于项目文件创建包的特殊原因是什么

根据,我们可以基于指定的
.nuspec或项目文件
使用
nuget-pack
命令

因此,通常,如果我们基于
.csproj
创建一个包,就不需要创建
.nuspec
文件。另外,如果我们想基于
.nuspec
创建一个包,我建议使用命令
nuget pack xx.nuspec
而不是
nuget pack xx.csproj

问题原因:

我做了一些测试,发现当我们将
Project.nuspec
文件放在Project dir中,并使用类似
nuget pack xx.csproj
的命令时,nuget.exe将同时从读取
xx.nuspec和xx.csproj
,这就是为什么您有第二个副本的原因

1#同一目录下的
nuget pack xx.csproj
如果no
xx.nuspec
的结构:

content
    StuffToInclude
        Images
            image1.gif
            image2.gif
        Doc
            text1.txt
            text2.txt
2#
nuget pack xx.nuspec的结构:

content
    SampleCode
        sampleclass1.cs.pp
        sampleclass2.cs.pp
    Images
        image1.gif
        image2.gif
    Doc
        text1.txt
        text2.txt
当您基于存在ProjectName.nuspec的项目文件(.csproj)使用
nuget-pack
命令时,它同时从.csproj和.nuspec读取数据。所以它会产生第二个副本,就像你得到的一样

要解决此问题:

  • 保留xx.nuspec文件并使用类似于
    nuget pack xx.nuspec的命令(建议使用)

  • 删除xx.nuspec文件(或删除
    部分
    ),并使用类似
    nuget pack xx.csproj
    的命令,要将那些
    xx.cs.pp
    文件包括到内容文件夹中,您可能需要将它们的
    构建操作
    设置为
    内容

  • 保留xx.nuspec文件和xx.csproj文件(见下文)以及xx.nuspec文件中所需的所有内容,但为了避免不需要的第二次复制,请确保不想复制两次的每个项目都设置为
    构建操作


  • 我跟随你的脚步,得到了你想要的。因此,我认为您可能在打包过程中犯了一些错误,您是否可以共享.nuspec的更多内容?(删除id、作者等个人数据)以及是否使用命令
    nuget spec
    nuget pack xx.xxspec
    ?如果没有,请分享您使用的真实命令。谢谢Lance。我对你的两个解决方案都很赞同,但都不理想。我专门创建了NuSpec文件,以便列出框架依赖项、文件等。但是,在生成该文件时,它能够很好地引用来自MSBuild的变量参数(如$id$的标题等),但仅当包针对的是项目文件而不是NuSpec文件时。因此,似乎有很多想法使整个过程变得非常方便,但由于这个问题,似乎还有一个漏洞。还有其他见解吗?关于你的解决方案的进一步试验#2。。。。这样做的唯一方法似乎是用您希望通过NuGet复制到目标消费项目的实际内容污染源项目的目录层次结构。所以我想我已经证实了这在理论上是可能的,但肯定有点难看和难以维护。兰斯-你的工作激发了我解决这个问题的能力-我编辑了你的解决方案,包括一个新的#3解决方案如下:“保留xx.nuspec文件和xx.csproj文件(见下文)以及您想要在xx.nuspec文件中保存的所有内容,但为了避免不需要的第二次复制,请确保您不想复制两次的每个项目都设置为“无”构建操作。