Asp.net core msbuild-是否可以在不使用导入的情况下将子项目中定义的项共享给其父项目? 问题:

Asp.net core msbuild-是否可以在不使用导入的情况下将子项目中定义的项共享给其父项目? 问题:,asp.net-core,msbuild,.net-core,csproj,Asp.net Core,Msbuild,.net Core,Csproj,如何在不使用元素的情况下将子项目中定义的项传递给其父项目? 我想避免使用的原因是,它还导入了在父项目中执行的目标,这是不需要的 例子 假设我有以下孩子.csproj: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup>

如何在不使用
元素的情况下将子项目中定义的项传递给其父项目? 我想避免使用
的原因是,它还导入了在父项目中执行的目标,这是不需要的

例子 假设我有以下孩子.csproj:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <MyItem Include="some.file" />
    </ItemGroup>

    <Target Name="MyChildPreBuild" BeforeTargets="Build">
        <Message Text="MyChildPreBuild" Importance="high" />
        <Message Text="%(MyItem.FullPath)" Importance="high" />
    </Target>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <MyItem Include="some.other.file" />
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="..\child\child.csproj" />
    </ItemGroup>

    <Target Name="MyPreBuild" BeforeTargets="Build">
        <Message Text="MyPreBuild" Importance="high" />
        <Message Text="%(MyItem.FullPath)" Importance="high" />
    </Target>
</Project>
构建
parent.csproj
,我得到以下构建输出消息:

1>------ Rebuild All started: Project: child, Configuration: Debug Any CPU ------
1>child -> C:\Users\dan\test\child\bin\Debug\netstandard2.0\child.dll
2>------ Rebuild All started: Project: parent, Configuration: Debug Any CPU ------
2>parent -> C:\Users\dan\test\parent\bin\Debug\netstandard2.0\parent.dll
2>C:\Users\dan\test\parent\some.other.file
========== Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========
可以看到,仅打印
父.csproj
MyItem

添加到
parent.csproj
会打印所有
MyItem
但也会执行子项的目标:

1>------ Rebuild All started: Project: child, Configuration: Debug Any CPU ------
1>child -> C:\Users\dan\test\child\bin\Debug\netstandard2.0\child.dll
1>MyChildPreBuild
1>C:\Users\dan\test\child\some.file
2>------ Rebuild All started: Project: parent, Configuration: Debug Any CPU ------
2>C:\Users\dan\test\child\child.csproj : warning MSB4011: "C:\Program Files\dotnet\sdk\2.1.403\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props" cannot be imported again. It was already imported at "C:\Users\dan\test\parent\parent.csproj". This is most likely a build authoring error. This subsequent import will be ignored.
2>C:\Users\dan\test\parent\parent.csproj : warning MSB4011: "C:\Program Files\dotnet\sdk\2.1.403\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets" cannot be imported again. It was already imported at "C:\Users\dan\test\child\child.csproj". This is most likely a build authoring error. This subsequent import will be ignored.
2>parent -> C:\Users\dan\test\parent\bin\Debug\netstandard2.0\parent.dll
2>MyChildPreBuild
2>C:\Users\dan\test\parent\some.other.file
2>C:\Users\dan\test\parent\some.file
2>MyPreBuild
2>C:\Users\dan\test\parent\some.other.file
2>C:\Users\dan\test\parent\some.file
========== Rebuild All: 2 succeeded, 0 failed, 0 skipped ==========

如果使用
import
方法,则可以向子目标添加一个条件,使其仅在未设置该标志时执行。然后,在父级中,将标志设置为一个值

孩子


netstandard2.0
然后,在父项目中

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <ParentProject>parent.csproj</ParentProject>
    </PropertyGroup>

    <ItemGroup>
        <MyItem Include="some.other.file" />
    </ItemGroup>

    <Import Project="..\child\child.csproj" />

    <Target Name="MyPreBuild" BeforeTargets="Build">
        <Message Text="MyPreBuild" Importance="high" />
        <Message Text="%(MyItem.FullPath)" Importance="high" />
    </Target>
</Project>

netstandard2.0
parent.csproj
这将防止在导入时调用子目标

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <ParentProject>parent.csproj</ParentProject>
    </PropertyGroup>

    <ItemGroup>
        <MyItem Include="some.other.file" />
    </ItemGroup>

    <Import Project="..\child\child.csproj" />

    <Target Name="MyPreBuild" BeforeTargets="Build">
        <Message Text="MyPreBuild" Importance="high" />
        <Message Text="%(MyItem.FullPath)" Importance="high" />
    </Target>
</Project>