Reputation: 16111
I have written some MSBuild scripts that allows me to build a few hundred projects. One requirements is to copy files into the build directory. I have an XML file that contains listing of what needs to be copied and where it needs to be copied into. This XML file is just an ItemGroup that I imported into the main project, and everything works great.
My only problem with that is my XML file with all the items is huge (I have hundreds of directories to copy from)! This is because all items have to have meta data attached, which means each copy declaration has to have 3 lines of code, and three XML element's. i.e.
<copy_to_build Include="$(FooDir)/BAZ/CoreDataFiles/*">
<Destination>$(BuildDir)//BAZ/</Destination>
</copy_to_build>
This amounts to tripling the size of my copy script and means it is difficult to maintain.
Ok, so I transformed my XML into this format, in another file:
<copy_to_build Source="$(FooDir)/BAZ/CoreDataFiles/*" Destination="$(BuildDir)//BAZ/" />
Which is more concise because it has one xml element per copy command. It is much easier to read.
The only problem with that is I can't use these as 'Items' in MSBuild because the meta data is missing, and Items can NOT have a 'Destination' attribute.
So I wrote a tool to 'convert' my concise XML back into the verbose MSBuild format. Then I was faced with the dilemna of how to get this transformed XML file back into my 'project' file. I did find that Imports have to be done at the project level: Therefore I can't 'transform' my xml file inside a task or a target. This means the file has to be ready by the time the Import is processed. Hence the xml file cannot be reevaluated inside my Target... So apparently I have to 'transform' my xml file before even calling MSBuild from the command line. Which adds complicated steps to my build process.
So my final question is, How do I read my (one element)XML file data and convert it into (three element)Items in a Target or Task?
I've heard of dynamic items but I'm still new enough to msbuild that I don't how to use them, or if they are relevant to my problem.
Thanks.
Upvotes: 2
Views: 2461
Reputation: 5107
I've found that the MSBuild Extension Pack is extremely useful for working with arbitrary XML files, specifically XmlFile and XmlTask. We use this sort of thing for managing/staging database changes.
That said, my suggestion is only in response to the question "How do I transform XML into Items using MSBuild?" @Sayed's point about whether or not it's suitable for your problem at hand is another matter entirely - worth thinking about though.
Upvotes: 1
Reputation: 44342
This is not the correct way to solve the problem that you are having (unless I don't understand the issue). Forget trying to generate MSBuild XML elements on the fly. Instead what you should do is the following.
Creating an MSBuild task is pretty easy. With MSBuild 4.0+ you can even define the entire task in an MSBuild file so that you don't have to worry about compiling it and building the assembly. For more info on that take a look at Inline Tasks.
If you take this approach it will be easier to implement and maintain as well as being a better approach overall. If you want more details on how to create tasks the best resource is my book.
Upvotes: 1