获取MSBuild中引用项目的输出路径。

huangapple go评论60阅读模式
英文:

Get Output path of referenced project in MSBuild

问题

在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版本 17 ...
   输出路径为C:\Path\To\Solution\Ref\bin\Release
英文:

How can I get the build output directory of a referenced project in MSBuild 17?

&lt;!-- Installer.wixproj --&gt;
&lt;Project Sdk=&quot;WixToolset.Sdk/4.0.0&quot;&gt;
    &lt;ItemGroup&gt;
        &lt;ProjectReference Include=&quot;..\Ref\Ref.csproj&quot; /&gt;
    &lt;/ItemGroup&gt;

    &lt;Target Name=&quot;ShowProject&quot; AfterTargets=&quot;Build&quot;&gt;
        &lt;Message Text=&quot;Output path is $(Ref.OutputDir)&quot; Importance=&quot;high&quot; /&gt;
    &lt;/Target&gt;
&lt;/Project&gt;

Expected result from dotnet build Installer.wixproj:

MSBuild version 17 ...
   Output path is C:\Path\To\Solution\Ref\bin\Release

答案1

得分: 1

抱歉,以下是已翻译的部分:

不幸的是,上面的方法对桌面应用程序,即自包含应用程序等,不起作用。我最终发布了对我有效的代码。

注意:我还包括了与 Wixv4 特定部分,以防有人在其中使用它。在 JetBrains Rider 中,收集目录目标不会运行,但通过 dotnet 命令行可以运行。

<Project Sdk="WixToolset.Sdk/4.0.0">
    <PropertyGroup>
        <Platform Condition="'$(RuntimeIdentifier)' == 'win-x64'">x64</Platform>
        <Platform Condition="'$(RuntimeIdentifier)' == 'win-x86'">x86</Platform>
    </PropertyGroup>
    <ItemGroup>
        <!-- This is required to make the HarvestDirectories target run -->
        <HarvestDirectory Include="obj/" Visible="false" ComponentGroupName="CMP_None" SupressCom="true" SuppressRegistry="true" />
    </ItemGroup>
    
    <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 版本 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


<details>
<summary>英文:</summary>

Unfortunately, the approach above did not work for desktop applications i.e. self contained etc. I am posting the code that worked for me in the end. 

Note: I am also including the Wixv4-specific part, in case anyone uses it for that. The harvest directories target does not run in JetBrains Rider, but does via the dotnet cli.

<Project Sdk="WixToolset.Sdk/4.0.0">
<PropertyGroup>
<Platform Condition="'$(RuntimeIdentifier)' == 'win-x64'">x64</Platform>
<Platform Condition="'$(RuntimeIdentifier)' == 'win-x86'">x86</Platform>
</PropertyGroup>
<ItemGroup>
<!-- This is required to make the HarvestDirectories target run -->
<HarvestDirectory Include="obj/" Visible="false" ComponentGroupName="CMP_None" SupressCom="true" SuppressRegistry="true" />
</ItemGroup>

&lt;ItemGroup&gt;
    &lt;ProjectReference Include=&quot;..\App\App.csproj&quot; /&gt;
&lt;/ItemGroup&gt;

&lt;ItemGroup&gt;
    &lt;PackageReference Include=&quot;WixToolset.Heat&quot; Version=&quot;4.0.1&quot; /&gt;
    &lt;PackageReference Include=&quot;WixToolset.UI.wixext&quot; Version=&quot;4.0.1&quot; /&gt;
&lt;/ItemGroup&gt;

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

    &lt;Message Text=&quot;Harvesting CMP_%(PrimaryAssembly.Filename): %(PrimaryAssembly.RelativeDir)&quot; Importance=&quot;high&quot; /&gt;
    &lt;ItemGroup&gt;
        &lt;HarvestDirectory ComponentGroupName=&quot;CMP_%(PrimaryAssembly.Filename)&quot; SuppressCom=&quot;true&quot; SuppressRegistry=&quot;true&quot; SuppressRootDirectory=&quot;true&quot; DirectoryRefId=&quot;InstallDir&quot; Include=&quot;%(PrimaryAssembly.RelativeDir)&quot; /&gt;
    &lt;/ItemGroup&gt;
&lt;/Target&gt;

</Project>


The `GenerateHarvestDirectories` target runs for each referenced project. Result (paths removed for brevity):

$ 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


</details>



# 答案2
**得分**: -1

我可以在主项目构建步骤后获取引用项目的输出路径:

另外,如果你只想进入发布目录而不是输出目录,那么你需要自定义属性:

CustomOutputPath 是自定义的输出路径属性。

<details>
<summary>英文:</summary>

Do you mean this?

    &lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
    
      &lt;PropertyGroup&gt;
        &lt;OutputType&gt;Exe&lt;/OutputType&gt;
        &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;
        &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
        &lt;Nullable&gt;enable&lt;/Nullable&gt;
      &lt;/PropertyGroup&gt;
    
      &lt;ItemGroup&gt;
        &lt;ProjectReference Include=&quot;..\Ref\Ref.csproj&quot; /&gt;
      &lt;/ItemGroup&gt;
    
    
        &lt;Target Name=&quot;ShowProject&quot; AfterTargets=&quot;Build&quot;&gt;
            &lt;Message Text=&quot;Output path is $(ProjectDir)\Ref$(OutDir)&quot; Importance=&quot;high&quot; /&gt;
    
        &lt;/Target&gt;
    &lt;/Project&gt;

I can get the reference project output path after main project build step:

[![enter image description here][1]][1]

**By the way, if you only want to go to release directory instead of output directory, then you need custom property:**

    &lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
    
      &lt;PropertyGroup&gt;
        &lt;OutputType&gt;Exe&lt;/OutputType&gt;
        &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;
        &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
        &lt;Nullable&gt;enable&lt;/Nullable&gt;
      &lt;/PropertyGroup&gt;
    
      &lt;ItemGroup&gt;
        &lt;ProjectReference Include=&quot;..\Ref\Ref.csproj&quot; /&gt;
      &lt;/ItemGroup&gt;
    
    
        &lt;Target Name=&quot;ShowProject&quot; AfterTargets=&quot;Build&quot;&gt;
            &lt;!--&lt;Message Text=&quot;Output path is $(ProjectDir)\Ref$(OutDir)&quot; Importance=&quot;high&quot; /&gt;--&gt;
            &lt;PropertyGroup&gt;
                &lt;CustomOutputPath&gt;$(ProjectDir)\Ref\bin\Release&lt;/CustomOutputPath&gt;
            &lt;/PropertyGroup&gt;
            &lt;Message Text=&quot;Output path is $(CustomOutputPath)&quot; Importance=&quot;high&quot; /&gt;
    
    
        &lt;/Target&gt;
    &lt;/Project&gt;

[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/2rMIK.png
  [2]: https://i.stack.imgur.com/eE6yY.png

</details>



huangapple
  • 本文由 发表于 2023年6月26日 16:54:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定