如何使用Build action“Content”将文件复制到项目中

时间:2012-06-11 16:58:40

标签: visual-studio-2010 msbuild-4.0

我们在VS2010之外创建了内容(文件/文件夹)。使用项目的“BeforeBuild”,我可以将temp目录中的文件导入到项目的相同目录结构中。导入后,我们如何告诉VS2010 IDE将Build Action识别为“内容”?在VS2010 IDE中,宏“包含在项目中”为您执行此操作。它与指定文件的MetaData有什么关系吗?这是我到目前为止所拥有的......

    <Target Name="BeforeBuild" Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " DependsOnTargets="ImportProperties;CustomImportsTarget" />
  <Target Name="ImportProperties">
    <PropertyGroup>
      <ImportSrcPath>$(temp)\$(MSBuildProjectName)\Imports</ImportSrcPath>
      <!-- File to save project references to -->
      <ImportLogFile>$(ImportSrcPath)\ImportResults.txt</ImportLogFile>
    </PropertyGroup>
  </Target>
  <Target Name="ImportLogger">
    <Delete Files="$(ImportLogFile)" />
    <WriteLinesToFile File="$(ImportLogFile)" Lines="Import Results%09============" />
  </Target>
  <Target Name="CustomImportsTarget" Condition="Exists('$(ImportSrcPath)')" DependsOnTargets="ImportLogger">
    <!--
        ==============================================================
        Allows for dynamically created content (files/folders) to be imported from our temp directory to our project's same
        directory structure as "Compile Include content"
        i.e. move %temp%\scripts\1.1.0\sample.js to <project>\scripts\1.1.0\sample.js
        ==============================================================
        -->
    <Message Text="==============================================================%0aCustomImportsTarget Begin%0a==============================================================" />
    <!-- Create an Item list that contains the files to import. $(ImportSrcPath) is the property that contains the location where 
            import source files are placed. -->
    <CreateItem Include="$(ImportSrcPath)\**\*" Exclude="$(ImportLogFile)">
      <Output TaskParameter="Include" ItemName="Files2Import" />
    </CreateItem>
    <!-- For testing purposes, Copy the files into our project. TODO:Should be a Move operation so project file modified dates are not constantly changing.  -->
    <Copy SkipUnchangedFiles="false" SourceFiles="@(Files2Import)" DestinationFolder="$(MSBuildProjectDirectory)\%(Files2Import.RecursiveDir)">
      <Output TaskParameter="CopiedFiles" ItemName="Changed" />
    </Copy>
    <!-- Imported files are now located in our project folders, 
        How do we assign properties to the files in @(Changed) so VS2010 IDE recognizes Build Action as "Content"???
        In VS2010 IDE, the macro "Include in Project" does this for you. Does it have anything to do with specifying a file's MetaData???
        -->
    <!-- Now send these values to the logger -->
    <Message Text="Changed:@(Changed->'%(Filename)%(Extension)')" Importance="high" />
    <Message Text="Script:@(Files2Import->'%(Filename)%(Extension)')" Importance="high" />
    <!--<Message Text="MSBuildProjectDirectory:$(MSBuildProjectDirectory)" Importance="high" />-->
    <Message Text="==============================================================%0aCustomImportsTarget End%0a==============================================================" />
  </Target>

1 个答案:

答案 0 :(得分:0)

我认为您不能在同一项目的BeforeBuild步骤中更改项目。即使它有可能也不是一个好主意。

您可以提前将这些文件添加到项目文件中,即使文件尚未存在。如果你不知道名字 - 只需使用* .cs之类的通配符。当构建开始时,它将使用您刚刚在预构建步骤中复制/创建的文件。