title: sbt: publish to corporate Nexus repository unauthorized
body: <p><strong>Quick resolution</strong></p>
  
  <p>The required credential expects the exact realm as defined by nexus. See below how to find the one you have defined, but most certainly is "Sonatype Nexus Repository Manager". Add the rest of the details to the credentials as normal.</p>
  
  <p>c:/data/user/.sbt/.credentials</p>
  
  <pre><code>realm=Sonatype Nexus Repository Manager
  host=nexus
  user=repouser
  password=password
  </code></pre>
  
  <p>build.sbt
      credentials += Credentials(Path.userHome / ".sbt" / ".credentials")</p>
  
  <pre><code>publishTo &lt;&lt;= version { v: String =&gt;
    val nexus = "http://nexus/" 
    if (v.trim.endsWith("SNAPSHOT"))
      Some("snapshots" at nexus + "content/repositories/libs-snapshots")
    else
      Some("releases" at nexus + "content/repositories/libs-releases")
    }
  </code></pre>
  
  <p><strong>Problem</strong> </p>
  
  <p>I'm trying to publish a jar into a corporate nexus repo.</p>
  
  <p>I'm able to do this fine from Maven, and I have configured the repositories to be able to use Nexus to provide internal jars. However, publication fails due to authorization.</p>
  
  <pre><code>&gt; publish
  [info] Packaging c:\app\target\scala-2.10\app_2.10-0.1-sources.jar ...
  [info] Wrote D:\app\target\scala-2.10\app_2.10-0.1.pom
  [info] :: delivering :: com.app#app_2.10;0.1 :: 0.1 :: release :: Tue May 07 18:28:44 BST     2013
  [info] Done packaging.
  [info]  delivering ivy file to D:\app\target\scala-2.10\ivy-0.1.xml
  [info] Packaging D:\app\target\scala-2.10\app_2.10-0.1.jar ...
  [info] Done packaging.
  [trace] Stack trace suppressed: run last *:publish for the full output.
  [error] (*:publish) java.io.IOException: Access to URL http://nexus/content/groups/common/com/app/app_2.10/0.1/app_2.10-0.1.pom was refused by the server: Unauthorized
  </code></pre>
  
  <p>c:/data/user/.sbt/.credentials</p>
  
  <pre><code>realm=X 
  host=nexus
  user=repouser
  password=password
  </code></pre>
  
  <p>c:/data/user/.sbt/repositories </p>
  
  <pre><code> [repositories]
    local
    x-repo: http://nexus/content/groups/common
    typesafe-ivy-releases: http://repo.typesafe.com/typesafe/ivy-releases/,     [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext]   
    sbt-plugin-releases: http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases/
    maven-central
  </code></pre>
  
  <p>app/build.sbt</p>
  
  <pre><code> name := "app"
  
   organization := "com.app"
  
   version := "0.1"
  
   scalaVersion := "2.10.1"
  
   libraryDependencies ++= Seq(
  "org.scalatest" % "scalatest_2.10" % "2.0.M5b" % "test"
   )
  
   EclipseKeys.withSource := true
  
   publishMavenStyle := true
  
   credentials += Credentials(Path.userHome / ".sbt" / ".credentials")
  
   publishTo := Some("X Maven Repo" at "http://nexus/content/groups/common")
  </code></pre>
  
  <p>My Maven settings.xml</p>
  
  <pre><code> &lt;mirrors&gt;
   &lt;mirror&gt;
    &lt;id&gt;x-repo&lt;/id&gt;
    &lt;name&gt;X Maven repo&lt;/name&gt;
        &lt;url&gt;http://nexus/content/groups/common&lt;/url&gt;
    &lt;mirrorOf&gt;*&lt;/mirrorOf&gt;
   &lt;/mirror&gt;
   &lt;/mirrors&gt;
  
   &lt;servers&gt;
       &lt;server&gt;
         &lt;id&gt;x-repo&lt;/id&gt;
         &lt;username&gt;repouser&lt;/username&gt;
         &lt;password&gt;password&lt;/password&gt;
       &lt;/server&gt;
     &lt;/servers&gt;
  </code></pre>
  
  <p>I have followed instructions from <a href="http://www.scala-sbt.org/release/docs/Detailed-Topics/Publishing.html" rel="noreferrer">official doc</a> and various other posts, including StackOverflow such this <a href="https://stackoverflow.com/questions/4348805/how-can-i-access-a-secured-nexus-with-sbt">one</a> or mailing list such as <a href="https://groups.google.com/forum/?fromgroups=#!topic/simple-build-tool/n0v0jd4UWOQ" rel="noreferrer">this</a>. None worked. I have tried enabling extra logging, but no further details are given.</p>
  
  <p>I can manually deploy to maven using this command:</p>
  
  <pre><code> mvn deploy:deploy-file -Durl=http://nexus/content/repositories/libs-snapshots -DrepositoryId=x-repo -DgroupId=com.app -DartifactId=app -Dpackaging=jar -Dversion=0.1-SNAPSHOT -Dfile=D:\app\target\scala-2.10\app_2.10-0.1.jar
  </code></pre>
  
  <p>Tried using the following publishTo, also without luck</p>
  
  <pre><code>publishTo &lt;&lt;= version { v: String =&gt;
    val nexus = "http://nexus/" 
    if (v.trim.endsWith("SNAPSHOT"))
      Some("snapshots" at nexus + "content/repositories/libs-snapshots")
    else
      Some("releases" at nexus + "content/repositories/libs-releases")
    }
  </code></pre>
  
  <p>The commands run OK until they need to be authorized, at which point they fail.</p>
  
  <p>The Realm in the credentials, does it correspond to the Server Repository ID in maven or the Name? Either or it doesn't work.</p>
  
  <p>I've been trying to enable more logging for Ivy, but couldn't get more details.</p>
  
  <p>set ivyLoggingLevel := UpdateLogging.Full</p>
  
  <p>According to this, there should be further logging: </p>
  
  <p><a href="https://svn.apache.org/repos/asf/ant/ivy/core/tags/2.2.0/src/java/org/apache/ivy/util/url/IvyAuthenticator.java" rel="noreferrer">https://svn.apache.org/repos/asf/ant/ivy/core/tags/2.2.0/src/java/org/apache/ivy/util/url/IvyAuthenticator.java</a></p>
  
  <p>I'm behind a internal proxy, so i need to set both HTTP user and HTTPS user and password. Perhaps it's here that it's getting blocked?</p>
  
  <p>any suggestions how to increase the level of ivy logging?</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>I've got something to work, by using <a href="https://github.com/arktekk/sbt-aether-deploy" rel="noreferrer">sbt-aether-deploy</a> plugin, which uses Maven infrastructure (wagon) to deploy.</p>
  
  <p>Credentials are exactly the same. In fact, the realm didn't seem to matter.</p>
  
  <p>following are the lines used:</p>
  
  <pre><code>    credentials += Credentials(Path.userHome / ".sbt" / ".credentials")
  
  publishTo &lt;&lt;= version { v: String =&gt;
    val nexus = "http://nexus/"
      if (v.trim.endsWith("SNAPSHOT"))
      Some("snapshots" at nexus + "content/repositories/libs-snapshots")
    else 
      Some("releases" at nexus + "content/repositories/libs-releases")
  }
  
  seq(aetherSettings: _*)
  
  seq(aetherPublishSettings: _*)
  </code></pre>
  
  <p>Something is not right between the proxy, ivy and nexus. </p>
  
  <p>I would still be interested in suggestions to use ivy.</p>
  
  <hr>
  
  <p><strong>Further update:</strong></p>
  
  <p>Using </p>
  
  <pre><code>curl -X POST http://nexusUser:nexusPassword@nexus/content/repositories/libs-snapshots -v
  </code></pre>
  
  <p>I was able to reach the server. </p>
  
  <p>Same result specifying the proxy to use (it's configured to bypass for local networks, but some java processes like SBT seem to require the headers)</p>
  
  <p>When nexusUser:nexusPassword were not specificied, i was getting the following header:</p>
  
  <p>WWW-Authenticate: BASIC realm="Sonatype Nexus Repository Manager"</p>
  
  <p>effectively that was the issue, the credentials required the name of the Realm to be that exact header, as opposed to other custom repository name like maven defines.</p>
  
  <p>Many thanks!</p>