首页 > 解决方案 > 如何导入由 Target 生成的 .props 文件?

问题描述

背景

我正在尝试构建一个 C++ Visual Studio 项目。我的项目正在导入指定单个目标“SetupConanDependencies”的 MSBuild .targets 文件。此目标使用该conan工具根据位于项目根目录中的“conanfile.txt”文件为项目安装依赖项。

此目标的输出由多个文件组成,包括“conanbuildinfo.props”属性文件。我可以在我的项目中导入生成的 .props 文件,以告诉它如何解决通过 .props 安装的依赖项conan

这是我的目标定义的精简版本(我已经测试并验证它确实会生成一个“conanbuildinfo.props”文件):

<Target Name="SetupConanDependencies"
        Inputs="$(SolutionDir)conanfile.txt"
        Outputs="$(OutDir).conan/conanbuildinfo.props"
        BeforeTargets="ClCompile">
    <Message Importance="High" Text="Installing project dependencies with conan..." />
    <Exec Command="conan install $(ProjectDir)conanfile.txt --install-folder=$(OutDir).conan"/>
</Target>

对于我的问题,唯一相关的信息是我有一个正在生成 .props 属性文件的目标。我想在构建之前在我的项目中导入生成的属性文件。但是,我还希望 Target 作为构建过程的一部分自动运行。

问题

我不能简单地将生成的 .props 文件导入到 .targets 文件中,因为它还没有由 Target 构建。

<Import Project="$(OutDir).conan/conanbuildinfo.props"/>
<!-- Adding this line after my Target definition results in the following error, because
     the file doesn't exist on disk at the time the .targets file is imported. -->

<!-- Error MSB4019 The imported project "D:\CustomBuildToolExample\Debug\.conan\conanbuildinfo.props" was not
     found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. -->

此外,根据我目前对 MSBuild 的理解,.props 文件应该在 .targets 文件之前导入。这似乎意味着我根本无法导入由我的 Target 创建的 .props 文件,因为在 Target 生成 .props 文件时,项目已经导入了它将作为构建的一部分使用的所有属性过程。

有没有办法包含由 Target 生成的 .props 文件,以便我的项目将使用该 .props 文件中定义的属性作为其构建过程的一部分?

标签: visual-c++msbuildconan

解决方案


推荐阅读