【问题标题】:How to copy contentFiles of NuGet package references via MSBuild Copy task如何通过 MSBuild 复制任务复制 NuGet 包引用的 contentFiles
【发布时间】:2021-08-07 18:06:52
【问题描述】:

我们有多个存储库,每个存储库都有自己的 .editorconfig。显然,这些没有同步,这就是为什么我想通过 NuGet 包将 .editorconfig 从我们的框架解决方案(连同其他文件)分发到我们所有的存储库/解决方案,并通过简单的复制构建任务将其复制到解决方案目录.

我尝试执行以下操作:

创建一个项目“EditorConfigDistribution”,它应该包含主 .editorconfig 文件。

<Project Sdk="Microsoft.NET.Sdk">
  ...
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <NoDefaultExcludes>true</NoDefaultExcludes>
  </PropertyGroup>

   <ItemGroup>
    <Content Include=".editorconfig">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <Pack>true</Pack>
      <PackageCopyToOutput>false</PackageCopyToOutput>
      <PackagePath>contentFiles\any\any\content</PackagePath>
    </Content>
  </ItemGroup>
</Project>

这一切都按预期工作,我确实从其他解决方案中获得了我项目中所需的 .editorconfig 文件,它在文件夹 content/.editorconfig 中被引用为快捷方式(请参阅EditorConfigConsumer Project Structure)。 该文件只是对C:\Users\&lt;user&gt;\.nuget\packages\editorconfigdistribution\1.0.0\contentFiles\any\any\content\.editorconfig的引用。

现在,我想通过构建任务复制那个 .editorconfig 文件:

<Project Sdk="Microsoft.NET.Sdk">
  ...
  <ItemGroup>
    <PackageReference Include="EditorConfigDistribution" Version="1.0.0">
    </PackageReference>
  </ItemGroup>

  <Target Name="CopyEditorConfig" BeforeTargets="BeforeBuild">
    <ItemGroup>
      <EditorConfigFileToCopy Include="$(MSBuildProjectDirectory)\content\.editorconfig" />
    </ItemGroup>
    <Copy SourceFiles="@(EditorConfigFileToCopy)" DestinationFolder="$(MSBuildProjectDirectory)\.." SkipUnchangedFiles="true" UseHardlinksIfPossible="false" />
  </Target>
</Project>

但是,我确实收到以下错误:

Error MSB3030: Could not copy the file "C:\Users\weberma9\source\repos\<some_path>\EditorConfigConsumer\content\.editorconfig" because it was not found. (20, 5)

我可以理解无法找到该文件(因为它是一个快捷方式),但我只是想不出在我的构建任务中正确引用该快捷方式的方法。 我需要在&lt;EditorConfigFileToCopy Include="$(MSBuildProjectDirectory)\content\.editorconfig" /&gt; 的那一行更改什么?

当然,如果您对我的一般问题有更好的方法 - 我很高兴听到。

【问题讨论】:

标签: msbuild nuget .net-standard editorconfig


【解决方案1】:

我能够找到解决问题的方法: 我不仅提供了 .editorconfig,还通过我的 EditorConfigDistribution 项目提供了 Copy-Build 任务,现在看起来像这样:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <NoDefaultExcludes>true</NoDefaultExcludes>
  </PropertyGroup>

  <ItemGroup>
    <None Include="..\..\.editorconfig">
      <Link>Rules\.editorconfig</Link>
      <Pack>true</Pack>
      <PackageCopyToOutput>false</PackageCopyToOutput>
      <PackagePath>Rules\</PackagePath>
    </None>
  </ItemGroup>

  <ItemGroup>
    <None Include="build\**">
      <Pack>true</Pack>
      <PackageCopyToOutput>false</PackageCopyToOutput>
      <PackagePath>build\</PackagePath>
    </None>
  </ItemGroup>
</Project>

.props 文件很简单,并且由于将 .props 和 .target 添加到使用该包的项目中的约定(请参阅Include MSBuild props and targets in a package),它将始终在“BeforeBuild”之前执行.

EditorConfigDistribution.props(放置在构建文件夹中):

<Project>
  <Target Name="CopyEditorConfig" BeforeTargets="BeforeBuild">
    <ItemGroup>
      <EditorConfigFilesToCopy Include="$(MSBuildThisFileDirectory)..\Rules\.editorconfig" />
    </ItemGroup>
    <Copy SourceFiles="@(EditorConfigFilesToCopy)" DestinationFolder="$(SolutionDir).." SkipUnchangedFiles="true" UseHardlinksIfPossible="false" />
  </Target>
</Project>

【讨论】:

    猜你喜欢
    • 2020-03-05
    • 2014-09-13
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    相关资源
    最近更新 更多