Tuesday, October 22, 2013

Play2 and Sonatype Nexus

Play2 and Sonatype Nexus


Sonatype Nexus IN & OUT

Binary repositories like Sonatype Nexus can be used to share artifacts within single company. 

It's very common requirement for Play2 application to get shared artifacts from binary repository, but as viable to publish project as shared artifact for later deployment or to be integrated to other applications.

Play shell is based on SBT. SBT defines mechanisms for fetching and publishing artifacts. Resolvers define way of getting content from repositories and publishTo defines how to share project with others thru repository.

If your repository is not open for everyone and for everything, you need to supply credentials. Credentials can be hardcoded to your build file or stored on local user specific configuration. Hardcoding is not preferable.

You can find more info of publishing from here


Defining resolvers

Endpoints for repositories are defined as resolvers. You might have multiple resolvers, and thus you define them as sequence.
      
resolvers ++= Seq (
)
   

Defining publishing

Publishing destination is defined as endpoint with publishTo

publishTo := Some("Local proxy of central repo" at "http://192.168.24.3:8081/nexus/content/groups/development")

If you want to control what is to be published you can enable or disable publishing per artifact.

// disable publishing the main API jar
publishArtifact in (Compile, packageDoc) := false

// disable publishing the main sources jar
publishArtifact in (Compile, packageSrc) := false

How to enable or disable publishing per artifact is documented here


Defining credentials

Hardcoded credentials are defined at build.sbt like

 credentials += Credentials("Sonatype Nexus Repository Manager", "192.168.0.1", "developer", "password")

Dynamic loading of credentials can be done from ivy configuration

credentials += Credentials(Path.userHome / ".ivy2" / ".credentials")

Configuration file must contain realm, host and user with password

realm=Sonatype Nexus Repository Manager
host=192.168.24.3
user=research
password=research

Further steps

Next you might want to see how to connect publishing to CI tool of your choice. 

Whether it is Jenkins, Teamcity, Bamboo or anything else, there might not be direct integration to Play2 in which case you could first see integrations to SBT instead. 

There are multiple strategies for building and packaging your app, of which one of the easiest is to use Play2's shell and "play publish" command to produce zip of whole app.

If you have already heavy loads of Maven based processes you might even want to produce or use plugins that wrap play2 shell commands to maven plugins.

Another considerations for CI process is if you need to provide Play2 app as war to be deployed to application server. Yes, it is possible, but seems really counter intuitive to me. 

No comments: