MSBuild. Create EmbeddedResource before build

五迷三道 提交于 2019-12-31 04:46:06

问题


I want to embed local references in the assembly before compiling the main unit. But the written target does not work.

  <Target Name="EmbedLocal" BeforeTargets="CoreCompile">
    <Message Text="Run EmbedLocal for $(MSBuildProjectFullPath)..." Importance="high"/>    
    <ItemGroup>
      <EmbeddedResource Include="@( ReferencePath->WithMetadataValue( 'CopyLocal', 'true' )->Metadata( 'FullPath' ) )"/>
    </ItemGroup>
    <Message Text="Embed local references complete for $(OutputPath)$(TargetFileName)." Importance="high" />
  </Target>

@(EmbeddedResource) at this moment contains valid list of paths.

Update:
Now my import file contains:

<Project ToolsVersion="$(MSBuildToolsVersion)" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <EmbedLocalReferences Condition=" '$(EmbedLocalReferences)' == '' ">True</EmbedLocalReferences>
  </PropertyGroup>

  <Target Name="EmbedLocal" BeforeTargets="ResolveReferences" Condition=" '$(EmbedLocalReferences)' == 'True' ">
    <Message Text="Run EmbedLocal for $(MSBuildProjectFullPath)..." Importance="high"/>
    <ItemGroup>
      <EmbeddedResource Include="@(ReferenceCopyLocalPaths->WithMetadataValue( 'Extension', '.dll' )->Metadata( 'FullPath' ))">
        <LogicalName>%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
      </EmbeddedResource>
    </ItemGroup>   
    <Message Text="Embed local references complete for $(OutputPath)$(TargetFileName)." Importance="high" />
  </Target>
</Project>

It works fine. Output assembly contains all .dll references as EmbeddedResource.


回答1:


MSBuild. Create EmbeddedResource before build

You can try to use BeforeBuild action to the csproj file to include the embedded resources:

  <Target Name="BeforeBuild">
    ... 
    <ItemGroup>
      <EmbeddedResource Include="..."/>
    </ItemGroup>
    ...
  </Target>

Now MSBuild will add this file as embedded resource into your assembly.

Update:

Thanks @Martin Ullrich. He pointed out the correct direction, we could use <Target Name="EmbedLocal" BeforeTargets="PrepareForBuild"> in the Directory.Build.props to resolve this issue. You can check if it works for you.

  <Target Name="EmbedLocal" BeforeTargets="PrepareForBuild">
    ... 
    <ItemGroup>
      <EmbeddedResource Include="..."/>
    </ItemGroup>
    ...
  </Target>


来源:https://stackoverflow.com/questions/51607558/msbuild-create-embeddedresource-before-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!