将文件复制到输出目录问题.targets文件

问题描述

我正在使用目标文件在运行时生成一些文件,并将它们从中间目录(obj)复制到bin。

我的环境是.Net 5和相应的MSBUILD版本。

通常我的.targets文件可以工作,但是我有一个问题:

当文件在中间目录中生成时,它们保存在obj/x64/Debug/...FileName.fx之类的路径中,而整个路径正好复制到bin文件夹中的输出中。 例如,我的原始文件位于Effects/BasicEffect.fx中,但我想将此结构保留在输出中,但是它将复制到obj/x64/Debug/Effects/BasicEffect.fxo中,最终路径为:bin/x64/Debug/obj/x64/Debug/Effects/BasicEffect.fxo这就是我要修复的

这是我的目标文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

   <ItemGroup>
      <AvailableItemName Include="Effect"/>
      <!--<AvailableItemName Include="Font"/>-->
   </ItemGroup>
    
   <!-- In Project A -->  
    <UsingTask Architecture="*" TaskName="EffectCompilerTask" AssemblyFile="$(SolutionDir)CompilerTask\bin\Debug\net5.0\CompilerTask.dll"/>
    <UsingTask Architecture="*" TaskName="CompilerDependencyTask" AssemblyFile="$(SolutionDir)CompilerTask\bin\Debug\net5.0\CompilerTask.dll"/>
  
   <!-- 1st PART : When Project A is built,It will generate effectively the files -->
   <!-- Plugs all dependency listing in AssignTargetPathsDependsOn - in order to support generated output to be copied as part of the build  -->
   <PropertyGroup >
      <BuildDependsOn>
         EngineCompileTarget;
         $(BuildDependsOn);
      </BuildDependsOn>


      <IsBuildInDebug>true</IsBuildInDebug>

      <!--By default turn-on debugging on fx files when compiling in debug (no optimize)-->
      <EngineEffectDebugOption Condition="'$(EngineEffectDebugOption)' == ''">$(IsBuildInDebug)</EngineEffectDebugOption>

      <EffectDynamicCompilingOption Condition="'$(EffectDynamicCompilingOption)' == ''">$(IsBuildInDebug)</EffectDynamicCompilingOption>

      <GenerateCsFileFromEffect>false</GenerateCsFileFromEffect>
      <GenerateBinaryFromEffect>true</GenerateBinaryFromEffect>
      <GenerateCsFileFromFont>false</GenerateCsFileFromFont>
      <GenerateBinaryFromFont>true</GenerateBinaryFromFont>
      <CompileFontBitmaps>false</CompileFontBitmaps>
      
   </PropertyGroup>
    <Target Name="EngineContentAndCompileTarget" BeforeTargets="Build">
        <CompilerDependencyTask
                ProjectDirectory="$(ProjectDir)"
                IntermediateDirectory="$(IntermediateOutputPath)"
                Files="@(Effect)"
        >
            <Output ItemName="EngineContent" TaskParameter="ContentFiles" />
        </CompilerDependencyTask>

        <ItemGroup>
            <!--List Of fx compiled file-->
            <Content Include="@(EngineContent)" KeepMetadata="CopyToOutputDirectory"/>
        </ItemGroup>

    </Target>
    
<!-- Target used to calculate dependency output -->
   <Target Name="EngineCompileTarget" BeforeTargets="Build">
      <EffectCompilerTask
          Files="@(Effect)"
          ProjectDirectory="$(ProjectDir)"
          IntermediateDirectory="$(IntermediateOutputPath)"
        >
         </EffectCompilerTask>
         
      <Message Text="Hello from EngineCompileTarget" Importance="High"></Message>
      <Message Text="EngineCompileTarget @(Content)" Importance="High"></Message>
   </Target>

</Project>

这是我的Directory.buid.targets文件,我将其添加到正在构建解决方案的项目中。 这是此文件的内容:

<Project>
  <PropertyGroup>
      <AssignTargetPathsDependsOn>
         $(AssignTargetPathsDependsOn);
          EngineContentAndCompileTarget;
      </AssignTargetPathsDependsOn>
  </PropertyGroup>
  
</Project>

有人可以指出我在这里所缺少的内容,以便在保存原始文件结构的情况下将生成的文件正确地复制到输出中吗?

解决方法

也许您可以使用XCOPY来执行此操作。例如,我将整个文件夹复制到输出目录中。但是文件夹已经存在。我不确定它是否适用于生成的文件。

PropertiesBuild EventsPost-build event command line下,我这样做:

XCOPY "$(ProjectDir)myFolder" "$(TargetDir)myFolder" /s /i /y

enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...