Use site Maven plugin to build a public warehouse on GitHub

brief introduction

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

Building Maven warehouse requires servers and domain names. For the company, there are many domain names and servers, but it is too troublesome for us or small teams to share some very useful jar packages for others.

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

更多精彩内容且看:
* [区块链从入门到放弃系列教程-涵盖密码学,超级账本,以太坊,Libra,比特币等持续更新](https://blog.csdn.net/superfjj/article/details/106248377)
* [Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新](https://blog.csdn.net/superfjj/article/details/106226840)
* [Spring 5.X系列教程:满足你对Spring5的一切想象-持续更新](https://blog.csdn.net/superfjj/article/details/106226778)
* [java程序员从小工到专家成神之路(2020版)-持续更新中,附详细文章教程](https://blog.csdn.net/superfjj/article/details/105482751)

preparation in advance

To build 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 of GitHub. It's OK to use the user name and password directly, but it's not safe and we don't recommend it.

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

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/github-maven-repository

Configure GitHub permissions in maven

In this step, we need to edit setting XML file. Generally speaking, this file is in ~ / m2/settings. xml。

We need to add a server. If we directly use the user name and password of GitHub, it is like the following:

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

As 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 POM The XML file is used in the configuration. Let's 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 warehouse, we need to create 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 specify that MVN repo is created under the build directory of the project to store locally typed packages.

Next, we need to use Maven deploy plugin to specify to deploy the packed 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 during use:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>