如何使用较新的框架引用或复制其他C#项目的输出



场景:

Loader.csproj,它是.NET 4.8;并输出一个带有一些.dll依赖项的.exe
LoaderWrapper.csproj,它是.NET 4.6,需要用一些参数调用Loader的.exe。

Main.csproj,它是.NET 4.6,通常引用LoaderWrapper。

我无法从LoaderWrapper.csproj创建对Loader.csproj的普通项目引用,因为Loader.csproj是一个较新的.NET框架。

我无法创建程序集引用,因为它只复制.exe文件,而不复制Loader.csproj的.exe所依赖的任何.dll文件。

我无法更改框架版本,因为我的应用程序已加载到三维应用程序Revit中,即.NET 4.6

我无法对Loader.csproj的输出进行硬编码,因为在我的构建管道中,我有几个不同的输出目录。(例如,用于单元测试(

在Visual Studio中,我可以更改";构建依赖项"->quot;项目依赖项";在LoaderWrapper.csproj上,以确保Loader.csproj得到构建,但这似乎没有复制/引用Loader.csproj的输出。

因此,我正在寻找另一种方法,以确保Loader.csproj的所有输出都包含在LoaderWrapper.csproj中,并通过项目引用最终出现在Main.csproj中。

一段时间前,我解决了类似的任务——从一个项目收集内容文件,并将它们包含到另一个项目的输出中。这与你的任务不完全相同,但你可以尝试采用我的方法。

所以,我有一个名为MainApp和Lib的项目。Lib项目有一个txt文件ContentFile.txt,其中Build Action=Content。我需要将ContentFile.txt包含到MainApp的输出中。

我在Lib文件夹中创建了CustomBuildActions.targets文件,其中包含以下内容

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
Define project to get content files from.
Definition relies on the fact that this target file stored
in the same folder with Lib.csproj
-->
<ItemGroup>
<LibProject Include="$(MSBuildThisFileDirectory)/Lib.csproj"/>
</ItemGroup>
<!--
Run msbuild for Lib to collect the list of Content files and store it to the LibContentFiles list.
Then perform string repace to convert paths to Content files to paths inside app bundle. And store results in the LibContentFileTargetPath.
-->
<Target Name="GetBundleFiles" Outputs="@(LibContentFiles)">
<MSBuild Projects="@(LibProject)" Targets="ContentFilesProjectOutputGroup">
<Output ItemName="LibContentFiles" TaskParameter="TargetOutputs"/>
</MSBuild>
<ItemGroup>
<LibContentFileTargetPath Include="@(LibContentFiles->Replace($(MSBuildThisFileDirectory), $(AppBundleDir)/Contents/Resources/))"/>
</ItemGroup> 
</Target>
<!-- These targets will fire after mmp creates your bundle but before code signing -->
<PropertyGroup>
<CreateAppBundleDependsOn>$(CreateAppBundleDependsOn);GetBundleFiles;CopyOurFiles;</CreateAppBundleDependsOn>
</PropertyGroup>
<!-- Since this has inputs/outputs, it will fire only when the inputs are changed or the output does not exist -->
<Target Name="CopyOurFiles" Inputs="@(LibContentFiles)" Outputs="@(LibContentFileTargetPath)">
<Message Text="This is us copying a file into resources!" />
<!-- This could have easily been done w/ a built in build action, but you can extend it arbitrary. -->
<Copy SourceFiles="@(LibContentFiles)" DestinationFiles="@(LibContentFileTargetPath)" />
</Target>
</Project>

然后在MainApp项目中导入此目标

<Import Project="../Lib/CustomBuildActions.targets" />

这个自定义目标将从Lib项目中收集内容文件,转换路径以在MainApp输出中保留文件夹结构(例如,如果我们在Lib项目内部有类似Lib/ContentFolder/Subfolder/contentFile.txt的内容,那么在捆绑包内,如果将放在Resources/ContentFolder/Subfolder/contentFile.txt中(,然后复制捆绑包内的文件。

所以,你需要采用的是:

  1. 使用Loader项目的输出项,而不是内容文件。因此,您将需要其他目标,而不是ContentFilesProjectOutputGroup。请参阅msbuild文档,找到最适合您的内容
  2. 更改目标依赖项-您的自定义目标应该成为LoaderWrapper项目构建步骤的依赖项(在我的示例中,我使用CreateAppBundleDependsOn依赖项,但这是Xamarin唯一的依赖项,您需要找到添加依赖项的更好步骤(

我的案件的完整描述可以在这里找到https://forums.xamarin.com/discussion/comment/418130

最新更新