手把手教你将项目打成jar到maven官网

124 阅读2分钟

注册

1.首先去central.sonatype.com/注册账号,可以授权github账号,此步骤省略。

创建token

1.继上一步注册完成后到central.sonatype.com/publishing创建一个属于用户的token会生成出如下配置,并将配置配置在maven的settings文件中,{server}替换成自己的,pom文件会用到id如图:

image.png

命名空间

1.添加一个命名空间io.github.test,其中test是命名空间的名字,下面会用到如图:

image.png

验证

1.上图是我验证过的命名空间,实际上需要验证命名空间如下图:

image.png

2.点击Verify Namespace会弹出确认框,红框的地址就是你的github中public仓库的地址其中ssss(github用户名),0wzj042zqn是创建的仓库:

image.png

image.png

3.创建好了点击 Confirm,刷新页面后看见命名空间显示绿色状态,开始安装gpg image.png

gpg安装与配置

1.gpg自行查询网上教程,安装完以后先执行gpg --gen-key会让你提供命名空间名和邮箱,也就是图中test的,最后会让输入一个密码,这个密码一定要记住,发布jar时候会频繁用到,然后选择(O)kay 回车即可会生成一个ID序列。在执行

gpg --keyserver keyserver.ubuntu.com --send-keys 0129C6A5861715A1463ED3C2086CE0485A98FC6D(你的ID序列),到此gpg操作完成。

image.png

代码配置

1.接下来开始配置项目,由于配置太多,直接上代码:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>io.github.nimblejay</groupId>
    <artifactId>univTool</artifactId>
    <version>0.0.1-beta</version>
    <packaging>jar</packaging>
    <name>univTool</name>
    <description>Demo project for Spring Boot</description>
    <url>https://github.com/nimbleJay/1vs0eqw7ph.git</url>

    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>univTool</id>
            <name>灵动</name>
            <email>380492222@qq.com</email>
            <roles>
                <role>Project Manager</role>
                <role>Architect</role>
            </roles>
        </developer>
    </developers>

    <scm>
        <connection>https://github.com/nimbleJay/1vs0eqw7ph.git</connection>
        <developerConnection>git@github.com:nimbleJay/1vs0eqw7ph.git
        </developerConnection>
        <url>https://github.com/nimbleJay/1vs0eqw7ph</url>
    </scm>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
        <lombok.version>1.18.12</lombok.version>
        <commons-lang3.version>3.9</commons-lang3.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>
    </dependencies>
    <dependencyManagement>

    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <!-- Source -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.1.0</version>
                <inherited>true</inherited>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <excludeResources>true</excludeResources>
                    <useDefaultExcludes>true</useDefaultExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <!-- Javadoc -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.10.4</version>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- GPG -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.6</version>
                        <configuration>
                            <!-- 改成你自己的gpg安装路径 -->
                            <executable>D:\gpg\GnuPG\bin\gpg.exe</executable>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.sonatype.central</groupId>
                        <artifactId>central-publishing-maven-plugin</artifactId>
                        <version>0.4.0</version>
                        <extensions>true</extensions>
                        <configuration>
                            <!-- 这里的serverId是之前在settings.xml中配置的 -->
                            <publishingServerId>univTool</publishingServerId>
                            <tokenAuth>true</tokenAuth>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

打包

1.最后clean-deploy打包,打包成功会弹出密码输入框,就是gpg生成时候设置的密码,输入后会同步到central.sonatype.com/publishing,可以看见打包是否成功如图:

image.png

image.png

2.到此发布就成功了,这是本人研究了2天的成果,由于网上资料不是很多,希望能提供帮助给大家。 另外本人发布了开源的工具类univTool,希望帮忙测试和不足,后续会继续维护。