Use site-maven-plugin to build public warehouses on github

brief introduction

Maven is a building tool that we often use in developing java programs. During team development, if we want to share our own written jar packages with others, we usually need to build our own Maven repository and upload the written jar packages to the Maven repository for other users to use.

Building a maven repository requires a server and domain name. There are a lot of domains and servers for companies, but it would be too cumbersome for us or small teams to share some very useful jar packages for others to use.

Recently, the good news for Github has been that the number of repositories and collaborators has been removed for individual users, followed by upgrades and price reductions for enterprise users.If the warehouse is small, you can move it to github.

More interesting and see:

More please visit www.flydean.com

preparation in advance

To build a maven repository on github, we need to use maven's plug-in: site-maven-plugin.Because you want to connect to github, you need to set the oauth permission for github.It's okay to use a username password directly, but it's not safe and we don't recommend it.

As shown in the figure above, create an access tokens in Settings->Developer settings->Personal access tokens with the following permissions:

Note that the user must choose permissions here, otherwise an exception will be reported later.

With privileges, let's create a github-maven-repository to store data as an mvn repository.

If the generated address is: https://github.com/flydean/github-maven-repository

Configure GitHub permissions in maven

This step we need to editSetting.xmlFile, normally this file is ~/.m2/settings.xml.

We need to add a Server that uses github's username password directly, like the following:

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

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

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

This id will followPom.xmlThis is used in the file configuration, so let's write it down here first.

Configure deploy-plugin

Our goal is to generate maven dependencies that contain jar packages.Before uploading jar packages to a remote warehouse, we need to build them locally.

Configure a local repository first:

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

Above we specified that mvn-repo was created under the project's build directory to store locally typed package s.

Next, we need to use maven-deploy-plugin to specify that the packages will be deployed 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 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>

The following points should be noted during 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 deployment.

  2. Github's permission configuration, we can set server=github in the configuration, or we can configure the following global variables:

    <properties>
        <github.global.server>github</github.global.server>
    </properties>
  1. repositoryName and repositoryOwner need to be specified, otherwise errors will be reported.

  2. Message represents a message submitted to github.

  3. By default, the branch submitted to github is refs/heads/gh-pages, which we customize here.

Okay, everything is configured and we can run mvn deploy:

As you can see from the above image, there is already a project on github that can be shared.

How to use this shared project

It's easy to use, just need toPom.xmlAdd the appropriate dependencies and repositories 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

Github's benefits for us. Get them started.

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

Author: Fldean Programs

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

Source: Fldean's blog

Welcome to my Public Number: program stuff, more exciting waiting for you!

Tags: Programming github Maven Spring Java

Posted on Tue, 26 May 2020 15:55:01 -0700 by eddiegster