Search code examples
.net-corenuget

nuget.config package source add <clear /> with command line parameter?


For my nuget.config, I need the <clear /> configuration on my <packageSources> like this

<packageSources>
  <clear />
  <add key="MyNexusNuGit" value="URL_TO_NEXUS_GIT" />
</packageSources>

I am also trying to automate configuration. I have this command line which will add the the source to nuget.config, but I don't see any option or command line parameter to add <clear />

dotnet nuget add source "URL_TO_NEXUS_NUGET" --name "MyNexusNuGet" --username USER --password "PASS"

Does anyone know if there is a command line option or parameter to automate adding <clear /> to the nuget.config file?


Solution

  • First create a new nuget.config file using dotnet new nugetconfig command.

    This generates a new file with the following content that already includes a <clear> tag.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <!--To inherit the global NuGet package sources remove the <clear/> line below -->
        <clear />
        <add key="nuget" value="https://api.nuget.org/v3/index.json" />
      </packageSources>
    </configuration>
    

    Next, add the new package source using below command.

    dotnet nuget add source "http://packageSourceUrl.com" --name "MyNexusNuGet" --username USER --password "PASS" --configfile "C:\Users\ankushzn\nuget.config"
    

    This will add the new package source in the XML.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <!--To inherit the global NuGet package sources remove the <clear/> line below -->
        <clear />
        <add key="nuget" value="https://api.nuget.org/v3/index.json" />
        <add key="MyNexusNuGet" value="http://packageSourceUrl.com" />
      </packageSources>
      <packageSourceCredentials>
        <MyNexusNuGet>
            <add key="Username" value="USER" />
            <add key="Password" value="AQAAANCMnd8BFdERjHoAwE/Cl+sBAAATo.......=" />
          </MyNexusNuGet>
      </packageSourceCredentials>
    </configuration>