2013-08-28

MSBuild - Copy Task using Items with Includes and Excludes

I struggled for a couple of hours on this one... until finally finding this useful answer from stackoverflow.com! My scenario was needing to copy some files in a pre-build step in a Visual Studio C# project. For every file I needed to copy, there was a counterpart for each of our supported languages. The naming convention for the files was like this:

MyFile.properties
MyFile_de_DE.properties
MyFile_fr_FR.properties

etc.

Here is what my csproj looked like:
<PropertyGroup>
    <ReportingEnglishLocFolder>$(SolutionDir)..\path\to\destination\</ReportingEnglishLocFolder>
</PropertyGroup>
<ItemGroup>
    <EnglishLocPropertiesFiles
        Include="$(ReportingBaseFolder)master\*.properties"
        Exclude="*_de_DE.properties"/>
</ItemGroup>
<Copy
        SourceFiles="@(EnglishLocPropertiesFiles)"
        DestinationFolder="$(ReportingEnglishLocFolder)" />

After attempting multiple combinations of wildcard patterns and having no success, I changed the search phrases I used to search the internet and found the stackoverflow thread.
The most important part from the thread, and the most important thing to remember about using Include and Exclude is:
If you use an absolute path for Include, you must use an absolute path for Exclude. If you use a relative path for Include, you must use a relative path for both.
So I changed to this and it worked like a champ!
<ItemGroup>
    <EnglishLocPropertiesFiles
        Include="$(ReportingBaseFolder)master\*.properties"
        Exclude="$(ReportingBaseFolder)master\*_de_DE.properties"/>
</ItemGroup>

So remember, whichever type of path you use for Include, you need to do the same for Exclude.

No comments: