Delayed evaluation of items in MSBUILD file

J

Joshua Flanagan

First, if there is a more appropriate newsgroup for MSBUILD questions,
please let me know.

I am trying to figure out how to create an item group that is evaluated
AFTER some tasks have already run. For example, the build file will
create a folder, and then copy various files into that folder. I then
want a task to do something with all of the files in that folder.
The way I currently have it setup will not work. I define a folder:
<PropertyGroup>
<BuildDir>Output</BuildDir>
</PropertyGroup>

And then an item that refers to the files in that folder:

<ItemGroup>
<CreatedFiles Include="$(BuildDir)\*" />
</ItemGroup>

And then use that item list when performing a task. (As you may know,
most tasks will not take wildcards as their parameters, they need an
expanded item list).
The problem is, the "CreatedFiles" item list will be evaluated when the
*build begins* (and the folder is empty). I want it to be evaluated when
it is *first used* (and files now exist in that folder).

Any ideas on how I can get the desired behavior (an item group that
references a list of files in a folder, after tasks have been run to
populate that folder)?

Here is a sample project file that will demonstrate the problem. The
first time you run it, it will create a file called FolderListing.txt,
with nothing in it. Run it again (without manually cleaning anything)
and the file will contain the names of the files in the Output folder.
The second run is the desired behavior - I need to figure out how to
make it work on the first run.


<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildDir>Output</BuildDir>
</PropertyGroup>

<ItemGroup>
<CreatedFiles Include="$(BuildDir)\*" />
</ItemGroup>

<Target Name="Clean">
<RemoveDir Directories="$(BuildDir)" />
</Target>

<Target Name="Setup" DependsOnTargets="Clean">
<MakeDir Directories="$(BuildDir)" />
<Touch AlwaysCreate="true" Files="$(BuildDir)\a.txt" />
<Touch AlwaysCreate="true" Files="$(BuildDir)\b.txt" />
</Target>

<Target Name="Build" DependsOnTargets="Setup">
<WriteLinesToFile File="FolderListing.txt" Lines="@(CreatedFiles)" />
</Target>
</Project>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top