将参数/属性从发布配置文件传递到csproj文件

问题描述 投票:0回答:2

我在.csproj文件中构建开发时排除了一些文件夹。

<ItemGroup>
<Content Remove="Production\**" />
<Compile Remove="Production\**" />
</ItemGroup>

我想将这些文件夹包含在“发布配置文件”中,但它不起作用。

<ItemGroup>
<Content Include="Production\**" />
<Compile Include="Production\**" />
</ItemGroup>

那么,我怎样才能将参数从“发布配置文件”传递到构建(.csproj)并防止排除这些文件夹或将它们包含回来。

<ItemGroup Condition="'$(SOMEPARAM)'!='Production'">
<Content Remove="Production\**" />
<Compile Remove="Production\**" />
</ItemGroup>

因此,我需要确定何时在.csproj文件中使用“发布配置文件”运行构建并采取相应的操作。

我知道有可能使用命令行参数,但我想使用Visual Studio,而不是命令行。

dotnet build /p:DeployOnBuild=true /p:PublishProfile=FolderProfile;SOMEPARAM=Production

更新:(解决方案)检查下面的答案,使用CopyToPublishDirectory时它可以正常工作。

更新:(另一种解决方案)或者,当使用相同名称指定目标时,发布文件中的目标将覆盖项目文件中的目标,因此我们可以在项目和发布文件中单独定义要包含/排除的内容。

asp.net-core msbuild
2个回答
1
投票

您可以在发布配置文件中的<PropertyGroup>中定义任何属性,并在csproj的<ItemGroup>s中使用它。

这是因为发布配置文件被导入到项目中,并且msbuild在所有项目组之前评估所有静态属性组,这意味着即使在其末尾导入的文件也会在逻辑上影响其上方的项目组。


0
投票

所以,我确定那些包含需要CopyToPublishDirectory。所以下面的工作也很好。 在.csproj文件中;

<ItemGroup>
<Content Remove="Production\**" />
<Compile Remove="Production\**" />
</ItemGroup>

在发布配置文件;

<ItemGroup>
<Content Include="Production\**" CopyToPublishDirectory="PreserveNewest" />
<Compile Include="Production\**" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
© www.soinside.com 2019 - 2024. All rights reserved.