【问题标题】:VS2017 nuget package files treated as link instead of actual fileVS2017 nuget 包文件被视为链接而不是实际文件
【发布时间】:2018-06-04 09:48:02
【问题描述】:

我正在尝试使用 csproj 文件创建一个 nuget。我的文件被复制,但是当另一个项目导入 nuget 包时,我的 log4net.config(和其他)被视为链接。这些文件也不会在构建时复制到目标目录。 (见下方截图中的蓝色图标)

我的 cs 项目包含以下代码。

<ItemGroup>
  <Content Include="contentFiles/**/*.*;log4net.config" copyToOutput="true">
    <IncludeInPackage>true</IncludeInPackage>
    <CopyToOutput>true</CopyToOutput>
    <BuildAction>Content</BuildAction>
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </Content>
</ItemGroup>

我需要做什么才能使 log4net.config:

1 - 不被视为链接文件

2 - 默认复制到 outbut 目录。

【问题讨论】:

    标签: visual-studio-2017 nuget nuget-package


    【解决方案1】:

    我需要做什么才能使 log4net.config: 1 - 不被视为链接文件

    这是设计使然。 contentFiles 应该作为链接添加。不支持将文件复制到项目的源目录中,并且对于经典项目一直是不鼓励的做法。

    查看马丁的回答here

    这是设计使然。 NuGet 包不再应该修改项目的源,而只是添加到其构建的输出或构建过程中(除了 .NET 库)。 用于将源添加到项目的内容文件夹仅继续支持基于 packages.config 的项目,因为这将是现有项目的重大更改,但使用 project.json 或 PackageReference (VS 2017) 的项目会获得新的行为。

    现在,contentFiles 部分控制为这些文件生成的 msbuild 项到 obj\projectname.csproj.nuget.g.props 文件中。

    2 - 默认复制到 outbut 目录。

    您可以在 .nuspec 文件中为log4net.config 设置copyToOutput="true"

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
      <metadata>
        <id>xxxx</id>
        <version>1.0.0</version>
        <authors>Tester</authors>
        <owners>Tester</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Package Description</description>
        <contentFiles>
          <files include="any/any/Views/log4net.config" buildAction="content" flatten="true" copyToOutput="true"/>
      </metadata>
    
        <files>
          <file src="contentFiles/any/any/log4net.config" target="contentFiles/any/any/log4net.config" />
        </files>
    

    您可以查看文档NuGet ContentFiles Demystifiedthis thread了解更多详情。

    【讨论】:

    • 如何修复添加到项目文件中的“坏相对链接”呢? VS2019。 &lt;Content Update="C:\Users\bad_link_that_should_not_be_in_source\some_content"&gt;
    • 啊..它被添加到项目中[仅]当构建操作等被修改时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多