获取MSBuild中引用项目的输出路径[重复]

问题描述 投票:0回答:2

如何获取 MSBuild 17 中引用项目的构建输出目录?

<!-- Installer.wixproj -->
<Project Sdk="WixToolset.Sdk/4.0.0">
    <ItemGroup>
        <ProjectReference Include="..\Ref\Ref.csproj" />
    </ItemGroup>

    <Target Name="ShowProject" AfterTargets="Build">
        <Message Text="Output path is $(Ref.OutputDir)" Importance="high" />
    </Target>
</Project>

dotnet build Installer.wixproj
的预期结果:

MSBuild version 17 ...
   Output path is C:\Path\To\Solution\Ref\bin\Release
.net visual-studio msbuild
2个回答
2
投票

不幸的是,使用

$(OutDir)
不适用于独立发布。我最终发布了对我有用的代码。

注意:我还包括了 Wixv4 特定的部分,以防有人使用它。收获目录目标不在 JetBrains Rider 中运行,而是通过 dotnet cli 运行。

<Project Sdk="WixToolset.Sdk/4.0.0" InitialTargets="GenerateHarvestDirectories">
    <PropertyGroup>
        <Platform Condition="'$(RuntimeIdentifier)' == 'win-x64'">x64</Platform>
        <Platform Condition="'$(RuntimeIdentifier)' == 'win-x86'">x86</Platform>
    </PropertyGroup>
    
    <ItemGroup>
        <ProjectReference Include="..\App\App.csproj" />
    </ItemGroup>
    
    <ItemGroup>
        <PackageReference Include="WixToolset.Heat" Version="4.0.1" />
        <PackageReference Include="WixToolset.UI.wixext" Version="4.0.1" />
    </ItemGroup>

    <Target Name="GenerateHarvestDirectories" BeforeTargets="HarvestDirectory">
        <MSBuild Projects="%(ProjectReference.Identity)" Targets="GetTargetPath" Properties="Configuration=$(Configuration);RuntimeIdentifier=$(RuntimeIdentifier);SelfContained=$(SelfContained)">
            <Output TaskParameter="TargetOutputs" ItemName="PrimaryAssembly" />
        </MSBuild>

        <Message Text="Harvesting CMP_%(PrimaryAssembly.Filename): %(PrimaryAssembly.RelativeDir)" Importance="high" />
        <ItemGroup>
            <HarvestDirectory ComponentGroupName="CMP_%(PrimaryAssembly.Filename)" SuppressCom="true" SuppressRegistry="true" SuppressRootDirectory="true" DirectoryRefId="InstallDir" Include="%(PrimaryAssembly.RelativeDir)" />
        </ItemGroup>
    </Target>
</Project>

GenerateHarvestDirectories
目标针对每个引用的项目运行。结果(为简洁起见,删除了路径):

$ dotnet build Installer.wixproj -c Release -r win-x64 --self-contained

MSBuild version 17.5.1+f6fdcf537 for .NET
  ...
  App -> ...\App\bin\Release\net7.0-windows10.0.17763\win-x64\App.dll
  Harvesting CMP_App: ...App\bin\Release\net7.0-windows10.0.17763\win-x64\
  Installer -> ...\Installer\bin\x64\Release\Installer.msi

编辑:根据评论更新。


-1
投票

你是这个意思吗?

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

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


    <Target Name="ShowProject" AfterTargets="Build">
        <Message Text="Output path is $(ProjectDir)\Ref\$(OutDir)" Importance="high" />

    </Target>
</Project>

我可以在主项目构建步骤后获取参考项目输出路径:

顺便说一句,如果您只想转到发布目录而不是输出目录,那么您需要自定义属性:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

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


    <Target Name="ShowProject" AfterTargets="Build">
        <!--<Message Text="Output path is $(ProjectDir)\Ref\$(OutDir)" Importance="high" />-->
        <PropertyGroup>
            <CustomOutputPath>$(ProjectDir)\Ref\bin\Release</CustomOutputPath>
        </PropertyGroup>
        <Message Text="Output path is $(CustomOutputPath)" Importance="high" />


    </Target>
</Project>

© www.soinside.com 2019 - 2024. All rights reserved.