How to build your own maven warehouse with GitHub

Build your own maven warehouse with GitHub

I. background

Github has also written a lot of projects, and then a common problem is that many of the projects written by themselves can only be downloaded when they want to be used in another project, which is quite inconvenient.

Because most of the java back-end projects are based on Maven management dependency, I hope to have a common maven repository, which can throw my project in, and then it will be more convenient to apply.

Based on this, we have this tutorial.

II. Implementation steps

1. github Warehouse Establishment

The premise of creating a new repository is to have a github account number. By default, you can see that this article has an account number.

The first is to build a new warehouse on github with arbitrary commands, such as my new project is

2. Configuring local warehouses

Specify a directory locally and create a new folder, maven-repository, as shown in my local configuration

## Entry directory
cd /Users/yihui/GitHub

## new directory
mkdir maven-repository; cd maven-repository

## New repository directory
# Below this directory is the information about the project we deploy ed.
# That is to say, the directory designated by our project deploy is here
mkdir repository

## Add a new readme document
# Keep good habits and have a documentation for each project
touch README.md

Why is this directory structure like this?

Let's look directly at the default directory structure in the maven configuration and copy it out as well.

3. Warehouse Association

Linking a local warehouse to a remote github warehouse 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

Then there's branch management.

  • The agreement is to deploy the snapshot version of the project to the snapshot branch of the warehouse
  • It is agreed to deploy the release version of the project to the release branch of the warehouse.
  • master branch manages all versions

So you need to create two new branches

## Create snapshot branches
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 release branch
git checkout -b release
git push origin release

4. Project deploy ment

The deploy of the project needs to actively specify the address of the deploy, so our deploy command is as follows

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

The above command is more common, the main thing to note is that the parameters behind the file are replaced according to the local warehouse directory you set up earlier.

5. deploy script

It's not easy to remember the above commands, especially when different versions are deployed to different branches. It's also troublesome to switch branches and upload them on your own initiative, so it's necessary to write a deploy script.

Because the shell is really not very good at writing, the following script can only be used to make sense.

#!/bin/bash

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

## The deploy parameter, snapshot for snapshot packages, is abbreviated as s, release for formal packages, and r
arg=$1

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

deployFunc(){
  br=$1
  ## Snapshot package release
  cd $DEPLOY_PATH
  ## Switching Corresponding Branches
  git checkout $br
  cd $CURRENT_PATH
  # Start deploy
  mvn clean deploy -Dmaven.test.skip  -DaltDeploymentRepository=self-mvn-repo::default::file:/Users/yihui/GitHub/maven-repository/repository

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

  # Merge master branches
  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 release
  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

Put the script above into the project's root directory and execute it.

chmod +x deploy.sh

## Publish snapshot packages
./deploy.sh s
# sh deploy.sh snapshot can also be used

## Publish official packages
./deploy.sh r

Based on this, the whole process is completed.

III. use

The basic building of the above warehouse is ok, and then it is used. How should the pom file of maven be configured?

First, add the warehouse address

Add warehouse

If you want to distinguish snapshot from release, configure 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>

If you don't care, just add the following

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

After the warehouse is configured, you can directly introduce dependencies. If you depend on my Quick-Alarm package, you can add the following dependency configurations

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

IV. other

Personal blog: Z+|blog

Personal blog based on hexo + github pages records all the blog articles in study and work. Welcome to visit

statement

Letters are not as good as letters. They are purely family statements. Because of my general ability and limited knowledge, if I find bug s or have better suggestions, I am always welcome to criticize and correct them. My microblog address is: Small Ash Blog

Scanning concerns

Keywords: Maven snapshot git github

Added by adammc on Fri, 17 May 2019 19:54:16 +0300