Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 

(시리즈 글이 4개 있습니다.)
.NET Framework: 2111. C# - 바이너리 출력 디렉터리와 연관된 csproj 설정
; https://www.sysnet.pe.kr/2/0/13336

VS.NET IDE: 197. (OneDrive, Dropbox 등의 공유 디렉터리에 있는) C++ 프로젝트의 출력 경로 변경하기
; https://www.sysnet.pe.kr/2/0/13903

VS.NET IDE: 198. (OneDrive, Dropbox 등의 공유 디렉터리에 있는) C# 프로젝트의 출력 경로 변경하기
; https://www.sysnet.pe.kr/2/0/13907

VS.NET IDE: 199. Directory.Build.props에 정의한 속성에 대해 Condition 제약으로 값을 변경하는 방법
; https://www.sysnet.pe.kr/2/0/13917




Directory.Build.props에 정의한 속성에 대해 Condition 제약으로 값을 변경하는 방법

지난 글을 통해,

(OneDrive, Dropbox 등의 공유 디렉터리에 있는) C# 프로젝트의 출력 경로 변경하기
; https://www.sysnet.pe.kr/2/0/13907

빌드 결과물을 프로젝트 경로의 하위가 아닌, 별도로 지정한 임시 디렉터리로 지정할 수 있었는데요, 아쉽게도 여기엔 문제가 좀 있습니다. ^^;

한 가지 사례로, Web Application 유형의 프로젝트라면 Visual Studio에서 F5 디버깅 시 기본적으로 ./[프로젝트]/bin 디렉터리를 사용하기 때문에 (별도 디렉터리에 생성된) dll 파일을 찾을 수 없어 오류가 발생합니다.

이 문제를 해결할 수 있는 한 가지 방법으로, Condition 속성을 고려할 수 있는데요,

MSBuild conditions
; https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditions?view=vs-2022

그렇다면 이제 필요한 것은 Condition 식 내에 Web Application 프로젝트를 구분할 수 있는 적절한 속성을 찾아야 합니다. 음... 어떤 것이 좋을까요? ^^; 이를 위해 Web Application 프로젝트용의 csproj를 살펴보면,

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>
    </ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{9F65F804-0A96-4FC4-9F32-6A8DC1A897D7}</ProjectGuid>
    <ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>WebApplication1</RootNamespace>
    <AssemblyName>WebApplication1</AssemblyName>
    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
    <MvcBuildViews>false</MvcBuildViews>
    <UseIISExpress>true</UseIISExpress>
    <Use64BitIISExpress />
    <IISExpressSSLPort />
    <IISExpressAnonymousAuthentication />
    <IISExpressWindowsAuthentication />
    <IISExpressUseClassicPipelineMode />
    <UseGlobalApplicationHostFile />
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>

ProjectTypeGuids, UseIISExpress 정도가 후보로 떠오릅니다. (아무거나 고른) ProjectTypeGuids의 경우 "349c5851-65df-11da-9384-00065b846f21" 값이 있다면 MVC 프로젝트 유형을 나타내므로 Directory.Build.props에 아래와 같이 올바른 문법으로 사용해 볼 수 있지만,

<Project>
    <PropertyGroup Condition="$(ProjectTypeGuids.IndexOf('349c5851-65df-11da-9384-00065b846f21')) == -1">
        <OutputPath>$(BaseOutputPath)\$(Platform)\$(Configuration)\</OutputPath>
        <BaseIntermediateOutputPath>$(BaseOutputPath)\temp</BaseIntermediateOutputPath>
        <IntermediateOutputPath>$(BaseIntermediateOutputPath)\$(Platform)\$(Configuration)\</IntermediateOutputPath>
        ...[생략]...
    </PropertyGroup>
</Project>

실제로 저렇게 해보면 의도한 대로 동작하지 않습니다. 그 이유는, Directory.Build.props의 평가가 너무 이른 시기에 완료되므로 ProjectTypeGuids의 값이 저 시점에는 비어 있기 때문입니다. (UseIISExpress를 사용해도 마찬가지입니다.)

아이러니하게도, Directory.Build.props 파일의 속성이 프로젝트에 대해 매우 이른 시기에 적용된다는 점은 장점이면서도 단점이 된 것입니다. 즉, OutputPath 등의 적용이 프로젝트 로딩 초기에 이뤄지므로 /bin, 또는 /obj 디렉터리가 생성되지 않을 수 있었던 건데요, 오히려 그런 이른 평가 시점으로 인해 Condition 조건에 여타 다른 속성을 사용할 수 없다는 제약으로 작용합니다.

아쉬운 대로, 이런 제약을 해결할 수 있는 방법이 Target을 경유해 속성 변경을 하는 것입니다. 가령, PrepareForBuild 이전에 실행하도록 Target을 정의하고, 그 안에서 CallTarget + Condition을 적용하는 식으로 우회할 수 있습니다.

<Target Name="RollbackProperties">
    <PropertyGroup>
        <BaseOutputPath>$(ProjectDir)bin\</BaseOutputPath>
        <OutputPath>$(ProjectDir)bin\</OutputPath>
        <BaseIntermediateOutputPath>$(ProjectDir)obj\$(Configuration)\</BaseIntermediateOutputPath>
        <IntermediateOutputPath>$(ProjectDir)obj\$(Configuration)\</IntermediateOutputPath>
    </PropertyGroup>
</Target>

<Target Name="CSharpUseTempDirectory" BeforeTargets="PrepareForBuild">
    <CallTarget Condition="'$(UseIISExpress)' == 'true'" Targets="RollbackProperties" />
</Target>

(물론, 시점의 변화로 인해 위와 같은 상황에서 의도했던 /obj, /bin 디렉터리 생성을 막을 수는 없습니다.)




참고로, Condition 등의 조건식에 문자열을 중복 사용할 때는 escape 처리를 해야 합니다. 가령, 다음과 같이 사용하게 되면,

Condition="'$(ProjectTypeGuids.IndexOf('349c5851-65df-11da-9384-00065b846f21'))' == '-1'"

2중으로 홑따옴표를 사용해 오류가 발생합니다.

error  : An unexpected token "..." was found at character position ... in condition 

이런 경우 내부에 중첩된 홑따옴표를 backtick(`)으로 escape 처리하면 됩니다.

Condition="'$(ProjectTypeGuids.IndexOf(`349c5851-65df-11da-9384-00065b846f21`))' == '-1'"




만약 특정 속성의 값이 궁금하다면 Message Task를 사용해 확인해 볼 수 있습니다. 가령 다음과 같이 속성을 정의했다면,

<Project>

    ...[생략]...

    <PropertyGroup>
        <TestProp>$(ProjectTypeGuids)</TestProp>
    </PropertyGroup>
</Project>

적절하게 Target을 정의한 후, Message Task를 추가하면 빌드 시에 Output 창을 통해 속성 값을 확인할 수 있습니다.

<Project>
    ...[생략]...

    <Target Name="mywork" AfterTargets="CoreBuild">
        <Message Importance="high" Text="[TEST] $(TestProp)"/>
    </Target>
</Project>




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







[최초 등록일: ]
[최종 수정일: 5/1/2025]

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

비밀번호

댓글 작성자
 




... 121  122  123  124  125  126  127  128  129  130  131  132  133  [134]  135  ...
NoWriterDateCnt.TitleFile(s)
1797정성태11/4/201421194오류 유형: 255. 이벤트 로그 - The Adobe Flash Player Update Service service entered the stopped state.
1796정성태10/30/201428523개발 환경 구성: 249. Visual Studio 2013에서 Mono 컴파일하는 방법
1795정성태10/29/201430271개발 환경 구성: 248. Lync 2013 서버 설치 방법
1794정성태10/29/201425937개발 환경 구성: 247. "Microsoft Office 365 Enterprise E3" 서비스에 대한 간략 소개
1793정성태10/27/201426938.NET Framework: 474. C# - chromiumembedded 사용 - 두 번째 이야기 [2]파일 다운로드1
1792정성태10/27/201427090.NET Framework: 473. WebClient 객체에 쿠키(Cookie)를 사용하는 방법
1791정성태10/22/201426548VC++: 83. G++ - 템플릿 클래스의 iterator 코드 사용에서 발생하는 컴파일 오류 [5]
1790정성태10/22/201421254오류 유형: 254. NETLOGON Service is paused on [... AD Server...]
1789정성태10/22/201425149오류 유형: 253. 이벤트 로그 - The client-side extension could not remove user policy settings for '...'
1788정성태10/22/201426446VC++: 82. COM 프로그래밍에서 HRESULT 타입의 S_FALSE는 실패일까요? 성공일까요? [2]
1787정성태10/22/201435220오류 유형: 252. COM 개체 등록시 0x8002801C 오류가 발생한다면?
1786정성태10/22/201436661디버깅 기술: 65. 프로세스 비정상 종료 시 "Debug Diagnostic Tool"를 이용해 덤프를 남기는 방법 [3]파일 다운로드1
1785정성태10/22/201426022오류 유형: 251. 이벤트 로그 - Load control template file /_controltemplates/TaxonomyPicker.ascx failed [1]
1784정성태10/22/201432851.NET Framework: 472. C/C++과 C# 사이의 메모리 할당/해제 방법파일 다운로드1
1783정성태10/21/201426844VC++: 81. 프로그래밍에서 borrowing의 개념
1782정성태10/21/201423874오류 유형: 250. 이벤트 로그 - Application Server job failed for service instance Microsoft.Office.Server.Search.Administration.SearchServiceInstance
1781정성태10/21/201424942디버깅 기술: 64. new/delete의 짝이 맞는 경우에도 메모리 누수가 발생한다면?
1780정성태10/15/201428494오류 유형: 249. The application-specific permission settings do not grant Local Activation permission for the COM Server application with CLSID
1779정성태10/15/201423637오류 유형: 248. Active Directory에서 OU가 지워지지 않는 경우
1778정성태10/10/201422082오류 유형: 247. The Netlogon service could not create server share C:\Windows\SYSVOL\sysvol\[도메인명]\SCRIPTS.
1777정성태10/10/201425054오류 유형: 246. The processing of Group Policy failed. Windows attempted to read the file \\[도메인]\sysvol\[도메인]\Policies\{...GUID...}\gpt.ini
1776정성태10/10/201421860오류 유형: 245. 이벤트 로그 - Name resolution for the name _ldap._tcp.dc._msdcs.[도메인명]. timed out after none of the configured DNS servers responded.
1775정성태10/9/201423441오류 유형: 244. Visual Studio 디버깅 (2) - Unable to break execution. This process is not currently executing the type of code that you selected to debug.
1774정성태10/9/201429903개발 환경 구성: 246. IIS 작업자 프로세스의 20분 자동 재생(Recycle)을 끄는 방법
1773정성태10/8/201433463.NET Framework: 471. 웹 브라우저로 다운로드가 되는 파일을 왜 C# 코드로 하면 안되는 걸까요? [1]
1772정성태10/3/201422318.NET Framework: 470. C# 3.0의 기본 인자(default parameter)가 .NET 1.1/2.0에서도 실행될까? [3]
... 121  122  123  124  125  126  127  128  129  130  131  132  133  [134]  135  ...