Nathan Raley
Nathan Raley

Reputation: 55

Change NuGet Package Referenced Based on Profile

We have a NuGet package we created locally. We have a version that includes non-production environments and one that includes the production environment. Let's use Connection and ConnectionProd for reference.

Is there a way that I can set Debug or my non Prod publish profile that I have set up to use the Connection package, but have the Production profile use ConnectionProd? I know the PackageReference has conditions, but I wasn't sure if there was a way to tie that to the selected publish profile?

For security reasons, most of our developers don't have access to the ConnectionProd NuGet source location and I'd like to not have to create another TFS branch solely to handle the production NuGet reference.

Upvotes: 0

Views: 678

Answers (1)

LazZiya
LazZiya

Reputation: 5729

You specify different package name and version depending on the envirounment:

<ItemGroup Condition="'$(Configuration)'=='Debug'">
    <PackageReference Include="Connection" Version="1.0.0-dev" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='Release'">
    <PackageReference Include="ConnectionProd" Version="1.0.0" />
</ItemGroup>

UPDATE

Theorically '$(ASPNETCORE_ENVIRONMENT)'=='Development' or using any environment variable for the condition should do it, but somehow I couldn't make it work on my test project.

However, I found another way to make it run with any custom variable, just define your variable inside a <PropertyGroup> then use it to define the condition:

<PropertyGroup>
    <MyVariable Condition="'$(MyVariable)'==''">MyValue</MyVariable>
</PropertyGroup>

<ItemGroup Condition="'$(MyVariable)'=='MyValue'">
    ...
</ItemGroup>

Reference: https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-build-the-same-source-files-with-different-options?view=vs-2019#example

Upvotes: 1

Related Questions