防止引用的程序集 PDB 和 XML 文件复制到输出

发布于 2024-08-16 12:50:21 字数 340 浏览 10 评论 0原文

我有一个 Visual Studio 2008 C#/.NET 3.5 项目,其中包含用于压缩内容的构建后任务。但是我发现我还在输出目录(和 ZIP)中获取引用的程序集的 .pdb(调试)和 .xml(文档)文件。

例如,如果 MyProject.csproj 引用 YourAssembly.dll,并且 DLL 所在的目录中有 YourAssembly.xml 和 YourAssembly.pdb 文件,它们将显示在我的输出目录(和 ZIP)中。

我可以在压缩时排除 *.pdb,但不能全面排除 *.xml 文件,因为我有具有相同扩展名的部署文件。

有没有办法阻止项目复制引用的程序集 PDB 和 XML 文件?

I have a Visual Studio 2008 C#/.NET 3.5 project with a post build task to ZIP the contents. However I'm finding that I'm also getting the referenced assemblies' .pdb (debug) and .xml (documentation) files in my output directory (and ZIP).

For example, if MyProject.csproj references YourAssembly.dll and there are YourAssembly.xml and YourAssembly.pdb files in the same directory as the DLL they will show up in my output directory (and ZIP).

I can exclude *.pdb when ZIP'ing but I cannot blanket exclude the *.xml files as I have deployment files with the same extension.

Is there a way to prevent the project from copying referenced assembly PDB and XML files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

清晨说晚安 2024-08-23 12:50:21

我希望能够在主应用程序中添加和删除引用的程序集,同时避免需要维护需要删除或排除的文件。

我仔细研究了 Microsoft.Common.targets 寻找可行的东西,并找到了 AllowedReferenceRelatedFileExtensions 属性。默认为 .pdb; .xml 所以我在项目文件中明确定义了它。问题是你需要一些东西(空格是不够的),否则它仍然会使用默认值。

<Project ...>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    <AllowedReferenceRelatedFileExtensions>
      <!-- Prevent default XML and PDB files copied to output in RELEASE. 
           Only *.allowedextension files will be included, which doesn't exist in my case.
       -->
      .allowedextension
    </AllowedReferenceRelatedFileExtensions> 
  </PropertyGroup>

I wanted to be able to add and remove referenced assemblies in my primary application while avoiding the the need to maintain which files I needed to delete or exclude.

I dug through Microsoft.Common.targets looking for something that would work and found the AllowedReferenceRelatedFileExtensions property. It defaults to .pdb; .xml so I explicitly defined it in my project file. The catch is that you need something (whitespace is not sufficient) otherwise it will still use the default.

<Project ...>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    <AllowedReferenceRelatedFileExtensions>
      <!-- Prevent default XML and PDB files copied to output in RELEASE. 
           Only *.allowedextension files will be included, which doesn't exist in my case.
       -->
      .allowedextension
    </AllowedReferenceRelatedFileExtensions> 
  </PropertyGroup>
假装不在乎 2024-08-23 12:50:21

您还可以通过命令行指定:

MsBuild.exe build.file /p:AllowedReferenceRelatedFileExtensions=none

You can also specify this via the command line:

MsBuild.exe build.file /p:AllowedReferenceRelatedFileExtensions=none
熟人话多 2024-08-23 12:50:21

您可以添加类似于 del "$(TargetDir)YourAssembly*.xml", "$(TargetDir)YourAssembly*.pdb" 的构建后事件命令

You can add a Post Build event command similar to del "$(TargetDir)YourAssembly*.xml", "$(TargetDir)YourAssembly*.pdb"

病毒体 2024-08-23 12:50:21

前 2 个答案对我不起作用。我在此链接中找到了一个对我有用的解决方案。 http://kitsula.com/Article/How-to -从-msbuild中排除-xml-doc-files

以防万一,上面的链接不起作用:

  1. 在解决方案资源管理器中卸载项目

  2. 右键单击该项目并单击“编辑 *.csproj”

  3. 在每个环境的 PropertyGroup 部分中添加下一行。

  4. 重新加载并重建项目。

     ;
              *.pdb;
              *.xml
     
    

top 2 answers didn't work for me. I found a solution in this link which worked for me. http://kitsula.com/Article/How-to-exclude-xml-doc-files-from-msbuild.

Just in case, the above link is not working:

  1. Unload project in Solution Explorer

  2. Right click the project and click 'edit *.csproj'

  3. Add next lines in the PropertyGroup section of each environment.

  4. Reload and rebuild project.

     <AllowedReferenceRelatedFileExtensions>
              *.pdb;
              *.xml
     </AllowedReferenceRelatedFileExtensions>
    
你如我软肋 2024-08-23 12:50:21

这是一个相当老的问题,但由于没有关于如何通过 UI 关闭生成 PDB 和 XML 文件的答案,我认为为了完整性,它应该放在这里。

在 Visual Studio 2013 中:在项目属性中的“编译”选项卡下,取消选中“生成 XML 文档文件”,然后单击其下方的“高级编译选项”,并将“生成调试信息”更改为“无”,这样就可以解决问题。

This is a rather old question, but since there is no answer about how to turn off generating PDB and XML files via UI, i figured that it should be here for completeness.

In Visual Studio 2013: in project properties, under compile tab, uncheck "Generate XML documentation file", then click on "Advanced Compile Options" below that and change "Generate debug info" to "None", and that will do the trick.

小女人ら 2024-08-23 12:50:21

我对其他答案没有太多运气,我终于弄清楚如何通过使用内置的 "删除"命令,显然您需要一种特定的方式实现通配符,它是有点微妙,这里是您需要放入“CSPROJ”中的所有内容 (TargetDir 是一个内置变量,自动包含在“Project”标签下:

<Target Name="RemoveFilesAfterBuild">   
    <ItemGroup>
        <XMLFilesToDelete Include="$(TargetDir)\*.xml"/>
        <PDBFilesToDelete Include="$(TargetDir)\*.pdb"/>
    </ItemGroup>
    <Delete Files="@(XMLFilesToDelete)" />
    <Delete Files="@(PDBFilesToDelete)" />
</Target>

我在生成各种语言特定文件夹时也遇到了问题,如果您也遇到这个问题,您也可以删除未使用的文件夹也有特定于语言的文件夹。我选择仅在构建类型“发布”下触发此操作:

<ItemGroup>
    <FluentValidationExcludedCultures Include="be;cs;cs-CZ;da;de;es;fa;fi;fr;ja;it;ko;mk;nl;pl;pt;ru;sv;tr;uk;zh-CN;zh-CHS;zh-CHT">
        <InProject>false</InProject>
    </FluentValidationExcludedCultures> 
</ItemGroup>

<Target Name="RemoveTranslationsAfterBuild" AfterTargets="AfterBuild" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <RemoveDir Directories="@(FluentValidationExcludedCultures->'$(OutputPath)%(Filename)')" />

    <ItemGroup>
        <XMLFilesToDelete Include="$(TargetDir)\*.xml"/>
        <PDBFilesToDelete Include="$(TargetDir)\*.pdb"/>
    </ItemGroup>
    <Delete Files="@(XMLFilesToDelete)" />
    <Delete Files="@(PDBFilesToDelete)" />
</Target>

I didn't have much luck with the other answers, I finally figured out how to do this in my implementation by using the built in "Delete" command, apparently there is a specific way you need to implement wildcards, it's bit nuanced, here's everything you need to be put into your "CSPROJ" (TargetDir is a built in variable, included automatically) under the "Project" tag:

<Target Name="RemoveFilesAfterBuild">   
    <ItemGroup>
        <XMLFilesToDelete Include="$(TargetDir)\*.xml"/>
        <PDBFilesToDelete Include="$(TargetDir)\*.pdb"/>
    </ItemGroup>
    <Delete Files="@(XMLFilesToDelete)" />
    <Delete Files="@(PDBFilesToDelete)" />
</Target>

I've also had trouble with various language specific folders being generated, if you have that issue too, you can also remove unused language specific folders too. I've chosen to only trigger this under the build type "Release":

<ItemGroup>
    <FluentValidationExcludedCultures Include="be;cs;cs-CZ;da;de;es;fa;fi;fr;ja;it;ko;mk;nl;pl;pt;ru;sv;tr;uk;zh-CN;zh-CHS;zh-CHT">
        <InProject>false</InProject>
    </FluentValidationExcludedCultures> 
</ItemGroup>

<Target Name="RemoveTranslationsAfterBuild" AfterTargets="AfterBuild" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <RemoveDir Directories="@(FluentValidationExcludedCultures->'$(OutputPath)%(Filename)')" />

    <ItemGroup>
        <XMLFilesToDelete Include="$(TargetDir)\*.xml"/>
        <PDBFilesToDelete Include="$(TargetDir)\*.pdb"/>
    </ItemGroup>
    <Delete Files="@(XMLFilesToDelete)" />
    <Delete Files="@(PDBFilesToDelete)" />
</Target>
吻泪 2024-08-23 12:50:21

我的答案现在可能很简单,但我想分享我用来删除 xml 文件的 BAT 脚本(如果有相应的 dll)。如果您只想清理输出文件夹并且有其他不想删除的 xml 文件,那么它非常有用。

SETLOCAL EnableDelayedExpansion

SET targetDir=%1

ECHO Deleting unnecessary XML files for dlls

FOR %%F IN (%targetDir%*.xml) DO (

  SET xmlPath=%%~fF
  SET dllPath=!xmlPath:.xml=.dll!

  IF EXIST "!dllPath!" (
    ECHO Deleting "!xmlPath!"
    DEL "!xmlPath!"
  )
)

用法:

Cleanup.bat c:\my-output-folder\

我花了一个小时才完成这个简单的工作(感谢“延迟扩展”的东西),到处都是各种类型的搜索。希望对像我这样的其他BAT新手有帮助。

My answer might be trivial now but I'd like to share the BAT script I use to delete the xml files if there's a corresponding dll for it. It's useful if you just want to cleanup the output folder and has other xml files that don't want to remove.

SETLOCAL EnableDelayedExpansion

SET targetDir=%1

ECHO Deleting unnecessary XML files for dlls

FOR %%F IN (%targetDir%*.xml) DO (

  SET xmlPath=%%~fF
  SET dllPath=!xmlPath:.xml=.dll!

  IF EXIST "!dllPath!" (
    ECHO Deleting "!xmlPath!"
    DEL "!xmlPath!"
  )
)

Usage:

Cleanup.bat c:\my-output-folder\

It took me an hour to finish this simple work (thanks to the "delayed expansion" stuff) with all type of searching here and there. Hope it helps other BAT newbies like me.

城歌 2024-08-23 12:50:21

正如上面其他人建议的那样,使用 AllowedReferenceRelatedFileExtensions 属性对我来说无论是在项目文件中还是通过 msbuild 调用都效果很好。

但有一个警告,那就是它普遍有效。

如果我们有很多相互引用的项目,这也将删除属于顶部项目引用的底部项目的 pdb,而不仅仅是外部 pdb。

一种可能性是回退到基于项目的设置并将其仅添加到“底部”项目,但这仍然使得很难直接在顶部项目中摆脱中间外部引用,所以我不确定如何正确控制它。

在我看来,找到一种通用的方法来摆脱来自外部库的所有 pdb,但保留来自内部项目引用的所有 pdb

似乎对黄金分割有用的一件事是使用如下组合

/p:AllowedReferenceRelatedFileExtensions=.xml;.config /p:CopyLocalLockFileAssemblies=false 

CopyLocalLockFileAssemblies 属性设置为 false 以防止从 NuGet 包复制 PDB 文件,并且 AllowedReferenceRelatedFileExtensions 属性设置为从外部 NuGet 包排除“.pdb”文件,但包括所有其他相关文件(“.xml”和“.config”文件)。包含 PDB 文件的本地引用项目在构建期间仍会将其 PDB 文件复制到输出目录

Using the AllowedReferenceRelatedFileExtensions property as others suggested above works well for me either in project files or via msbuild call as well.

There is one caveat though, which is that it works universally.

If we have a lot of projects that reference each other, this will also remove those pdbs that belong to the bottom projects referenced in top projects as well, not just external pdbs.

A possibility is to fallback to project-based setting and add it only to "bottom" projects, but that still makes it hard to get away with intermediate external references directly in top projects, so I am not sure how to control this properly.

In my opinion, it would be great to find a universal way of getting rid of all pdbs coming from external libraries but keep all pdbs coming from internal project references

One thing that seems to work for golden-means is using a combination as below:

/p:AllowedReferenceRelatedFileExtensions=.xml;.config /p:CopyLocalLockFileAssemblies=false 

the CopyLocalLockFileAssemblies property is set to false to prevent the copying of PDB files from NuGet packages, and the AllowedReferenceRelatedFileExtensions property is set to exclude ".pdb" files from external NuGet packages, but include all other related files (".xml" and ".config" files). Locally referenced projects that include PDB files will still have their PDB files copied to the output directory during build

浸婚纱 2024-08-23 12:50:21

如果您只想排除 XML 文件(例如调试版本),您可以执行以下操作:

<AllowedReferenceRelatedFileExtensions>
  <!-- Prevent default XML from debug release  -->
      *.xml
 </AllowedReferenceRelatedFileExtensions>

基本上,列出的每个扩展名(以分号分隔)都将被排除。

If you only want to exclude the XML files (for say a debug release) you can do something like this:

<AllowedReferenceRelatedFileExtensions>
  <!-- Prevent default XML from debug release  -->
      *.xml
 </AllowedReferenceRelatedFileExtensions>

Basically, each extension (delimited by a semi-colon) listed will be excluded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文