Hosting a Maven repository on github

I have a fork of a small open sourced library that I’m working on on github. I’d like to make it available to other developers via maven, but I don’t want to run my own Nexus server, and because it’s a fork I can’t easily deploy it to oss.sonatype.org.

What I’d like to do is to deploy it to github so that others can access it using maven. What’s the best way to do this?

  • How do I push changes from one branch to another branch after a successful Jenkins build?
  • Error push/pull on github: Unable to resolve github.com
  • If HTML doesn't work well in github, how can some people make it work
  • Is there a way to integrate git with Jupyter and have a version control over the notebooks created?
  • Jekyll on github pages showing error: 404 Index.html not found (window)?
  • reorganize Git after massive manual changes
  • how to add existing project in git using bluemix
  • How to ignore files and folders with pull requests to github in distinct git clones installations?
  • GIT HOOK to check keyword “PROMOTE” in message when GIT MERGE is executed
  • Revert git push to wrong branch
  • how to create a file with the cmd?
  • How to list all releases of public repository with GitHub API V3
  • 5 Solutions collect form web for “Hosting a Maven repository on github”

    The best solution I’ve been able to find consists of these steps:

    1. Create a branch called mvn-repo to host your maven artifacts.
    2. Use the github site-maven-plugin to push your artifacts to github.
    3. Configure maven to use your remote mvn-repo as a maven repository.

    There are several benefits to using this approach:

    • Maven artifacts are kept separate from your source in a separate branch called mvn-repo, much like github pages are kept in a separate branch called gh-pages (if you use github pages)
    • Unlike some other proposed solutions, it doesn’t conflict with your gh-pages if you’re using them.
    • Ties in naturally with the deploy target so there are no new maven commands to learn. Just use mvn deploy as you normally would

    The typical way you deploy artifacts to a remote maven repo is to use mvn deploy, so let’s patch into that mechanism for this solution.

    First, tell maven to deploy artifacts to a temporary staging location inside your target directory. Add this to your pom.xml:

    <distributionManagement>
        <repository>
            <id>internal.repo</id>
            <name>Temporary Staging Repository</name>
            <url>file://${project.build.directory}/mvn-repo</url>
        </repository>
    </distributionManagement>
    
    <plugins>
        <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.1</version>
            <configuration>
                <altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
            </configuration>
        </plugin>
    </plugins>
    

    Now try running mvn clean deploy. You’ll see that it deployed your maven repository to target/mvn-repo. The next step is to get it to upload that directory to GitHub.

    Add your authentication information to ~/.m2/settings.xml so that the github site-maven-plugin can push to GitHub:

    <!-- NOTE: MAKE SURE THAT settings.xml IS NOT WORLD READABLE! -->
    <settings>
      <servers>
        <server>
          <id>github</id>
          <username>YOUR-USERNAME</username>
          <password>YOUR-PASSWORD</password>
        </server>
      </servers>
    </settings>
    

    (As noted, please make sure to chmod 700 settings.xml to ensure no one can read your password in the file. If someone knows how to make site-maven-plugin prompt for a password instead of requiring it in a config file, let me know.)

    Then tell the GitHub site-maven-plugin about the new server you just configured by adding the following to your pom:

    <properties>
        <!-- github server corresponds to entry in ~/.m2/settings.xml -->
        <github.global.server>github</github.global.server>
    </properties>
    

    Finally, configure the site-maven-plugin to upload from your temporary staging repo to your mvn-repo branch on Github:

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.github</groupId>
                <artifactId>site-maven-plugin</artifactId>
                <version>0.11</version>
                <configuration>
                    <message>Maven artifacts for ${project.version}</message>  <!-- git commit message -->
                    <noJekyll>true</noJekyll>                                  <!-- disable webpage processing -->
                    <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory> <!-- matches distribution management repository url above -->
                    <branch>refs/heads/mvn-repo</branch>                       <!-- remote branch name -->
                    <includes><include>**/*</include></includes>
                    <repositoryName>YOUR-REPOSITORY-NAME</repositoryName>      <!-- github repo name -->
                    <repositoryOwner>YOUR-GITHUB-USERNAME</repositoryOwner>    <!-- github username  -->
                </configuration>
                <executions>
                  <!-- run site-maven-plugin's 'site' target as part of the build's normal 'deploy' phase -->
                  <execution>
                    <goals>
                      <goal>site</goal>
                    </goals>
                    <phase>deploy</phase>
                  </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    The mvn-repo branch does not need to exist, it will be created for you.

    Now run mvn clean deploy again. You should see maven-deploy-plugin “upload” the files to your local staging repository in the target directory, then site-maven-plugin committing those files and pushing them to the server.

    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building DaoCore 1.3-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    ...
    [INFO] --- maven-deploy-plugin:2.5:deploy (default-deploy) @ greendao ---
    Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/1.3-SNAPSHOT/greendao-1.3-20121223.182256-3.jar (77 KB at 2936.9 KB/sec)
    Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/1.3-SNAPSHOT/greendao-1.3-20121223.182256-3.pom (3 KB at 1402.3 KB/sec)
    Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/1.3-SNAPSHOT/maven-metadata.xml (768 B at 150.0 KB/sec)
    Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/maven-metadata.xml (282 B at 91.8 KB/sec)
    [INFO] 
    [INFO] --- site-maven-plugin:0.7:site (default) @ greendao ---
    [INFO] Creating 24 blobs
    [INFO] Creating tree with 25 blob entries
    [INFO] Creating commit with SHA-1: 0b8444e487a8acf9caabe7ec18a4e9cff4964809
    [INFO] Updating reference refs/heads/mvn-repo from ab7afb9a228bf33d9e04db39d178f96a7a225593 to 0b8444e487a8acf9caabe7ec18a4e9cff4964809
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 8.595s
    [INFO] Finished at: Sun Dec 23 11:23:03 MST 2012
    [INFO] Final Memory: 9M/81M
    [INFO] ------------------------------------------------------------------------
    

    Visit github.com in your browser, select the mvn-repo branch, and verify that all your binaries are now there.

    enter image description here

    Congratulations!

    You can now deploy your maven artifacts to a poor man’s public repo simply by running mvn clean deploy.

    There’s one more step you’ll want to take, which is to configure any poms that depend on your pom to know where your repository is. Add the following snippet to any project’s pom that depends on your project:

    <repositories>
        <repository>
            <id>YOUR-PROJECT-NAME-mvn-repo</id>
            <url>https://raw.github.com/YOUR-USERNAME/YOUR-PROJECT-NAME/mvn-repo/</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    

    Now any project that requires your jar files will automatically download them from your github maven repository.

    Edit: to avoid the problem mentioned in the comments (‘Error creating commit: Invalid request. For ‘properties/name’, nil is not a string.’), make sure you state a name in your profile on github.

    Don’t use GitHub as a Maven Repository.

    Edit: This option gets a lot of down votes, but no comments as to why. This is the correct option regardless of the technical capabilities to actually host on GitHub. Hosting on GitHub is wrong for all the reasons outlined below and without comments I can’t improve the answer to clarify your issues.

    The best option is to convince the original project to include your changes and stick with the original.

    But otherwise:

    You’ve forked an open source library and your fork is also open source.
    So just give it a new groupId and maybe a new artifactId and then upload it to Central http://maven.apache.org/guides/mini/guide-central-repository-upload.html.
    You can then manage this fork until the changes are incorporated into the parent project and then abandon this one.

    Why?

    1) Don’t bloat your repository with jars, this is a bad thing. It will increase the size and download time of your repository for no benefit. Also, some people point out that this may break Github’s terms and conditions. Non-versioned stuff should be on http://pages.github.com/ – but still don’t build your repository there. Use Maven Central.

    2) Stephen Connolly says:

    If anyone adds your repo they impact their build performance
    as they now have another repo to check artifacts against… It’s not a big
    problem if you only have to add one repo… But the problem grows and the next
    thing you know your maven build is checking 50 repos for every artifact and
    build time is a dog.

    You can read some more about repositories at https://maven.apache.org/guides/introduction/introduction-to-repositories.html

    You can use JitPack to expose your GitHub repository as a Maven artifact. Its very easy. Your users would need to add this to their pom.xml:

    1. Add repository:
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
    
    1. Add dependency:
    <dependency>
        <groupId>com.github.User</groupId>
        <artifactId>Repo name</artifactId>
        <version>Release tag</version>
    </dependency>
    

    As answered elsewhere the idea is that JitPack will build your GitHub repo and will serve the jars. The requirement is that you have a build file and a GitHub release.

    The nice thing is that you don’t have to handle deployment and uploads. Since you didn’t want to maintain your own artifact repository its a good match for your needs.

    Another alternative is to use any web hosting with webdav support. You will need some space for this somewhere of course but it is straightforward to set up and a good alternative to running a full blown nexus server.

    add this to your build section

         <extensions>
            <extension>
            <artifactId>wagon-webdav-jackrabbit</artifactId>
            <groupId>org.apache.maven.wagon</groupId>
            <version>2.2</version>
            </extension>
        </extensions>
    

    Add something like this to your distributionManagement section

    <repository>
        <id>release.repo</id>
        <url>dav:http://repo.jillesvangurp.com/releases/</url>
    </repository>
    

    Finally make sure to setup the repository access in your settings.xml

    add this to your servers section

        <server>
            <id>release.repo</id>
            <username>xxxx</username>
            <password>xxxx</password>
        </server>
    

    and a definition to your repositories section

                <repository>
                    <id>release.repo</id>
                    <url>http://repo.jillesvangurp.com/releases</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
    

    Finally, if you have any standard php hosting, you can use something like sabredav to add webdav capabilities.

    Advantages: you have your own maven repository
    Downsides: you don’t have any of the management capabilities in nexus; you need some webdav setup somewhere

    As an alternative, Bintray provides free hosting of maven repositories. That’s probably a good alternative to Sonatype OSS and Maven Central if you absolutely don’t want to rename the groupId. But please, at least make an effort to get your changes integrated upstream or rename and publish to Central. It makes it much easier for others to use your fork.

    Git Baby is a git and github fan, let's start git clone.