Maven - 发布JAR包到Maven远程中央仓库(二)

本文涉及的产品
对象存储 OSS,20GB 3个月
对象存储 OSS,恶意文件检测 1000次 1年
对象存储 OSS,内容安全 1000次 1年
简介: Maven - 发布JAR包到Maven远程中央仓库(二)

二、配置Maven(pom.xml)

接下来就是重头戏了,pom.xml是一个maven项目的重点配置,一个项目的所有配置都可以由这个文件来描述,文件中的所有配置都有默认值,也就是说所有的配置都是可选配置,但是为了把构件发布到中央仓库,我们必须配置一些关键信息,否则再发布时是不会通过了。


在工程的pom.xml文件中,引入Sonatype官方的一个通用配置oss-parent,这样做的好处是很多pom.xml的发布配置不需要自己配置了:


<parent>
    <groupId>org.sonatype.oss</groupId>
    <artifactId>oss-parent</artifactId>
    <version>7</version>
</parent>

并增加Licenses、SCM、Developers信息:

<licenses>
  <license>
    <name>The Apache Software License, Version 2.0</name>
    <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    <distribution>repo</distribution>
  </license>
</licenses>
<developers>
  <developer>
    <name>Lux Sun</name>
    <email>28554482@qq.com</email>
    <organization>Lux Sun</organization>
    <url>https://github.com/LuxSun</url>
  </developer>
</developers>
<scm>
  <url>https://github.com/LuxSun/requestjson</url>
  <connection>https://github.com/LuxSun/requestjson.git</connection>
</scm>

修改maven配置文件setting.xml,在servers中增加server配置。

<servers>
  <server>
    <id>sonatype-nexus-snapshots</id>
    <username>Sonatype 账号</username>
    <password>Sonatype 密码</password>
  </server>
  <server>
    <id>sonatype-nexus-staging</id>
    <username>Sonatype 账号</username>
    <password>Sonatype 密码</password>
  </server>
</servers>

根据官方指南,这里需要4个插件,


maven-source-plugin 用来生成Source Jar文件

maven-javadoc-plugin 用来生成 javadoc 文档

maven-gpg-plugin 用来对工程文件进行自动签名

nexus-staging-maven-plugin 用来将工程发布到中央仓库

另外注意生成javadoc文档时需要指定关闭doclint,不然可能因为使用了不规范的javadoc注解而导致失败,完整配置如下。


完整版


<?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>
  <groupId>com.luxsuen</groupId>
  <artifactId>requestjson</artifactId>
  <version>1.0.1</version>
  <packaging>jar</packaging>
  <name>requestjson</name>
  <description>RequestJson</description>
  <url>https://github.com/LuxSun/requestjson</url>
  <properties>
    <spring.version>4.3.2.RELEASE</spring.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
  </properties>
  <licenses>
    <license>
      <name>The Apache Software License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      <distribution>repo</distribution>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>Lux Sun</name>
      <email>28554482@qq.com</email>
      <organization>Lux Sun</organization>
      <url>https://github.com/LuxSun</url>
    </developer>
  </developers>
  <scm>
    <url>https://github.com/LuxSun/requestjson</url>
    <connection>https://github.com/LuxSun/requestjson.git</connection>
  </scm>
  <distributionManagement>
    <snapshotRepository>
      <id>ossrh</id>
      <name>oss Snapshots Repository</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </snapshotRepository>
    <repository>
      <id>ossrh</id>
      <name>oss Staging Repository</name>
      <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
    </repository>
  </distributionManagement>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <executions>
            <execution>
              <id>default-jar</id>
              <phase>package</phase>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <excludes>
              <exclude>**/spring/</exclude>
              <exclude>**/com/luxsuen/requestjson/entity/</exclude>
              <exclude>**/com/luxsuen/requestjson/web/</exclude>
              <exclude>**/META-INF/*.kotlin_module</exclude>
            </excludes>
          </configuration>
        </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>release</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <excludes>
                <exclude>**/spring/</exclude>
                <exclude>**/com/luxsuen/requestjson/entity/</exclude>
                <exclude>**/com/luxsuen/requestjson/web/</exclude>
                <exclude>**/META-INF/*.kotlin_module</exclude>
              </excludes>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9.1</version>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <excludePackageNames>com.luxsuen.requestjson.entity:com.luxsuen.requestjson.web</excludePackageNames>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>1.5</version>
            <executions>
              <execution>
                <id>sign-artifacts</id>
                <phase>verify</phase>
                <goals>
                  <goal>sign</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.sonatype.plugins</groupId>
            <artifactId>nexus-staging-maven-plugin</artifactId>
            <version>1.6.7</version>
            <extensions>true</extensions>
            <configuration>
              <serverId>ossrh</serverId>
              <nexusUrl>https://oss.sonatype.org/</nexusUrl>
              <autoReleaseAfterClose>true</autoReleaseAfterClose>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>

注意1:以上 pom.xml 必须包括:name、description、url、licenses、developers、scm 等基本信息,此外,使用了 Maven 的 profile 功能,只有在 release 的时候,创建源码包、创建文档包、使用 GPG 进行数字签名。此外,snapshotRepository 与 repository 中的 id 一定要与 setting.xml 中 server 的 id 保持一致。


注意2:这里在 nexus-staging-maven-plugin 插件里开启了自动 Release。也可以关掉,然后登录构件仓库 https://oss.sonatype.org 手动去 close 然后 relseae。


相关实践学习
借助OSS搭建在线教育视频课程分享网站
本教程介绍如何基于云服务器ECS和对象存储OSS,搭建一个在线教育视频课程分享网站。
目录
相关文章
|
21天前
|
JavaScript Java Maven
云效产品使用常见问题之android sdk 构建出aar后,上传到私有maven仓库失败如何解决
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
3天前
|
Java Maven
向 Maven 中央仓库上传一个修改过的基于jeecg的autoPOI的 jar包记录(一)
向 Maven 中央仓库上传一个修改过的基于jeecg的autoPOI的 jar包记录
15 0
|
3天前
|
Java Go Maven
向 Maven 中央仓库上传一个修改过的基于jeecg的autoPOI的 jar包记录(二)
向 Maven 中央仓库上传一个修改过的基于jeecg的autoPOI的 jar包记录
10 0
|
12天前
|
XML 存储 Java
Maven 仓库
Maven仓库用于存储项目依赖,包括本地、中央和远程三种类型。本地仓库在首次执行Maven命令时自动创建,默认位于%USER_HOME%/.m2/repository/。若本地缺少依赖,Maven会从远程仓库下载至本地。要更改本地仓库位置,可在settings.xml中配置`&lt;localRepository&gt;`标签。例如: ```xml &lt;localRepository&gt;C:/MyLocalRepository&lt;/localRepository&gt; ``` Maven首先从本地仓库获取构件,若不存在,则从远程仓库下载。
|
14天前
|
机器学习/深度学习 人工智能 运维
人工智能平台PAI产品使用合集之机器学习PAI中怎么拉到maven仓库的包
阿里云人工智能平台PAI是一个功能强大、易于使用的AI开发平台,旨在降低AI开发门槛,加速创新,助力企业和开发者高效构建、部署和管理人工智能应用。其中包含了一系列相互协同的产品与服务,共同构成一个完整的人工智能开发与应用生态系统。以下是对PAI产品使用合集的概述,涵盖数据处理、模型开发、训练加速、模型部署及管理等多个环节。
|
17天前
|
存储 安全 Java
maven仓库的版本列举
这段代码是Java程序,用于分析本地Maven项目的依赖版本。它遍历指定路径下的文件,提取groupId和version信息,并存储到HashSet中。最终,这些信息被写入到一个Excel文档。主要类`test`包含一个静态内部类`Version`来封装groupId和version字段。通过递归方法`func`处理文件夹结构,获取Maven坐标信息。
18 3
|
22天前
|
Java API 持续交付
云效产品使用常见问题之maven仓库迁移如何解决
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
1月前
|
Java Maven 数据安全/隐私保护
如何上传自己的Jar到Maven中央仓库
如何上传自己的Jar到Maven中央仓库
49 0
|
1月前
|
Java Docker 容器
|
1月前
|
运维 Java Shell
Linux非常详细的shell运维脚本一键启动停止状态SpringBoot打成可运行jar包
Linux非常详细的shell运维脚本一键启动停止状态SpringBoot打成可运行jar包
35 0

推荐镜像

更多