Github releases open source JAR packages as a maven repository

github releases open source JAR packages as a maven repository

1. Preparation:

  1. Create a warehouse on github to store jars. For example: maven-repository (warehouse address)
  2. Create a token (address link) on github, because we need to upload the jar to the warehouse, and permission issues cannot be avoided, but it is not safe to use the account password directly.
  3. Configure maven’s setting.xml
<servers>
  <server>
    <id>your-github</id><!-- Customized id. Remember this ID, you will need to use it when publishing the jar package later -->
    <password>your-token</password><!-- the token mentioned above -->
  </server>
</servers>

2. Generate JAR package

1. Configure pom.xml in the project

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--Project information-->
    <groupId>cn.dujy.mybatis</groupId><!-- groupId, recommended: prefix (company). Group name (yourself). Project related (required) -->
    <artifactId>spring-boot-mybatis-plus-starter</artifactId><!-- Usually the project name (required) -->
    <version>1.0.2</version><!-- Version (required) -->
    <name>spring-boot-mybatis-plus-starter</name><!-- Usually the project name (not required) -->
    <description>Secondary development based on mybatis-plus</description><!-- Details (optional) -->

    <!--Springboot's parent project: manages non-conflicting versions of quite a few jars-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
    </parent>

    <!--Unified version-->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!--Specific dependencies-->
    <dependencies>
          <!--Dependencies required in the project-->
    </dependencies>

    <!--Packaging configuration-->
    <build>
        <!-- Used to build standard maven packages and upload them to GitHub -->
        <plugins>

            <!--Used to compile Java source code. This plug-in allows you to define and configure Java compilation behavior in Maven projects, including source code version, target bytecode version, etc. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>

            <!--Used to compile Java source code. This plug-in allows you to define and configure Java compilation behavior in Maven projects, including source code version, target bytecode version, etc. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <target>1.8</target>
                    <source>1.8</source>
                    <!--Exclude files-->
                    <excludes>
                        <exclude>
<!--This is to exclude startup items (change according to your own needs)-->
                            cn/dujy/mybatis/SpringbootDomeApplication.java
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <!--Used to generate and manage the source code of the project (usually Java source code) and the accompanying source code JAR file-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!--Used to deploy build products (usually JAR files, WAR files, etc.) to the specified Maven repository-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <configuration>
                    <!-- Configure the local warehouse storage address after local packaging. Subsequent uploaded jar packages will be fetched from this warehouse -->
                    <altDeploymentRepository>
                        internal.repo::default::file://${project.build.directory}/maven-repository
                    </altDeploymentRepository>
                </configuration>
            </plugin>

            <!--For publishing to the specified warehouse-->
            <plugin>
                <groupId>com.github.github</groupId>
                <artifactId>site-maven-plugin</artifactId>
                <version>0.12</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>site</goal>
                        </goals>
                        <phase>deploy</phase>
                        <configuration>
                            <!--Service name (configuration content in settings.xml, id of the server configured above) -->
                            <server>your-github</server>
                            <!-- Commit message (commit message) -->
                            <message>Maven artifacts for [${project.artifactId}-${project.version}]</message>
                            <!-- The location of the uploaded website (remote warehouse name) -->
                            <repositoryName>maven-repository</repositoryName>
                            <!--Organization or username-->
                            <repositoryOwner>Davisesr</repositoryOwner>
                            <!-- Use merge or overwrite content -->
                            <merge>true</merge>
                            <!--Branch (fixed writing method: refs/heads/ + your group)-->
                            <branch>refs/heads/main</branch>
                            <!--Source file address-->
                            <outputDirectory>${project.build.directory}/maven-repository</outputDirectory>
                            <!-- Contains all contents in the folder filled in the outputDirectory tag -->
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2. Terminal execution:

mvn clean deploy

After packaging is completed, it will be automatically uploaded to the github warehouse according to the configured content.

3. Use JAR package

1.pom.xml

<!--1. Use -->
<dependencies>
  <dependency>
    <groupId>cn.dujy.mybatis</groupId>
    <artifactId>spring-boot-mybatis-plus-starter</artifactId>
    <version>1.0.2</version>
  </dependency>
</dependencies>

<repositories>
  <repository>
    <id>your-repository-id</id> <!--This id needs to be configured in the setting-->
    <!--
      1. https://raw.github.com => Fixed writing method. If the connection fails during compilation, you can change https to http and try again.
      2. davisesr => github login user name
      3. maven-repository => stores the released jar warehouse
      4. main => which branch to get the jar package from
-->
    <url>http://raw.github.com/davisesr/maven-repository/main</url>
    <snapshots>
      <enabled>true</enabled>
      <updatePolicy>always</updatePolicy>
    </snapshots>
  </repository>
</repositories>

2. Configure maven’s setting.xml

<!-- Configuration 1:
  Note: <mirrorOf>*,!your-github</mirrorOf>
  Original: <mirrorOf>*</mirrorOf>
-->
<mirror><!--Use Alibaba Cloud mirror here, add !your-repository-id-->
  <id>nexus-aliyun</id>
  <mirrorOf>*,!your-repository-id</mirrorOf>
  <name>Nexus aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>