如何保证ProjectReferece在引用的项目中会尊重导入的目标/道具

时间:2019-04-17 07:03:37

标签: msbuild

我有两个项目,A.csproj和B.csproj,其中B由ProjectReference引用。然后B导入一个通用的目标/道具文件C。整个参考链就像

A-( ProjectReference )-> B-(导入)-> C

问题在于:

A中定义的目标看不到C中定义的项目/属性。

但是,如果我将链更改为A-( Import )-> B-(Import)-> C,则这些项/属性对A中定义的目标可见。

请注意,这是一种很常见的情况,其中B安装了一个包含其他属性/项目的基本nuget包,稍后A依赖于B来实现高级功能,而无需再次安装nuget包。

EDIT1:添加项目A,B和程序包C的示例 文件夹结构如下:

<ROOT>--A--A.csproj
--------B--B.csproj
--------packages--C--build--C.props
----------------------------C.targets
---------------------lib--a.ini
--------------------------b.ini

A.csproj

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!--
    <Import Project="..\B\B.csproj" />
    -->
    <ItemGroup>
    <ProjectReference Include="..\B\B.csproj">
    <Project>{20129E3E-FE37-4BEF-81A9-DA8F3AA7764B}</Project>
    <Name>B</Name>
    </ProjectReference>
    </ItemGroup>
    <Target Name="Build">
    <Message Text="in A, This is my list of MyPackageSourceFilesProperty: $(MyPackageSourceFilesProperty)"/>
    <Message Text="------" />
    <Message Text="in A, This is my list of BingoInB: %(BingoInB.FileName)"/>
    <Message Text="------" />
    <Message Text="in A, This is my list of MyPackageSourceFiles: %(MyPackageSourceFiles.FileName)"/>
    <Message Text="------" />
    <Message Text="in A, This is my list of ini files: %(Content.FileName)"/>
    </Target>
    </Project>

B.csproj

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="..\packages\C\build\C.props" Condition="Exists('..\packages\C\build\C.props')" />
    <Target Name="Build">
    <Message Text="in B, This is my list of MyPackageSourceFilesProperty: $(MyPackageSourceFilesProperty)"/>
    <Message Text="------" />
    <Message Text="in B, This is my list of MyPackageSourceFiles: %(MyPackageSourceFiles.FileName)"/>
    <Message Text="------" />
    <Message Text="in B, This is my list of ini files: %(Content.FileName)"/>
    </Target>
    <Import Project="..\packages\C\build\C.targets" Condition="Exists('..\packages\C\build\C.targets')" />
    </Project>

C.targets

    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
    <MyPackageSourceFiles Include="$(MSBuildThisFileDirectory)\..\lib\net45\*.*" />
    <Content Include="@(MyPackageSourceFiles)">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    </ItemGroup>
    </Project>

C.props

    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
    <MyPackageSourceFilesProperty>$(MSBuildThisFileDirectory)\..\lib\net45\*.*</MyPackageSourceFilesProperty>
    </PropertyGroup>
    </Project>

0 个答案:

没有答案