Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 5개 있습니다.)
(시리즈 글이 10개 있습니다.)
VS.NET IDE: 60. Output 경로에 매크로 상수 사용하는 방법
; https://www.sysnet.pe.kr/2/0/688

개발 환경 구성: 91. MSBuild를 이용한 닷넷 응용프로그램의 플랫폼(x86/x64)별 빌드
; https://www.sysnet.pe.kr/2/0/963

개발 환경 구성: 93. MSBuild를 이용한 닷넷 응용프로그램의 다중 어셈블리 출력 빌드
; https://www.sysnet.pe.kr/2/0/965

개발 환경 구성: 102. MSBuild - DefineConstants에 다중 전처리 값 설정
; https://www.sysnet.pe.kr/2/0/988

개발 환경 구성: 115. MSBuild - x86/x64, .NET 2/4, debug/release 빌드에 대한 배치 처리
; https://www.sysnet.pe.kr/2/0/1017

개발 환경 구성: 372. MSBuild - 빌드 전/후, 배포 전/후 실행하고 싶은 Task 정의
; https://www.sysnet.pe.kr/2/0/11507

개발 환경 구성: 452. msbuild - csproj에 환경 변수 조건 사용
; https://www.sysnet.pe.kr/2/0/11985

개발 환경 구성: 580. msbuild의 Exec Task에 robocopy를 사용하는 방법
; https://www.sysnet.pe.kr/2/0/12716

개발 환경 구성: 693. msbuild - .NET Core/5+ 프로젝트에서 resgen을 이용한 리소스 파일 생성 방법
; https://www.sysnet.pe.kr/2/0/13481

닷넷: 2235. MSBuild - AccelerateBuildsInVisualStudio 옵션
; https://www.sysnet.pe.kr/2/0/13593




MSBuild - 빌드 전/후, 배포 전/후 실행하고 싶은 Task 정의

msbuild도 다른 빌드 도구들처럼 사용자가 임의로 Task를 작성해 추가할 수 있습니다. 예를 들면서 설명해 보면!

예제를 위해 Console Application 프로젝트를 하나 만들고 .csproj 파일의 내용에 다음과 같이 추가해 줍니다.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
...[생략]...

  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

  <Target Name="BeforeBuild">
       <Message Importance="high" Text="BeforeBuild(high) - Message Task $(MSBuildProjectFile)" /> 
       <Message Importance="normal" Text="BeforeBuild(normal) - Message Task $(MSBuildProjectFile)" /> 
  </Target>

  <Target Name="AfterBuild">
       <Message Importance="high" Text="AfterBuild - Message Task $(MSBuildProjectFile)" /> 
  </Target>

</Project>

이후 해당 프로젝트를 빌드하면 출력 창에 다음과 같은 내용을 확인할 수 있습니다.

1>------ Rebuild All started: Project: ConsoleApp1, Configuration: Debug Any CPU ------
1>  BeforeBuild(high) - Message Task ConsoleApp1.csproj
1>  ConsoleApp1 -> E:\ConsoleApp1\ConsoleApp1\bin\Debug\ConsoleApp1.exe
1>  AfterBuild - Message Task ConsoleApp1.csproj
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

msbuild는 Microsoft.CSharp.targets 파일 내에서 빌드의 전/후에 각각 BeforeBuild와 AfterBuild에 대한 Target이 정의되어 있으면 이를 호출하도록 하고 있기 때문에 저렇게 Message Task가 동작하게 되는 것입니다.




msbuild 스크립트 파일(C# 프로젝트인 경우 .csproj 파일)도 C++의 #include와 유사하게 다른 스크립트 파일을 import 노드를 이용해 포함할 수 있습니다. 일반적으로 비주얼 스튜디오에서 만든 C# 프로젝트는 다음의 스크립트 파일을 포함합니다.

<!-- %ProgramFiles(x86)%\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.CSharp.targets --> 
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

위의 Microsoft.CSharp.targets은 다시 .NET Framework이 설치된 폴더의 Microsoft.CSharp.targets을 import합니다.

<!-- C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.targets --> 
<CSharpTargetsPath>$(MSBuildFrameworkToolsPath)\Microsoft.CSharp.targets</CSharpTargetsPath>

.NET 설치 폴더에 있는 Microsoft.CSharp.targets은 동일 폴더의 Microsoft.Common.targets을 import하는데,

<!-- C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets --> 
<Import Project="Microsoft.Common.targets" />

(이 외에 프로젝트 유형에 따라 필요한 .targets 파일들이 import됩니다.)

바로 저 파일에 BeforeBuild와 AfterBuild가 이런 식으로 정의되어 있습니다.

  <PropertyGroup>
    <BuildDependsOn>
      BeforeBuild;
      CoreBuild;
      AfterBuild
    </BuildDependsOn>
  </PropertyGroup>

  <!--
    ============================================================
                                        BeforeBuild
    Redefine this target in your project in order to run tasks just before Build
    ============================================================
    -->
  <Target Name="BeforeBuild"/>

  <!--
    ============================================================
                                        AfterBuild
    Redefine this target in your project in order to run tasks just after Build
    ============================================================
    -->
  <Target Name="AfterBuild"/>

하지만, 저것은 로슬린 이전에 그랬고 Roslyn이 도입되면서 다음의 .targets 파일이 import 되고,

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets

Microsoft.CSharp.Core.targets 파일에서 다시 import하는 아래의 파일에 BeforeBuild, AfterBuild가 정의되어 있습니다.

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets

로슬린 전/후에 상관없이 BeforeBuild/AfterBuild의 처리 방식은 본격적인 빌드 작업을 담당하는 CoreBuild의 전/후에 비어 있는 Target 작업을 하나씩 미리 정의해 둔 것으로 동일합니다. 그런데 여기서 주의할 점이 하나 있습니다. 주석에 써진 데로 이것은 "redefine"이기 때문에 동일한 Target Name을 갖는 작업이 2개 이상 정의되어 있으면 나중에 정의된 것이 덮어써서 동작하는 부작용이 있습니다. 가령, Microsoft.Common.CurrentVersion.targets 파일을 편집해 BeforeBuild를 다음과 같이 수정했다면,

<Target Name="BeforeBuild">
    <Message Importance="high" Text="===================" />
</Target>

이후 여러분들의 .csproj 파일에서 BeforeBuild Target을 재정의하지 않았다면 빌드 시 "===================" 메시지가 보이겠지만, 재정의하는 순간부터 저 메시지를 볼 수 없습니다.

이와 함께 또 다른 문제가 있는데요, msbuild의 모든 Target 작업들이 저런 식으로 Befoer, Core, After와 같은 식으로 정의된 것은 아니기 때문에 다른 작업의 전/후로 어떤 처리를 하고 싶다면 Core를 복사해서 Before/After를 처리하는 구조로 변경해야 하는 등의 번거로움이 있습니다.

그리고 바로 이런 2가지의 문제를 극복하기 위해 나온 것이 MSBuild 4.0 이후부터 제공되는 Target 노드의 BeforeTargets / AfterTargets 속성입니다. 이를 이용하면 기존 BeforeBuild와 AfterBuild를 다음과 같은 형식으로 정의할 수 있습니다.

<Target Name="MyBeforeBuild" BeforeTargets="CoreBuild">
    <Message Text="Project File Name: $(MSBuildProjectFile)" /> 
</Target>

<Target Name="MyAfterBuild" AfterTargets="CoreBuild">
    <Message Text="Project File Name: $(MSBuildProjectFile)" /> 
</Target>




CoreBuild가 BeforeBuild/AfterBuild를 제공하는 것처럼, Publish 작업 역시 BeforePublish, AfterPublish를 제공합니다. 따라서, 배포(Publish) 전/후에 Target Task를 추가하는 것은 .csproj 파일에 다음과 같이 추가만 해주면 됩니다.

<Target Name="BeforePublish">
    <Message Importance="high" Text="=BeforePublish=" />
</Target>

<Target Name="AfterPublish">
    <Message Importance="high" Text="=AfterPublish=" />
</Target>

그런데, BeforePublish, AfterPublish를 정의하기 보단 BeforeTargets/AfterTargets을 이용한 정의가 더 낫습니다. 왜냐하면 일부 (예: ASP.NET Core 웹 애플리케이션) 프로젝트의 경우 BeforePublish, AfterPublish를 정의하면 실행이 안되는 반면 BeforeTargets/AfterTargets으로 다음과 같이 정의하면 실행이 잘 됩니다.

<Target Name="MyBeforePublish" BeforeTargets="Publish">
    <Message Importance="high" Text="$(MSBuildProjectFile)" /> 
</Target>

<Target Name="MyAfterPublish" AfterTargets="Publish">
    <Message Importance="high" Text="$(MSBuildProjectFile)" /> 
</Target>

참고로, 여러분들의 빌드 작업에 어떤 target 작업들이 실행되고 있는지 보고 싶다면 비주얼 스튜디오의 "TOOLS" / "Options" 메뉴에서 "Projects and Solutions" / "Build and Run" 범주의 "MSBuild project build output verbosity" 항목을 "Minimal"에서 다른 세부 항목 값으로 설정하면 됩니다.

sysmon_test_2.png

또한, 이 글의 예제에서 Message Task의 Importance 설정을 high로 했는데, 기본 값인 normal(또는 low)로 설정한 경우 위의 verbosity 설정 수준에 따라 출력 유무가 결정됩니다. (즉, 기본 verbosity 설정인 Minimal에서는 high로 설정한 메시지들만 Output 창에 보입니다.)

그 외에, msbuild 스크립트에서 사용 가능한 변수들의 목록은 다음의 글을 참고할 수 있습니다.

MSBuildToolsPath: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin
MSBuildFrameworkToolsPath: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\

Team Build에 사용되는 각종 Property 값
; https://www.sysnet.pe.kr/2/0/508




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 2/21/2022]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  67  68  69  70  71  [72]  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
11841정성태3/13/201910205VS.NET IDE: 132. Visual Studio 2019 - CMake의 컴파일러를 기본 g++에서 clang++로 변경
11840정성태3/13/201911295오류 유형: 526. 윈도우 10 Ubuntu App 환경에서는 USB 외장 하드 접근 불가
11839정성태3/12/201914024디버깅 기술: 124. .NET Core 웹 앱을 호스팅하는 Azure App Services의 프로세스 메모리 덤프 및 windbg 분석 개요 [3]
11838정성태3/7/201916810.NET Framework: 811. (번역글) .NET Internals Cookbook Part 1 - Exceptions, filters and corrupted processes [1]파일 다운로드1
11837정성태3/6/201926412기타: 74. 도서: 시작하세요! C# 7.3 프로그래밍 [10]
11836정성태3/5/201914348오류 유형: 525. Visual Studio 2019 Preview 4/RC - C# 8.0 Missing compiler required member 'System.Range..ctor' [1]
11835정성태3/5/201914113.NET Framework: 810. C# 8.0의 Index/Range 연산자를 .NET Framework에서 사용하는 방법 및 비동기 스트림의 컴파일 방법 [3]파일 다운로드1
11834정성태3/4/201913019개발 환경 구성: 432. Visual Studio 없이 최신 C# (8.0) 컴파일러를 사용하는 방법
11833정성태3/4/201913759개발 환경 구성: 431. Visual Studio 2019 - CMake를 이용한 공유/실행(so/out) 리눅스 프로젝트 설정파일 다운로드1
11832정성태3/4/201910805오류 유형: 524. Visual Studio CMake - rsync: connection unexpectedly closed
11831정성태3/4/201910398오류 유형: 523. Visual Studio 2019 - 새 창으로 뜬 윈도우를 닫을 때 비정상 종료
11830정성태2/26/201910196오류 유형: 522. 이벤트 로그 - Error opening event log file State. Log will not be processed. Return code from OpenEventLog is 87.
11829정성태2/26/201912106개발 환경 구성: 430. 마이크로소프트의 CoreCLR 프로파일러 예제 빌드 방법 - 리눅스 환경 [1]
11828정성태2/26/201918496개발 환경 구성: 429. Component Services 관리자의 RuntimeBroker 설정이 2개 있는 경우 [8]
11827정성태2/26/201912393오류 유형: 521. Visual Studio - Could not start the 'rsync' command on the remote host, please install it using your system package manager.
11826정성태2/26/201912275오류 유형: 520. 우분투에 .NET Core SDK 설치 시 패키지 의존성 오류
11825정성태2/25/201917143개발 환경 구성: 428. Visual Studio 2019 - CMake를 이용한 리눅스 빌드 환경 설정 [1]
11824정성태2/25/201911939오류 유형: 519. The SNMP Service encountered an error while accessing the registry key SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration. [1]
11823정성태2/21/201913404오류 유형: 518. IIS 관리 콘솔이 뜨지 않는 문제
11822정성태2/20/201911551오류 유형: 517. docker에 설치한 MongoDB 서버로 연결이 안 되는 경우
11821정성태2/20/201912019오류 유형: 516. Visual Studio 2019 - This extension uses deprecated APIs and is at risk of not functioning in a future VS update. [1]
11820정성태2/20/201915156오류 유형: 515. 윈도우 10 1809 업데이트 후 "User Profiles Service" 1534 경고 발생
11819정성태2/20/201913814Windows: 158. 컴퓨터와 사용자의 SID(security identifier) 확인 방법
11818정성태2/20/201912734VS.NET IDE: 131. Visual Studio 2019 Preview의 닷넷 프로젝트 빌드가 20초 이상 걸리는 경우 [2]
11817정성태2/17/20199910오류 유형: 514. WinDbg Preview 실행 오류 - Error : DbgX.dll : WindowsDebugger.WindowsDebuggerException: Could not load dbgeng.dll
11816정성태2/17/201912413Windows: 157. 윈도우 스토어 앱(Microsoft Store App)을 명령행에서 직접 실행하는 방법
... 61  62  63  64  65  66  67  68  69  70  71  [72]  73  74  75  ...