Over a million developers have joined DZone.

Using an Authenticated Maven Repo in Play 2.0

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

We have an internal Maven repo/mirror in my company, we use it to host the releases of our internal apps and libraries, as well as to mirror all the external packages we use (this speeds up build times, new environments set-ups and reduces bandwidth use).

Our internal repository requires authentication to both read/download packages and to publish new releases.

Play 2.0 provides built-in support for Maven repositories, and it’s quite straightforward to add new ones, but how to configure and use an authenticated one wasn’t documented (at least as far as I could see on the official Play website).

Long story short, what you need to do to get this scenario up and running is add the highlighted line to your /project/Build.scala file:

import sbt._
import Keys._
import PlayProject._
 
object ApplicationBuild extends Build {
 
    val appName         = "spei"
    val appVersion      = "1.0-SNAPSHOT"
 
    val appDependencies = Seq()
 
    Credentials.add(Path.userHome / ".mavencredentials", null)
 
    val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
      resolvers += "My Custom Repo" at "http://mymavenhost/my/maven/repo/path/"
    )
}

The path you pass to the Credentials.add() method can be anything, I chose ~/.mavencredentials arbitrarily. These file must contain the following info:

realm=My HTTP authentication realm
host=mymavenhost
user=john
password=trustno1

The realm parameter must match the HTTP auth realm name your web server publishes when requesting authentication.

Note that you can also use a variation of the Credentials.add() method that receives the information directly (ie. without using an external file), but since this promotes hard-coding credentials in your application I really don’t recommend it.

After that just perform an update within your projects play console and it should pull the new dependencies from your configured Maven repo.

 

 

 

 

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Ricardo Zuasti, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}