Zum Inhalt springen
Skillpedia
Deployment & Cloud (DevOps)5.8k · #14596

copy-to-output-directory

Was er macht

Dieser Skill führt bei der Wahl, wie Projektdateien in die Build-Ausgabe kopiert werden, in MSBuild. Er ist für Entwickler, deren Builds langsam sind, weil Dateien bei jedem Build kopiert werden, oder deren Ausgabeordner veraltete Dateien behält.

Wann du ihn nutzt

  • ✓Wenn ein Build langsam ist, weil Dateien bei jedem Build kopiert werden.
  • ✓Wenn Ausgabedateien veraltet sind oder sich nach einer Änderung nicht aktualisieren.
  • ✓Wenn du zwischen den Kopiermodi für Inhalts- oder Datendateien wählen musst.

Link zum Skill

https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/copy-to-output-directory/SKILL.md

Kopiere ihn und füge ihn in deine KI ein (Claude, ChatGPT oder Cursor) mit der Bitte, den Anweisungen zu folgen.

So nutzt du ihn

Am einfachsten: Kopiere die Nachricht und füge sie in deine KI ein. Sie liest den Skill und folgt den Anweisungen.

Fertige Nachricht zum Einfügen

Nutze diesen Skill, um mir zu helfen: https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/copy-to-output-directory/SKILL.md Lies die SKILL.md unter diesem Link und folge ihren Anweisungen für meine Aufgabe.
Soll deine KI Skills wie diesen selbst finden?Installiere den kostenlosen Skillpedia-MCP: eine Zeile, und sie durchsucht vor jeder Aufgabe 15.000+ Skills.In 1 Minute installieren →

Beispielanfrage an deine KI

“Mein Build kopiert eine große Testdatendatei bei jedem Lauf, auch wenn sich nichts geändert hat. Wähle die richtige Kopiereinstellung.”

5 neue Skills pro Woche. 1 Minute Lesezeit.

So funktioniert es

Der Agent prüft, wie jede Datei zur Laufzeit genutzt wird, und empfiehlt den passenden Kopiermodus, etwa die neueste Datei beibehalten oder nur bei Unterschieden kopieren.

Technischer Inhalt (originale SKILL.md)

Originaltext des Autors, auf Englisch, direkt von GitHub.

Choosing a CopyToOutputDirectory Mode

Overview

The CopyToOutputDirectory metadata (and its publish counterpart CopyToPublishDirectory) controls whether an item — Content, None, EmbeddedResource, or Compile — is copied next to your build output, and under what conditions the copy happens. Picking the wrong mode causes either stale files in bin/ or an unnecessary per-build performance hit.

As of MSBuild 17.13 / .NET SDK 9.0.2xx there are four values:

Mode Copies when… Incremental cost Typical use
Never (default) Never None Files not needed at runtime
PreserveNewest Source is newer than destination (or destination missing) Cheap (timestamp check) The common case — source files you edit
Always Every build, unconditionally Expensive — copies on every build even in no-op builds Legacy workaround; avoid (see below)
IfDifferent Source differs from destination in either direction (newer or older, or size differs, or destination missing) Cheap (timestamp + size check) Destination may be mutated between builds
<ItemGroup>
  <None Include="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
  <None Include="testdata\seed.db"  CopyToOutputDirectory="IfDifferent" />
</ItemGroup>

You can use either the attribute form shown above or the child-element form:

<None Include="testdata\seed.db">
  <CopyToOutputDirectory>IfDifferent</CopyToOutputDirectory>
</None>

Why Always is usually the wrong choice

Always re-copies the file on every build, including otherwise-clean incremental/no-op builds. On projects with many or large content files this is a measurable, recurring cost and a common cause of "why is my no-op build not instant?" reports.

Historically Always was the only way to handle a specific scenario: the destination file can change between builds — for example an SQLite database, a storage/state file, or a config file that a test run mutates. With PreserveNewest, if the destination is modified (making its timestamp newer than the source) MSBuild will not restore the original source file, because the source is no longer newer. People reached for Always to force the file back into a known-good state — paying the copy cost on every build as a side effect.

IfDifferent: copy when different, in either direction

IfDifferent is the targeted fix for that scenario. It copies the source over the destination whenever MSBuild considers the two different — whether the source is newer or older than the destination, whether the size differs, or the destination is missing — and skips the copy when the destination is unchanged per MSBuild's heuristic.

Under the hood the _CopyDifferingSourceItemsToOutputDirectory target uses the Copy task with SkipUnchangedFiles="true". That "unchanged" check is a heuristic: it compares last-write timestamp and file size only — not a content hash — so a destination that was edited to the same size and timestamp as the source is treated as unchanged and is not re-copied. In practice this restores a mutated destination back to the source version on the next build (the reason people reached for Always) while avoiding the unconditional per-build copy.

Use IfDifferent when:

  • A test run or the app itself writes to the copied file (databases, caches, state/storage files, editable config) and you want each build to reset it to the source version.
  • You were using Always purely as a "keep the output in sync with the source" mechanism, not because you truly need a copy on every single build.
<ItemGroup>
  <!-- Reset the fixture DB to the source copy whenever it has drifted,
       but don't pay a copy on every no-op build. -->
  <None Include="fixtures\catalog.db" CopyToOutputDirectory="IfDifferent" />
</ItemGroup>

Globally softening Always with $(SkipUnchangedFilesOnCopyAlways)

If you have an existing codebase full of CopyToOutputDirectory="Always" items and want the performance benefit without editing every item, set the property:

<PropertyGroup>
  <SkipUnchangedFilesOnCopyAlways>true</SkipUnchangedFilesOnCopyAlways>
</PropertyGroup>

This makes the _CopyOutOfDateSourceItemsToOutputDirectoryAlways target pass SkipUnchangedFiles="true" to its Copy task, so Always items are only copied when they actually differ — effectively giving Always the same skip-uncha

Häufige Fragen

Welche Kopiermodi gibt es?

Never, PreserveNewest, Always und IfDifferent, wobei IfDifferent in neueren SDKs verfügbar ist.

Warum Always vermeiden?

Always kopiert bei jedem Build, auch wenn sich nichts geändert hat, und bremst inkrementelle Builds.

Deckt es die Ausgabe von Publish ab?

Ja. Es deckt auch die Kopiereinstellung für Publish ab.

Details

Originalbeschreibung des Autors

Choosing an MSBuild CopyToOutputDirectory / CopyToPublishDirectory mode: Never, PreserveNewest, Always, and IfDifferent (MSBuild 17.13+), plus $(SkipUnchangedFilesOnCopyAlways). USE FOR: removing the per-build Always copy perf hit; resetting output files mutated between builds. DO NOT USE FOR: general incremental-build diagnosis (use incremental-build); non-MSBuild build systems.

repo: dotnet/skills

Beliebtheit

GitHub-Sterne
5.8k
Platz im Ranking
#14596
Lizenz
MIT
Aktualisiert
2.11.2026

Ist dieser Skill nützlich?

0

Anmelden um abzustimmen.

Kommentare

  • …

Weitere Skills aus diesem Repository

Ähnliche Skills