I'm using .json files as a data source during development In a WP8 project.
The build action for these files is set to content.
When I'm building a production release, I would like to exclude these files from the generated .xap file for security reasons (as it would be like a blueprint of all webservices).
Pre- and post-build events are of no use since the .xap file is generated during the build. Since a xap is technically a zip, I could use a post build event with a custom tool that extracts, deletes, and re-packages it, but I would like to avoid that.
I could also apply the Condition parameter to each json file in the .csproj file:
<Content Condition="'$(Configuration)' == 'Debug'" Include="DesignData\authentication\testservice.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
But as there are +100 files, this too seems like a sub-optimal solution without creating another custom tool to do the job and to keep the csproj file updated.
I finally discovered I can execute a target right before the xap packaging, but the packaging fails when I remove the directory in the following way:
<PropertyGroup>
<FilesToXapDependsOn>$(FilesToXapDependsOn);AfterFilesToXapDependsOn</FilesToXapDependsOn>
</PropertyGroup>
<Target Name="AfterFilesToXapDependsOn">
<RemoveDir Directories="$(TargetDir)/DesignData" />
</Target>
You need to remove the files you wish to exclude from the Item
collection that MSBuild uses when creating the .xap file. If you just remove the directory, you will get errors because MSBuild still expects the files to be there.
At least in version 8.1 of Microsoft.WindowsPhone.Common.targets, it appears that the target you might want needs to be in AssignTargetPathsDependsOn
:
<PropertyGroup>
<AssignTargetPathsDependsOn>$(AssignTargetPathsDependsOn);RemoveJsonFiles</AssignTargetPathsDependsOn>
</PropertyGroup>
<Target Name="RemoveJsonFiles">
<ItemGroup>
<Content Condition=" '$(Configuration)' == 'Release' " Remove="**\*.json" />
</ItemGroup>
</Target>
You may have to find other, better extension points to add your target if this one does not work for you.
As you can see, the other thing to try is the Remove
property of the item named Content
. This property is only available on items within ItemGroup
elements within Target
elements.