【问题标题】:How to add child project additional output to publish items with MSBuild?如何添加子项目附加输出以使用 MSBuild 发布项目?
【发布时间】:2019-08-07 15:47:21
【问题描述】:

我想创建一个附加文件,并将其包含在项目引用端的发布项中。

解决方案结构

Solution
├─ AdditionalItem.targets
├─ Main
│  ├─ Main.csproj
│  └─ bin
│      └─ Release
│          └─ netcoreapp2.2
│              ├─ Main.dll
│              ├─ Sub.dll
│              ├─ Sub.sha256     (OK: Copied in build by AdditionalItem.targets)
│              └─ publish
│                  ├─ Main.dll
│                  ├─ Sub.dll
│                  └─ Sub.sha256 (NG: I want to output this in publish)
└─ Sub
    ├─ Sub.csproj
    └─ bin
        └─ Release
            └─ netstandard2.0
                ├─ Main.dll
                ├─ Sub.dll
                └─ Sub.sha256     (OK: Created by AdditionalItem.targets)

文件详情

Main.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Sub\Sub.csproj" />
  </ItemGroup>

</Project>

Sub.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <Import Project="..\AdditionalItem.targets" />

</Project>

AdditionalItem.targets

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

  <PropertyGroup>
    <GenerateFileName>$(AssemblyName).sha256</GenerateFileName>
  </PropertyGroup>

  <!-- Target for copy sub project additional output to main project output -->
  <Target Name="GenerateGetCopyToOutputDirectoryItems" BeforeTargets="GetCopyToOutputDirectoryItems">
    <Message Text="**** GenerateGetCopyToOutputDirectoryItems ****"/>

    <PropertyGroup>
      <GenerateFullPath>$([System.IO.Path]::GetFullPath('$(OutputPath)$(GenerateFileName)'))</GenerateFullPath>
    </PropertyGroup>

    <ItemGroup>
      <FileWrites Include="$(OutputPath)$(GenerateFileName)"/>

      <AllItemsFullPathWithTargetPath Include="$(GenerateFullPath)">
        <TargetPath>$(GenerateFileName)</TargetPath>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </AllItemsFullPathWithTargetPath>
    </ItemGroup>
  </Target>

  <!-- Target for create additional output -->
  <Target Name="GenerateAdditionalItems" AfterTargets="Build" Inputs="$(OutputPath)$(TargetFileName)" Outputs="$(OutputPath)$(GenerateFileName)">
    <Message Text="**** GenerateAdditionalItems ****"/>

    <GetFileHash Files="$(OutputPath)$(TargetFileName)">
      <Output TaskParameter="Items" ItemName="Hash" />
    </GetFileHash>

    <WriteLinesToFile File="$(OutputPath)$(GenerateFileName)" Lines="@(Hash->'%(FileHash)')" Overwrite="true"/>
  </Target>

</Project>

我尝试过的

我能够在构建中将子项目的附加输出复制到主项目输出。

但我无法将文件复制到主项目发布中的发布目录。

在 Main\binRelease\netcoreapp2.2 中构建输出

Main.dll
Main.pdb
Sub.dll
Sub.pdb
Sub.sha256
...

在 Main\binRelease\netcoreapp2.2\publish 中发布输出

Main.dll
Main.pdb
Sub.dll
Sub.pdb
...

我想在发布时包含Sub.sha256

我尝试过的其他事情

目标GetCopyToOutputDirectoryItems也在发布中执行,我添加了以下内容。 但结果并没有改变。

<AllPublishItemsFullPathWithTargetPath Include="$(GenerateFullPath)">
  <TargetPath>$(GenerateFileName)</TargetPath>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AllPublishItemsFullPathWithTargetPath>

问题

  • 我想将子项目输出添加到父项目的发布中。
  • 我不想在父项目 csproj 中写入,但我想在子项目导入的目标文件中写入。
  • 除了Project.dll 之外,我还想做与 Razor 类库项目输出 Project.Views.dll 相同的事情。

【问题讨论】:

  • 不能修改main.csproj的特殊原因是什么?如果由于某种原因无法修改 main.csproj,那么使用 directory.build.props 呢?
  • 原因是创建一个包含 AdditionalItem.targets 的 NuGet 包,并在其他项目中重复使用。

标签: visual-studio msbuild


【解决方案1】:

我能够通过以下方式解决自己。

<!-- Target for copy sub project additional output to main publish output -->
<Target Name="GenerateGetCopyToPublishDirectoryItems"
        BeforeTargets="GetCopyToPublishDirectoryItems"
        Returns="@(AllPublishItemsFullPathWithTargetPath)">
  <Message Text="**** GenerateGetCopyToPublishDirectoryItems ****"/>

  <PropertyGroup>
    <GenerateFullPath>$([System.IO.Path]::GetFullPath('$(OutputPath)$(GenerateFileName)'))</GenerateFullPath>
  </PropertyGroup>

  <Message Text="$(GenerateFullPath)"/>

  <ItemGroup>
    <AllPublishItemsFullPathWithTargetPath Include="$(GenerateFullPath)">
      <TargetPath>$(GenerateFileName)</TargetPath>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </AllPublishItemsFullPathWithTargetPath>
  </ItemGroup>
</Target>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-23
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多