Use github to build a personal maven repository, githubmaven
Source: hengyunabc
Origin
Previously, I saw an open-source project using github as a maven repository, and I thought I would do it myself. Under research, record.
To put it simply, there are three steps:
Configure local file maven repository deploy to local
Maven can use deploy such as http, ftp, and ssh to connect to a remote server, or deploy to a local file system.
For example, deploy the project/home/hengyunabc/code/maven-repo/repository/
Directory:
123456 |
<distributionManagement> <repository> <id>hengyunabc-mvn-repo</id> <url>file:/home/hengyunabc/code/maven-repo/repository/</url> </repository> </distributionManagement> |
The command line is:
1 |
mvn deploy -DaltDeploymentRepository=hengyunabc-mvn-repo:: default ::file:/home/hengyunabc/code/maven-repo/repository/ |
We recommend that you use command line deploy to avoid explicit configuration in the project.
Https://maven.apache.org/plugins/maven-deploy-plugin/
Https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html
Submit the local repository to github
Deploy the project to the local directory.home/hengyunabc/code/maven-repo/repository
And submit the directory to github.
Create a new project on Github andhome/hengyunabc/code/maven-repo
All files under are submitted to gtihub.
123456 |
cd /home/hengyunabc/code/maven-repo/ git init git add repository/* git commit -m 'deploy xxx' git remote add origin git @github .com:hengyunabc/maven-repo.git git push origin master |
For the final result, refer to my personal Repository:
Https://github.com/hengyunabc/maven-repo
Use of github maven Repository
Because github usesraw.githubusercontent.com
This domain name is used for raw file download. To use this maven repository, you only need to add the following in pom. xml:
123456 |
<repositories> <repository> <id>hengyunabc-maven-repo</id> <url>https: //raw.githubusercontent.com/hengyunabc/maven-repo/master/repository</url> </repository> </repositories> |
Directory viewing and searching
It is worth noting that, for security reasons, github separates the raw file download from the original github domain name.raw.githubusercontent.com
This domain name does not support directory browsing. Therefore, if you want to browse the file directory or search for it, you can directly view it in the repository under the github domain name.
For example, this filemybatis-ehcache-spring-0.0.1-20150804.095005-1.jar
:
The browser address is:
Https://github.com/hengyunabc/maven-repo/blob/master/repository/io/github/hengyunabc/mybatis-ehcache-spring/0.0.1-SNAPSHOT/mybatis-ehcache-spring-0.0.1-20150804.095005-1.jar
Its maven repository url is:
Https://raw.githubusercontent.com/hengyunabc/maven-repo/master/repository/io/github/hengyunabc/mybatis-ehcache-spring/0.0.1-SNAPSHOT/mybatis-ehcache-spring-0.0.1-20150804.095005-1.jar
Working Mechanism of maven Repository
The following describes how maven repositories work. A typical maven dependency includes the following three files:
Https://github.com/hengyunabc/maven-repo/tree/master/repository/io/github/hengyunabc/mybatis-ehcache-spring/0.0.1-SNAPSHOT
123 |
maven-metadata.xml maven-metadata.xml.md5 maven-metadata.xml.sha1 |
maven-metadata.xml
It records the last deploy version and time.
1234567891011121314 |
<?xml version= "1.0" encoding= "UTF-8" ?> <metadata modelVersion= "1.1.0" > <groupId>io.github.hengyunabc</groupId> <artifactId>mybatis-ehcache-spring</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <versioning> <snapshot> <timestamp> 20150804.095005 </timestamp> <buildNumber> 1 </buildNumber> </snapshot> <lastUpdated> 20150804095005 </lastUpdated> </versioning> </metadata> |
The md5 and sha1 verification files are used to ensure the integrity of the meta File.
When maven is compiling a development project, it first tries to requestmaven-metadata.xml
If not found, the system will directly request the jar file. When downloading the jar file, it will also try to download the md5 and sha1 files of the jar.
maven-metadata.xml
File is very important. If this file is not used to specify the latest jar version, even if the jar version in the remote repository is updated, the-U parameter is used during local maven compilation, it won't pull the latest jar!
Therefore, you cannot simply put the jar package on github. You must first Deploy it locally to generatemaven-metadata.xml
File and upload it to github.
Reference: http://maven.apache.org/ref/3.2.2/maven-repository-metadata/repository-metadata.html
Maven repository relationship
Https://maven.apache.org/repository/index.html
Configure local repository
To use the local file repository, configure it in the pom. xml file of the project, for example:
123456 |
<repositories> <repository> <id>hengyunabc-maven-repo</id> <url>file:/home/hengyunabc/code/maven-repo/repository/</url> </repository> </repositories> |
Notes
The repository of maven does not have a priority configuration and cannot be configured separately for some dependencies. Therefore, if the project is configured with multiple repository, the dependencies will be downloaded in sequence during the first compilation. If it is not found, try the next one, and the entire process will be long.
Therefore, try to store as many dependencies as possible in the same repository. Do not have a repository of your own for each project. QQ technology exchange group 290551701