.NET Framework Bookmark and Share   
 index > MSBuild > How to expose an Item Group defined in a csproj file to targets called afterward by Team Build?
 

How to expose an Item Group defined in a csproj file to targets called afterward by Team Build?

Visual Studio project files contain a wealth of metadata. I want to capture some of the elements from a particular project in such a way that overridden Team Build targets can refer to them later in the process.

Let's say I have a project named ProjectFoo.csproj that looks like:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    ...etc...
  </PropertyGroup>  
  <ItemGroup>    
    <Reference Include="ProjectBar, Version=1.0.0.0, Culture=neutral, PublicKeyToken=asdf" />
    <None Include="Web.config" />    
    <Content Include="Index.aspx" />
    <Folder Include="Images" />
    <Compile Include="Code\MenuBuilder.cs" />
    <EmbeddedResource Include="Properties\licenses.licx" />    
    ...etc etc etc...
  </ItemGroup> 

  <!-- try to create & output some items dynamically -->
  <Target Name="AfterCompile" Outputs="@(ResourcesInFoo)">    
    <ItemGroup>
      <ResourcesInFoo Include="@(EmbeddedResource)" />
    </ItemGroup>
  </Target>
</Project>


Then in my Team Build scripts, I might have a snippet along these lines:

<Target Name="AfterDropBuild">
    <DoSomething Files=@(ResourcesInFoo) />
    <DoSomethingElse Param=@(FoldersInBar) />
    <!-- etc -->
</Target>


My understanding is that Team Build builds the projects by invoking the <MSBuild> task on each project file. And according to http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/4ca448c3-d571-4a4b-b96e-755309cd1a97, an Item that's created dynamically+ explicitly outputted from that target should become globally available (except to the immediate caller). Thus, I expected the <AfterCompile> override seen in my sample project file to work. It does not.

Are my assumptions about ItemGroup scope incorrect? Is there a completely different / better supported way to get what I want? Right now I'm offloading the entireprocess to Powershell via <Exec>, re-parsing the desired *proj files using native XML and operating on the resultsimperatively,but that feels like a hack.

Richard Berg
Hi Richard,

I tested in two seperate msbuild files with the same configuration at the link you mentioned. However, the item can't be accessed in both Build and AfterBuild targets. It seems that the nesteditems are updated only when build same project file.

I wonder if it is proper to update items from nested project files. It may causemore problems when there are many duplicate items in both files.

In addition to xml parser, you mayimplement a task to use the Project.Load api to load a project file and update its items to current project. Not sure if it is the best way, but it is another possible method.

Hongye Sun [MSFT]

MSDN Subscriber Support in Forum

If you have any feedback on our support, please contact msdnmg@microsoft.com




Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Hongye Sun

You can use google to search for other answers

Custom Search

More Threads

• Embedded resources in Web Application Project - AL.EXE problem
• msbuild BootstrapperFile Include for Office 2007 System Driver
• AxImp and ComReference with msbuild
• MSBuild gives LINK : fatal error LNK1104: cannot open file 'user32.lib'
• Memory not freed by devenv after Build Task completes
• Project Build order is different while building [through build server] then what it has been mentioned in visual studio.
• Start Installation of msi-package with command
• Target .NET 1.1 using Visual Studio 2005
• error MSB3323: Unable to find manifest signing certificate in the certificate store.
• Project.Save behavior different from XmlDocument.Save