Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this but it is not very intutive. I've discussed this type of technique a few times on my <a href="http://www.sedodream.com" rel="noreferrer">blog</a> ( <em>which is currently down :(</em> ).</p> <p>So create a new file, I named it GetContentFiles.proj which is shown here.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Project ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt; &lt;ItemGroup&gt; &lt;Projects Include="WindowsFormsApplication1\WindowsFormsApplication1.csproj"/&gt; &lt;/ItemGroup&gt; &lt;!-- This target will be executed once for each file declared in the Project target --&gt; &lt;Target Name="PrintFiles" Outputs="%(Projects.Identity)"&gt; &lt;Message Text="PrintFiles" Importance="high"/&gt; &lt;MSBuild Projects="$(MSBuildProjectFile)" Targets="GetContentFiles" Properties="ProjectToGetFiles=%(Projects.Identity)"&gt; &lt;Output ItemName="projContent" TaskParameter="TargetOutputs"/&gt; &lt;/MSBuild&gt; &lt;Message Text="ProjContent: @(projContent)" Importance="high"/&gt; &lt;!-- Transform the projContent to have correct path --&gt; &lt;!-- Get the relative path to the project itself, this serves as the base for the Content files path --&gt; &lt;PropertyGroup&gt; &lt;_ProjRelativeDir&gt;%(Projects.RelativeDir)&lt;/_ProjRelativeDir&gt; &lt;/PropertyGroup&gt; &lt;!-- This item will contain the item with the corrected path values --&gt; &lt;ItemGroup&gt; &lt;ProjContentFixed Include="@(projContent-&gt;'$(_ProjRelativeDir)%(RelativeDir)%(Filename)%(Extension)')"/&gt; &lt;/ItemGroup&gt; &lt;!-- Create a new item with the correct relative dirs--&gt; &lt;Error Condition="!Exists('%(ProjContentFixed.FullPath)')" Text="File not found at [%(ProjContentFixed.FullPath)]"/&gt; &lt;/Target&gt; &lt;Import Project="$(ProjectToGetFiles)" Condition="'$(ProjectToGetFiles)'!=''"/&gt; &lt;Target Name="GetContentFiles" Condition="'$(ProjectToGetFiles)'!=''" Outputs="@(Content)"&gt; &lt;Message Text="Content : @(Content)" Importance="high"/&gt; &lt;Message Text="Inside GetContentFiles" Importance="high"/&gt; &lt;/Target&gt; &lt;/Project&gt; </code></pre> <p>I will try and explain this, but it may be tough to follow. Let me know if you need me to expand on it. This file has two targets <strong>PrintFiles</strong> and <strong>GetContentFiles</strong>. The entry point into this file is the PrintFiles target, in the sense that this is the target that you are going to call. So you call the <strong>PrintFiles</strong> target which it then uses the <a href="http://msdn.microsoft.com/en-us/library/z7f65y0d.aspx" rel="noreferrer">MSBuild task</a> to call the GetContentFiles target on itself, also it passes a value for the <strong>ProjectToGetFiles</strong> property. Because of that the <a href="http://msdn.microsoft.com/en-us/library/92x05xfs.aspx" rel="noreferrer">Import elemnent</a> will be executed. So what you are really doing is taking the project defined in the <strong>ProjectToGetFiles</strong> property and extending it to include the target GetContentFiles (and whatever other content is inside the GetContentFiles.proj file). So we are effectively <em>extending</em> that file. I'm calling this technique "MSBuild Inheritance" because. So inside the GetContentFiles target we can access all properties and items that are declared inthe ProjectToGetFiles property. So I take advantage of that by simply putting the content of the <strong>Content</strong> item into the outputs for the target, which can be accessed by the original file using the <strong>TargetOutputs</strong> from the MSBuild task.</p> <p>You mentioned in your post that you wanted to correct the path values to be the right ones. The problem here is that in the .csproj file all items are declared relative to the original project file. So if you "extend" the project file in this way from a file in a different directory you must correct the file path values manually. I've done this inside the <strong>PrintFiles</strong> target, check it out.</p> <p>If you execute the command <strong>msbuild GetContentFile.proj /fl /t:PrintFiles</strong> the result would be:</p> <pre><code>Build started 7/3/2009 12:56:35 AM. Project "C:\Data\Development\My Code\Community\MSBuild\FileWrites\GetContentFile.proj" on node 0 (PrintFiles target(s)). PrintFiles Project "C:\Data\Development\My Code\Community\MSBuild\FileWrites\GetContentFile.proj" (1) is building "C:\Data\Development\My Co de\Community\MSBuild\FileWrites\GetContentFile.proj" (1:2) on node 0 (GetContentFiles target(s)). Content : Configs\Config1.xml;Configs\Config2.xml Inside GetContentFiles Done Building Project "C:\Data\Development\My Code\Community\MSBuild\FileWrites\GetContentFile.proj" (GetContentFiles target(s)). PrintFiles: ProjContent: Configs\Config1.xml;Configs\Config2.xml Done Building Project "C:\Data\Development\My Code\Community\MSBuild\FileWrites\GetContentFile.proj" (PrintFiles target(s)). Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:00.03 </code></pre> <p>Sayed Ibrahim Hashimi</p> <p>My Book: <a href="http://rads.stackoverflow.com/amzn/click/0735626286" rel="noreferrer">Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload