英文:
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?
<!-- 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>
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>
<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>
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?
<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>
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:**
<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>
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/2rMIK.png
[2]: https://i.stack.imgur.com/eE6yY.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论