Thursday, February 7, 2013

[Github / Maven] Maven repository using GitHub

This simple post is a reminder on how to create a Maven2 repository and use it with Maven in a "pom.xml" file or in Java Play!.

The first step is to create a GitHub Repository (it will be named "maven2").

Then reproduce the folder structure of a Maven Repository, as I did in my "Maven2" repo (https://github.com/enreeco/maven2).

For instance, given this artifact (jar):

<groupId>com.rubenlaguna</groupId>
  <artifactId>evernote-api</artifactId>
  <name>Official Evernote API</name>
  <version>1.22</version>

Create these folders:

./com
     ./com/rubenlaguna
     ./com/rubenlaguna/evernote-api
     ./com/rubenlaguna/evernote-api/1.22
     ./com/rubenlaguna/evernote-api/1.22/evernote-api-1.22.jar
     ./com/rubenlaguna/evernote-api/1.22/evernote-api-1.22.pom

Now you have a pubblicly accessible Maven2 repository at https://github.com/[username]/[repoName]/raw/master/ (in my case https://github.com/enreeco/maven2/raw/master/).

To consume it in your projects, just add this lines in the "pom.xml" file of your project:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.echoservice</groupId>
    <version>1.0-SNAPSHOT</version>
    <artifactId>echoservlet</artifactId>
    <repositories>
    <repository>
      <id>myRepository</id>
      <url>https://github.com/enreeco/maven2/raw/master/</url>
    </repository>
    </repositories>
    <dependencies>
 . . .
 </depencencies>
 . . .
</project>

To integrate the maven repo in a Java Play! project (necessary if you're messing with Heroku), take the "/prject/Build.scala" file and add those lines:

import sbt._
import Keys._
import PlayProject._

object ApplicationBuild extends Build {

    val appName         = "evernote-integration"
    val appVersion      = "0.1"

    val appDependencies = Seq(
      "com.rubenlaguna" % "evernote-api" % "1.22", //Evernote Thrift Library
      "com.rubenlaguna" % "libthrift" % "1.0-SNAPSHOT",//Thrift core library
      "postgresql" % "postgresql" % "9.1-901.jdbc4", //Postgres
      "org.apache.httpcomponents" % "fluent-hc" % "4.2.1" //Apache HTTP client
    )

    val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
     resolvers+= "personal maven repo" at "https://github.com/enreeco/maven2/raw/master/"
    )

}

Maven will search for the selected jars in the default locations, but when it's not finding anything it will search for your personal resolvers.

No comments:

Post a Comment