Include files dynamically using msbuild while keeping relative path?

I am trying to include files from a second project as EmbeddedResources using the following msbuild target:

    <CreateItem Include="..\MyProject.Templates\**\*.css">
      <Output ItemName="EmbeddedResource" TaskParameter="Include" />
    </CreateItem>

      

but the included file will lose its path eg. ~\Views\_Layout.cshtml

included as _Layout.cshtml

(not Views._Layout.cshtml

as desired). Is there a way to achieve the desired effect?

+3


source to share


1 answer


MSBuild has New methods for manipulating elements and properties . Using these methods, you can map your resources using an ItemGroup (instead of CreateItem) and then create a different ItemGroup using MSBuild Transforms with MSBuild Known Item Metadata . There are many options for item metadata that you can use to achieve the desired effect. There's a prime example of the syntax for this answer .

I wrote a little script as an example. It creates an ItemGroup with * .exe files and converts them. Tested with MSBuild 3.5.



<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Project DefaultTargets="CreateItems" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="CreateItems">
    <ItemGroup>
      <Exe Include="..\**\*.exe" />
    </ItemGroup>
    <ItemGroup>
      <TransformedExe Include="@(Exe->'%(Relativedir)')"/>
    </ItemGroup>
    <Message Text="1 - @(Exe)" />
    <Message Text="2 - @(TransformedExe)" />
  </Target>
</Project>

      

+2


source







All Articles