Skip to content

Instantly share code, notes, and snippets.

@fernandezpablo85
Last active September 22, 2024 11:36
Show Gist options
  • Save fernandezpablo85/03cf8b0cd2e7d8527063 to your computer and use it in GitHub Desktop.
Save fernandezpablo85/03cf8b0cd2e7d8527063 to your computer and use it in GitHub Desktop.
how to create your own maven repository on github

How to create a maven repository for your github project step by step

Clone your project in a separate folder

(note: replace ORGANIZATION and PROJECT)

git clone git clone [email protected]:ORGANIZATION/PROJECT.git my-repository

Cd into it

cd my-repository

Create a new branch (here named repository)

git branch repository

Switch to that branch

git checkout repository

Remove all files

rm -rf file1 file2 file3 .. etc

Install your jar in that directory

(note: replace YOUR_GROUP, YOUR_ARTIFACT, YOUR_VERSION and YOUR_JAR_FILE)

mvn install:install-file -DgroupId=YOUR_GROUP -DartifactId=YOUR_ARTIFACT -Dversion=YOUR_VERSION -Dfile=YOUR_JAR_FILE -Dpackaging=jar -DgeneratePom=true -DlocalRepositoryPath=.  -DcreateChecksum=true

YOUR_JAR_FILE should point to an existent jar file, this is why it's best to create your repository branch in a different folder, so you can reference the existing jar in /your/project/path/target/artifact-x.y.z.jar

Add all generated files, commit and push

git add -A . && git commit -m "released version X.Y.Z"

git push origin repository

Reference your jar from a different project

The repository url you just created is https://raw.github.com/YOUR_ORGANIZATION/YOUR_ARTIFACT/repository/

@EsikAntony
Copy link

Thank you for your instruction.
But in present time at last step you should use URL: https://github.com/YOUR_ORGANIZATION/YOUR_PROJECT/raw/repository.
And it's more correct to use existing POM (if you have it) instead of generated, just set -DpomFile=YOUR_POM_FILE_FULL_NAME in install:install-file params instead of -DgeneratePom=true.

@lubosek
Copy link

lubosek commented Nov 8, 2017

This is great! Thanks for the howto.

@jjpe
Copy link

jjpe commented Nov 19, 2017

This explanation is short, to the point, and most of all, it works. So thanks for this 👍 👍

@kuzeko
Copy link

kuzeko commented Nov 28, 2017

Thanks a lot! Great tutorial!

@EsikAntony where can I read more about why use ' -DpomFile=YOUR_POM_FILE_FULL_NAME' and what exactly is POM_FILE_FULL_NAME'?

Also, how do I update the pom of another project in order to use this repository?

@hervinhio
Copy link

Thank you very much for this useful post.
However, I got one question, how do I define dependencies on other artifacts ?

@cap5lut
Copy link

cap5lut commented Dec 28, 2017

Thanks for the small, but on point, tutorial!

@kuzeko

And it's more correct to use existing POM (if you have it) instead of generated

Basically your artifact will have it's own pom.xml, there you set up the specific details for it. With the -DgeneratePom=true you would let maven generate one for you, which can't totally configure your artifact by itself.

@hervinhio
Dependencies for other artifacts are defined in the pom.xml of your artifact. For further information, read the maven manual.

@Tianhao25
Copy link

Tianhao25 commented Jan 18, 2018

Thanks!
This is a perfect howto I am always expecting.

It's clear and it works!

@n0mer
Copy link

n0mer commented Feb 20, 2018

@crearo
Copy link

crearo commented Jul 11, 2018

How do I create the pom.xml? The one generated after ./gradlew assembleRelease isn't being accepted by mvn.

@bvandenbon
Copy link

worked the first time, but there was a mistake in my release, and I re-released the same version. Now the pom fill that uses/downloads this repository no longer finds it. (although it worked fine the first time).

@Pythonic-Rainbow
Copy link

Pythonic-Rainbow commented Aug 3, 2018

@bvandenbon how do u make it work

EDIT: I figured it out
Maven URL should be http://raw.github.com/ORGANIZATION/REPO/BRANCH

@sreeni-b
Copy link

sreeni-b commented Jun 7, 2019

For accessing the private repository during maven build, add the following in the maven settings.xml

<servers>
    <server>
      <id>private-repo</id>
	   <configuration>
        <httpHeaders>
          <property>
            <name>Authorization</name>
            <!-- Base64-encoded username:access_token -->
            <value>Basic {Place the token here}</value>
          </property>
        </httpHeaders>
      </configuration>
    </server>
</server>

<repositories>
      <repository>
                    <id>private-repo</id>
                    `<name>Private` Repository</name>
                    <url>{Place the RAW github url here}</url>
                    <releases>
                        <enabled>true</enabled>
                        <updatePolicy>never</updatePolicy>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
       </repository>
</repositories>

@eirikh1996
Copy link

Still, source jars are not generated. How do I make it so that source jars and javadocs are generated?

@borsch
Copy link

borsch commented Sep 7, 2019

@eirikh1996
check this https://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html

you have to specify -Djavadoc & -Dsource arguments which is pathes to corresponding jar files like -Dfile argument

@prydin
Copy link

prydin commented Nov 6, 2019

The generated POM doesn't contain dependencies. How to I make it use the original POM instead and copy it to the proper directories?

@borsch
Copy link

borsch commented Nov 6, 2019

@prydin

please check this link https://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html. what you need is pomFile argument

@prydin
Copy link

prydin commented Nov 7, 2019

Thanks! I suspected it was something really simple I had overlooked!

@neo-expert
Copy link

neo-expert commented Jan 18, 2020

Yeah this worked. But isn't it a security risc?
Someone could provide a pom.xml and maven would just download a malicious library from a custom Repostory. Does it mean, we should check if the repostories defined in the pom are secure?

@OlivierLD
Copy link

On the first line, maybe git clone git clone should be replaced with just git clone ?

@smac89
Copy link

smac89 commented Apr 18, 2020

@dgallegos
The current state of Github maven registries is severely lacking in usability.

You can't delete packages if your repository was a fork, so your only option is to delete the fork and recreate the packages in a private repository.
Also don't bother uploading snapshots because you will never be able to resolve them in maven/gradle. It's quite annoying because they said in their blog, that they now support snapshots, but now I'm not sure what they meant by this.

I did some manual digging and I found that it even messes up the names of the packages.

For example I had uploaded a package with the name groupid: com.foo artifactid: com.foo.bar version: 0.1.0-SNAPSHOT.

When I sent a GET request to https://maven.pkg.github.com/<owner>/<repo>/com/foo/com.foo.bar/maven-metadata.xml, I get this back:

<metadata>
    <groupId>com.foo.com.foo</groupId>
    <artifactId>bar</artifactId>
    <versioning>
        <latest>0.1.0-SNAPSHOT</latest>
        <versions>
            <version>0.1.0-SNAPSHOT</version>
        </versions>
        <lastUpdated>20200418063644</lastUpdated>
    </versioning>
</metadata>

Doesn't make sense.

So I'd say don't bother using it yet until they've worked out the issues.

@firatkucuk
Copy link

firatkucuk commented May 3, 2020

I use repsy.io, It gives 1 GB of free private maven repository. It's enough for most of the projects.

@MikhailKaduchka
Copy link

MikhailKaduchka commented Jun 17, 2020

Thanks for tutor it helped me a lot.

For me next command works with sources and docs

 mvn install:install-file -DgroupId=com.foo -DartifactId=bar -Dversion=0.0.1 -Dfile=~/tmp/bar/target/bar-0.0.1.jar -Dpackaging=jar -DgeneratePom=true -DlocalRepositoryPath=.  -DcreateChecksum=true -Dsources=/~/tmp/bar/target/bar-0.0.1-sources.jar -Djavadoc=~/tmp/bar/target/bar-0.0.1-javadoc.jar -DpomFile=~/tmp/bar/pom.xml 

and to use it

    <repositories>
        <repository>
            <id>private-repo</id>
            <name>Private Repository</name>
            <url>https://raw.githubusercontent.com/GITHUB_PROFILE/PROJECT/repository/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

@baszero
Copy link

baszero commented Aug 31, 2020

@mikhail: i get a "400: Invalid request" if I call my repo like you mention.
What is the correct URL? The repo must be browsable within the browser in order to work within a regular maven build.

@OCTO26092020-Akash
Copy link

Excellent ! It works fine.

@priyanka2211
Copy link

How to find the correct url?
I get "400: Invalid request" for all the urls formats mentioned above

@droar
Copy link

droar commented Jan 25, 2021

Thanks

@elect86
Copy link

elect86 commented Apr 30, 2021

any way to directly publish on it without manually having to commit and push on the repository itself?

@elect86
Copy link

elect86 commented May 26, 2021

Sorry for shameless self-promotion, but I'd like to gather some feedbacks about one gradle plugin I've been working on: magik. Basically it allows to publish directly on a github repository acting as a maven repository. Github repos may serve for some nice custom solution since they allow up to 100GB total repo size with a max 100MB size for single file

@arathi-linvest21
Copy link

I m getting 400 for the url. any correctiosn to the url above?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment