Using site Maven plugin to build public warehouse on GitHub

Time:2020-11-21

[toc]

Using site Maven plugin to build public warehouse on GitHub

brief introduction

Maven is a build tool that we often use in developing Java programs. In the process of team development, if we want to share our jar package to others, we usually need to build our own Maven repository, and then upload the jar package to Maven warehouse for other users to use.

Building Maven repository requires servers and domain names. For companies, there are many domain names and servers. However, it would be too troublesome for us or small teams to share some very useful jar packages for others to use.

Recently, GitHub has seen a lot of good news. First, the number of repositories and collaborative users for individual users has been removed, and then enterprise users have been upgraded and reduced prices. If the warehouse is small, you can move the warehouse to GitHub.

See more:

  • A series of tutorials from introduction to abandonment of blockchain – covering cryptography, super ledger, Ethereum, Libra, bitcoin, etc
  • Spring boot 2. X Series tutorials: seven days to master spring boot – continuous update
  • Spring 5. X series of tutorials: meet your imagination of spring 5 – constantly updated
  • Java programmer’s road to becoming an expert (2020 Edition) – continuous update, with detailed articles and tutorials

For more information, please visit www.flydean.com

preparation in advance

To build Maven repository on GitHub, we need to use the Maven plug-in: site Maven plugin. Because you want to connect to GitHub, you need to set the OAuth permission of GitHub. It’s OK to use user name and password directly, but it’s not safe. We don’t recommend it.

Using site Maven plugin to build public warehouse on GitHub

As shown in the above figure, create an access token in Settings > developer Settings > personal access tokens. The required permissions are as follows:

Using site Maven plugin to build public warehouse on GitHub

Using site Maven plugin to build public warehouse on GitHub

Note that the user’s permission must be selected, otherwise an exception will be reported later.

With permissions, we will create a GitHub Maven repository to store data as an MVN repository.

If the generated address is: https://github.com/flydean/gi…

Configuring GitHub permissions in maven

We need to edit this step setting.xml File. Generally speaking, this file is in ~ /. M2/ settings.xml 。

We need to add a server. If the user name and password of GitHub are used directly, it will be as follows:

<server>
   <id>github</id>
    <username>YOUR_USERNAME</username>
    <password>YOUR_PASSWORD</password>
</server>

As we mentioned earlier, it is not safe to use the user name directly. We can use the OAuth key created above:

<server>
    <id>github</id>
    <password>OAUTH2TOKEN</password>
</server>

This ID will be in the back pom.xml It is used in file configuration. Here we will write it down first.

Configure deploy plugin

Our goal is to generate Maven dependencies that contain jar packages. Before uploading the jar package to the remote repository, we need to do it locally.

First configure a local repository:

<distributionManagement>
        <repository>
            <id>maven.repo</id>
            <name>Local Staging Repository</name>
            <url>file://${project.build.directory}/mvn-repo</url>
        </repository>
    </distributionManagement>

Above, we have specified to create MVN repo under the build directory of the project to store the locally typed package.

Next, we need to use Maven deploy plugin to deploy the package to the local repository we just specified.

<plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
            <configuration>
                <altDeploymentRepository>maven.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
            </configuration>
        </plugin>

Configure site Maven plugin

Now we can use the site Maven plugin

<plugin>
            <!-- Deploy the web site -->
            <groupId>com.github.github</groupId>
            <artifactId>site-maven-plugin</artifactId>
            <version>0.12</version>
            <executions>
                <execution>
                    <goals>
                        <goal>site</goal>
                    </goals>
                    <!-- select the Maven phase in which the plugin will be executed -->
                    <phase>deploy</phase>
                    <configuration>
                        <!-- Plugin configuration goes here -->
                        <server>github</server>
                        <!-- The commit message -->
                        <message>init git maven repository</message>
                        <!-- The location where the site is uploaded -->
                        <repositoryName>github-maven-repository</repositoryName> <!-- github repo name -->
                        <repositoryOwner>flydean</repositoryOwner> <!-- organization or user name  -->
                        <!-- Use merge or override the content -->
                        <merge>true</merge>
                        <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
                        <branch>refs/heads/mvn-repo</branch>
<!--                        <includes>-->
<!--                            <include>**/*</include>-->
<!--                        </includes>-->
                    </configuration>
                </execution>
            </executions>
        </plugin>

Pay attention to the following points in use:

  1. The goals of site Maven plugin are sites, which need to be associated with Maven’s deploy phase to automatically run site Maven plugin when we execute MVN deploy.
  2. For permission configuration of GitHub, we can set server = GitHub in configuration, or configure the following global variables:
    <properties>
        <github.global.server>github</github.global.server>
    </properties>
  1. You need to specify the repository name and repository owner, otherwise an error will be reported.
  2. Message represents the message submitted to GitHub.
  3. By default, the branch submitted to GitHub is refs / heads / GH pages. Here we have customized one.

Now that everything is configured, we can run MVN deploy:

Using site Maven plugin to build public warehouse on GitHub

As you can see from the above figure, there is already a shareable project on GitHub.

How to use this shared project

It’s very easy to use, just use the pom.xml Add the corresponding dependency and repository to the file

<dependency>
    <groupId>YOUR.PROJECT.GROUPID</groupId>
    <artifactId>ARTIFACT-ID</artifactId>
    <version>VERSION</version>
</dependency>

<repository>
    <id>ARTIFACT-ID</id>
    <url>https://raw.github.com/flydean/github-maven-repository/mvn-repo/</url>
</repository>

summary

Let’s use the benefits GitHub brings us.

Examples of this article[ https://github.com/ddean2009/
learn-java-base-9-to-20](https://github.com/ddean2009/…

The author of this article: what about the flybean program

Link to this article: http://www.flydean.com/apache-maven-git-repository/

Source: flyden’s blog

Welcome to my official account: the procedures, the more wonderful things waiting for you!