Reputation: 15934
I am using the following code in the BeforeBuild
event:
<XmlPeek XmlInputPath="SiteSettings.config" Query="appSettings/add[@key='cProjectNumber']/@value">
<Output TaskParameter="Result" ItemName="value" />
</XmlPeek>
<PropertyGroup>
<ProjectNumber>0@(value)</ProjectNumber>
</PropertyGroup>
which then gives me access to $(ProjectNumber)
which will contain a project number like 1234
. I then use this to include some project specific overrides on classes. This is working fine.
My issue is accessing ProjectNumber
outside of the BeforeBuild
event.
What I would like to do is to modify the <OutputPath>bin\</OutputPath>
of the project so it goes into a project specific folder (so I know which builds are done). I tried <OutputPath>bin\$(ProjectNumber)\</OutputPath>
but I assume my property is out of scope as it's empty.
Does anyone know of a way to achieve what I want?
EDIT: This is what I have tried with declaring a global var:
<Project ToolsVersion="4.0" DefaultTarget="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectNumber></ProjectNumber>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\$(ProjectNumber)\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<Target Name="BeforeBuild">
<XmlPeek XmlInputPath="SiteSettings.config" Query="appSettings/add[@key='cProjectNumber']/@value">
<Output TaskParameter="Result" ItemName="value" />
</XmlPeek>
<PropertyGroup>
<ProjectNumber>0@(value)</ProjectNumber>
</PropertyGroup>
<ItemGroup>
<Compile Include="Accounts\$(ProjectNumber)\Objects\MyObject.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
</Target>
<Target Name="AfterBuild">
<Message Text="Proj:$(ProjectNumber)" Importance="High" />
</Target>
</Project>
AfterBuild has the correct project number message printed but <OutputPath>bin\$(ProjectNumber)\</OutputPath>
doesn't have the correct value.
Upvotes: 1
Views: 219
Reputation: 2326
You can set a Property in the Project Global level with the name ProjectNumber
and empty value as a place holder. The the ProjectNumber
Property value you set in the InitializeBuild
target will set the global level property of ProjectNumber
.
By looking at the sample below you can understand it better:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" InitialTargets="InitializeBuild" DefaultTargets="All" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectNumber></ProjectNumber>
</PropertyGroup>
<Target Name="All">
<Message Text="This is the value after I set it in InitializeBuild '$(ProjectNumber)'"/>
</Target>
<Target Name="BeforeBuild">
<Message Text="This is the value before I set it in InitializeBuild '$(ProjectNumber)'"/>
<PropertyGroup>
<ProjectNumber>12345678</ProjectNumber>
</PropertyGroup>
</Target>
</Project>
In this sample, we are setting Property ProjectNumber
with empty value at Global level and then in InitializeBuild
target, it is set with value 12345678
as InitializeBuild
is part of the InitialTargets
and then through DefaultTargets
later, once it is accessed in target All
it is having the value 12345678
.
The output of the above code goes as below:
>msbuild mybuild.msbuild
Microsoft (R) Build Engine version 4.0.30319.17929
[Microsoft .NET Framework, version 4.0.30319.17929]
Copyright (C) Microsoft Corporation. All rights reserved.
Build started 9/25/2013 5:35:22 AM.
Project "D:\CodeBase\COLNEW\COL-BIS\dev\mybuild.msbuild" on node 1 (default targets).
InitializeBuild:
This is the value before I set it in InitializeBuild''
All:
This is the value after I set it in InitializeBuild'12345678'
Done Building Project "D:\CodeBase\COLNEW\COL-BIS\dev\mybuild.msbuild" (default targets).
So your issue can be solved in similar fashion.
Upvotes: 1