1

我在BeforeBuild事件中使用以下代码:

<XmlPeek XmlInputPath="SiteSettings.config" Query="appSettings/add[@key='cProjectNumber']/@value">
  <Output TaskParameter="Result" ItemName="value" />
</XmlPeek>
<PropertyGroup>
  <ProjectNumber>0@(value)</ProjectNumber>
</PropertyGroup>

然后让我访问$(ProjectNumber)其中将包含一个项目编号,如1234. 然后我使用它在类上包含一些特定于项目的覆盖。这工作正常。

我的问题是ProjectNumber在活动之外访问BeforeBuild

我想做的是修改<OutputPath>bin\</OutputPath>项目的,使其进入项目特定的文件夹(所以我知道哪些构建已完成)。我试过<OutputPath>bin\$(ProjectNumber)\</OutputPath>了,但我认为我的财产超出范围,因为它是空的。

有谁知道实现我想要的方法?

编辑:这是我尝试声明全局变量的方法:

<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 打印了正确的项目编号消息,但<OutputPath>bin\$(ProjectNumber)\</OutputPath>没有正确的值。

4

1 回答 1

1

您可以在 Project Global 级别中设置一个 Property,其名称ProjectNumber和空值作为占位符。您在目标中设置的ProjectNumberProperty 值InitializeBuild将设置 的全局级别属性ProjectNumber

通过查看下面的示例,您可以更好地理解它:

<?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>

在此示例中,我们ProjectNumber在 Global 级别将 Property 设置为空值,然后在InitializeBuildtarget 中将其设置为 value12345678作为InitializeBuild的一部分,InitialTargets然后通过DefaultTargets稍后,一旦在 target 中访问All它,它就具有 value 12345678

上述代码的输出如下:

>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).

所以你的问题可以用类似的方式解决。

于 2013-09-25T12:45:58.787 回答