문제 설명
.sln 파일의 속성을 MSBuild의 프로젝트 파일로 전달할 수 있습니까? (Is it possible to pass properties from an .sln file to project file in MSBuild?)
Consider the following snippet from an .sln file:
...
# Visual Studio 2010
Project("{FAE04EC0‑301F‑11D3‑BF4B‑00C04F79EFBC}") = "MainApp", "MainApp\MainApp.csproj", "{FC66E4A5‑0538‑47DC‑B450‑788B98D9461E}"
EndProject
...
And the following snippet from the corresponding MainApp.csproj file:
...
<ItemGroup Condition="$(LibAProjRef) == false">
<Reference Include="LibA, Version=1.0.0.0, Culture=neutral">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Lib\LibA.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition="$(LibAProjRef) == true">
<ProjectReference Include="..\LibA\LibA.csproj">
<Project>{2A45F32B‑182B‑4B8D‑A8A4‑1BFBF2E81CAD}</Project>
<Name>LibA</Name>
</ProjectReference>
</ItemGroup>
...
Basically what I'm trying to do here is create a project file that can use another library project, either already built, or as a project reference, depending on if the library project is present in the solution or not.
Is there a way to pass properties from the .sln file to the project file so that I can accomplish this?
Or: is there another way to achieve this?
참조 솔루션
방법 1:
I don't think you can pass properties from the .sln file, because MSBuild actually works with XML files!
What I could think of is to write a Custom MSBuild Task, and read the wanted values from the .sln file.
방법 2:
You can repurpose either the Configuration or Platform properties, which can be set in a dropdown of the solution, or use one of the properties Scott Bilas mentioned.
I was hijacking the $(Configuration) field, but now thanks to Scott's comment:
<ProjectReference Include="$(SolutionDir)\..\DataLib\DataLib.csproj">
I'm guessing you could go either way, and your use of conditions appears sound.
방법 3:
I had the same need for a post‑build event. I used the following solution in VS2015.
You can define your Property in MainApp.csproj inside the PropertyGroup tag like so:
<PropertyGroup Label="Globals">
<LibAProjRef>False</LibAProjRef>
</PropertyGroup>
and then when you build the solution you can say
MSBuild "MySln.sln" /m /t:rebuild /p:Configuration=Release /p:Platform=x64 /p:LibAProjRef=True
Note that the here the Properties are strings so your condition should be
Condition="'$(LibAProjRef)' == 'True'"
(by Amir Abiri、user900202、Lamarth、Alex Ilie)