Build your own Maven repository with GitHub

I. background

I’ve also written a lot of projects on Github, and one of the problems I often encounter is that when I want to use my own projects in another project, I have to download the project, which is quite inconvenient

Since most Java back-end projects rely on Maven to manage dependencies, it is desirable to have a common Maven repository into which you can drop your own projects and then apply them

This is where this tutorial comes in

II. Implementation steps

1. Set up the Github repository

To create a repository, you must have a Github account, which you will have by default

The first step is to create a new repository on Github with arbitrary commands, such as my new project

  • Github.com/liuyueyi/ma…

2. Configure the local repository

Locally specify a directory and create a maven-repository folder, as shown in my local configuration below

Enter the directory
cd /Users/yihui/GitHub

## Create a directory
mkdir maven-repository; cd maven-repository

Create a new repository directory
# This directory contains information about the projects we deploy
This is the directory where our project deploy is specified
mkdir repository

Add a readme document
Keep it a good practice to have a documentation for each project
touch README.md
Copy the code

Why is this directory structure the way it is?

Let’s look directly at the default directory structure in maven configuration and copy it again

3. Warehouse association

Associating local repositories with remote Github repositories makes it easier to execute commands

git add .
git commit -m 'first comit'
git remote add origin https://github.com/liuyueyi/maven-repository.git
git push -u origin master
Copy the code

Then there’s branch management

  • The convention is to deploy the Snapshot version of the project to the Snapshot branch of the repository
  • The convention deploys the release version of the project to the release branch of the repository
  • The master branch manages all versions

So we need to create two new branches

Create the snapshot branch
git checkout -b snapshot 
git push origin snapshot
You can also use git branch snapshot, which I usually use, to create and switch branches

Create the release branch
git checkout -b release
git push origin release
Copy the code

4. The deploy of the project

To deploy the project, we need to proactively specify the location of deploy, so our deploy command is as follows

## Deploy the project to a local repository
mvn clean deploy -Dmaven.test.skip  -DaltDeploymentRepository=self-mvn-repo::default::file:/Users/yihui/GitHub/maven-repository/repository
Copy the code

The command above is more common, the main need to pay attention to is the parameter after file, according to their previous set of local repository directory to replace

5. The deploy script

It is not easy to remember, especially when different versions of the command deploy to different branches, active to switch branches and upload, also quite troublesome, so it is necessary to write a deploy script

Since the shell is not very good at writing, the following scripts are only passable

#! /bin/bash
if [ $# != 1 ];then
  echo 'deploy argument [snapshot(s for short) | release(r for short) ] needed! '
  exit 0
fi

## deploy parameter: snapshot indicates the snapshot package (s), release indicates the official package (R)
arg=The $1

DEPLOY_PATH=/Users/yihui/GitHub/maven-repository/
CURRENT_PATH=`pwd`

deployFunc(){
  br=The $1
  ## Snapshot package released
  cd $DEPLOY_PATH
  Switch the corresponding branch
  git checkout $br
  cd $CURRENT_PATH
  # started to deploy
  mvn clean deploy -Dmaven.test.skip  -DaltDeploymentRepository=self-mvn-repo::default::file:/Users/yihui/GitHub/maven-repository/repository

  # deploy completed
  cd $DEPLOY_PATH
  git add -am 'deploy'
  git push origin $br

  Merge the master branch
  git checkout master
  git merge $br
  git commit -am 'merge'
  git push origin master
  cd $CURRENT_PATH
}

if [ $arg = 'snapshot'] | | [$arg = 's' ];then
  ## Snapshot package released
  deployFunc snapshot
elif [ $arg = 'release'] | | [$arg = 'r' ];then
  ## Official package release
  deployFunc release
else
  echo 'argument should be snapshot(s for short) or release(r for short). like: `sh deploy.sh snapshot` or `sh deploy.sh s`'
fi
Copy the code

Put the above script into the root directory of the project and execute it

chmod +x deploy.sh

## Release snapshot package
./deploy.sh s
# sh deploy. Sh snapshot also works

## Release the official package
./deploy.sh r
Copy the code

Based on this, the whole step is complete

III. Use

Maven’s poM files should be configured in the same way that maven’s POM files are configured.

The first is to add the warehouse address

Add the warehouse

To distinguish snapshot from release, do the following

<repositories>
    <repository>
        <id>yihui-maven-repo-snap</id>
        <url>https://raw.githubusercontent.com/liuyueyi/maven-repository/snapshot/repository</url>
    </repository>
    <repository>
        <id>yihui-maven-repo-release</id>
        <url>https://raw.githubusercontent.com/liuyueyi/maven-repository/release/repository</url>
    </repository>
</repositories>
Copy the code

If you do not care, simply add the following

<repositories>
    <repository>
        <id>yihui-maven-repo</id>
        <url>https://raw.githubusercontent.com/liuyueyi/maven-repository/master/repository</url>
    </repository>
</repositories>
Copy the code

Once the repository is configured, you can simply import dependencies, such as my Quick-alarm package, to add the following dependency configuration

<dependency>
  <groupId>com.hust.hui.alarm</groupId>
  <artifactId>core</artifactId>
  <version>0.1</version>
</dependency>
Copy the code

IV. The other

Personal Blog:Z+|blog

Hexo + Github Pages, hexo + Github Pages, hexo + Github Pages, Hexo + Github Pages

The statement

As far as the letter book is not as good as, has been on the content, pure one’s opinion, because of my ability is general, knowledge is limited, if you find a bug or have a better suggestion, welcome criticism and correction at any time, my micro Blog address: small gray Blog

Scanning attention