본문으로 건너뛰기
Skillpedia
배포 및 클라우드 (DevOps)5.8k · #14596

copy-to-output-directory

하는 일

이 스킬은 MSBuild에서 프로젝트 파일이 빌드 출력에 어떻게 복사될지 고르는 과정을 안내합니다. 빌드할 때마다 파일이 복사되어 느리거나, 출력 폴더에 오래된 파일이 남는 개발자를 위한 스킬입니다.

사용하는 경우

  • ✓빌드할 때마다 파일이 복사되어 빌드가 느릴 때.
  • ✓변경 후에도 출력 파일이 오래되거나 업데이트되지 않을 때.
  • ✓콘텐츠나 데이터 파일의 복사 모드를 골라야 할 때.

스킬 링크

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

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/copy-to-output-directory/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“변경이 없어도 빌드마다 큰 테스트 데이터 파일을 복사합니다. 알맞은 복사 설정을 골라 주세요.”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

에이전트가 각 파일이 실행 시에 어떻게 쓰이는지 살펴보고, 최신 파일 유지나 다를 때만 복사처럼 맞는 복사 모드를 추천합니다.

기술 내용 (원본 SKILL.md)

작성자의 영어 원문을 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

자주 묻는 질문

복사 모드에는 무엇이 있나요?

Never, PreserveNewest, Always, IfDifferent가 있으며, IfDifferent는 최신 SDK에서 쓸 수 있습니다.

Always를 피해야 하는 이유는 무엇인가요?

Always는 변경이 없어도 매번 복사하므로 증분 빌드를 느리게 만듭니다.

게시 출력도 다루나요?

네. 게시용 복사 설정도 다룹니다.

상세 정보

작성자의 원본 설명

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

인기도

GitHub 스타
5.8k
순위
#14596
라이선스
MIT
업데이트
2026. 11. 2.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬