maven打包指定依赖与commitId

news/2024/7/8 3:32:07 标签: maven, java

maven打包指定依赖与commitId

  • 1. 需求说明
  • 2. 实现方式
  • 3. 读取jar包git-properties配置
  • 4. 参考资料

1. 需求说明

这是两个需求,一个是打包指定依赖,另一个是打包时关联指定git的commitId。

2. 实现方式

maven打包关联commitId采用。

maven">......
  <plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>2.2.4</version>
    <executions>
      <execution>
        <id>get-the-git-infos</id>
        <goals>
          <goal>revision</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <!-- 使properties扩展到整个maven bulid 周期
      Ref: https://github.com/ktoso/maven-git-commit-id-plugin/issues/280 -->
      <injectAllReactorProjects>true</injectAllReactorProjects>
      <dateFormat>yyyy.MM.dd HH:mm:ss</dateFormat>
      <verbose>true</verbose>
      <!-- 是否生 git.properties 属性文件 -->
      <generateGitPropertiesFile>true</generateGitPropertiesFile>
      <!--git描述配置,可选;由JGit提供实现;-->
      <gitDescribe>
        <!--是否生成描述属性-->
        <skip>false</skip>
        <!--提交操作未发现tag时,仅打印提交操作ID,-->
        <always>false</always>
        <!--提交操作ID显式字符长度,最大值为:40;默认值:7; 0代表特殊意义;后面有解释; -->
        <abbrev>7</abbrev>
        <!--构建触发时,代码有修改时(即"dirty state"),添加指定后缀;默认值:"";-->
        <dirty>-dirty</dirty>
        <!--always print using the "tag-commits_from_tag-g_commit_id-maybe_dirty" format, even if "on" a tag. The distance will always be 0 if you're "on" the tag. -->
        <forceLongFormat>false</forceLongFormat>
      </gitDescribe>
    </configuration>
</plugin>
......

maven打包指定依赖采用,打入hello-api这个模块

maven">plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.6.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <minimizeJar>true</minimizeJar>
              <artifactSet>
                <includes>
                  <include>com.hello:hello-api</include>
                </includes>
              </artifactSet>
              <outputFile>
                ${project.build.directory}/${project.artifactId}-${project.version}-${git.commit.id}.jar
              </outputFile>
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>**/*.xml</exclude>
                  </excludes>
                </filter>
              </filters>
            </configuration>
          </execution>
        </executions>
      </plugin>

通过maven install查看打包结果:
git.properties

#Generated by Git-Commit-Id-Plugin
#Tue Jun 25 14:47:31 CST 2029
git.branch=
git.build.host=
git.build.time=
git.build.user.email=
git.build.user.name=
git.build.version=
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.id=
git.commit.id.abbrev=
git.commit.id.describe=
git.commit.id.describe-short=
git.commit.message.full=
git.commit.message.short=
git.commit.time=
git.commit.user.email=
git.commit.user.name=
git.dirty=true
git.remote.origin.url=https\://com.cn/
git.tags=

3. 读取jar包git-properties配置

java">import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class ReadFromJar {
    public static void main(String[] args) {
        String jarPath = "/market-plugin.jar";
        readGitPropertiesFromJar(jarPath);
    }

    public static void readGitPropertiesFromJar(String jarPath) {
    	Properties props = new Properties();
        try (JarFile jarFile = new JarFile(jarPath)) {
            JarEntry entry = jarFile.getJarEntry("git.properties");
            if (entry!= null) {
                try (InputStream inputStream = jarFile.getInputStream(entry);
                     BufferedInputStream bis = new BufferedInputStream(inputStream)) {
                    // 在此处对输入流进行读取和处理
                    props.load(bis);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Properties:{}", props);
    }
}

4. 参考资料

https://maven.apache.org/plugins/maven-help-plugin/

https://github.com/git-commit-id/git-commit-id-maven-plugin/blob/master/docs/using-the-plugin.md

Maven学习笔记 - git-commit-id-plugin插件

https://www.jianshu.com/p/85230aeb8ef9

https://springdoc.cn/spring-boot-build-with-git-commit-id-maven-plugin/

Maven: jar包名中自动添加git commit id


http://www.niftyadmin.cn/n/5536563.html

相关文章

学懂C#编程:实用方法——string字符串指定连接符拼接之 string.Join 的详细用法

在C#中&#xff0c;string.Join 方法用于将一个字符串数组或集合中的元素连接成一个单一的字符串&#xff0c;并在每个元素之间插入指定的分隔符。这个方法非常有用&#xff0c;特别是在需要将多个字符串合并成一个字符串时。以下是 string.Join 方法的详细用法&#xff1a; 方…

开源六轴协作机械臂myCobot280实现交互式乘法!让学习充满乐趣

本文经作者Fumitaka Kimizuka 授权我们翻译和转载。 原文链接&#xff1a;myCobotに「頷き」「首振り」「首傾げ」をしてもらう &#x1f916; - みかづきブログ・カスタム 引言 Fumitaka Kimizuka 创造了一个乘法表系统&#xff0c;帮助他的女儿享受学习乘法表的乐趣。她可以…

MYSQL8.0环境部署

创建用户 groupadd mysql useradd -g mysql mysql 删除原来的包 # rpm -qa|grep mysql # rpm -qa|grep mari mariadb-libs-5.5.68-1.el7.x86_64 # rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64 解压 cd /usr/local & mkdir mysql cd mysql # cp mysql-8…

数据结构 - C/C++ - 链表

公开视频 -> 链接点击跳转公开课程博客首页 -> 链接点击跳转博客主页 目录 结构特性 内存布局 结构样式 结构拓展 单链表 结构定义 节点关联 插入节点 删除节点 常见操作 双链表 环链表 结构容器 结构设计 结构特性 线性结构的存储方式 顺序存储 …

【数据结构】(C语言):堆(二叉树的应用)

堆&#xff1a; 此处堆为二叉树的应用&#xff0c;不是计算机中用于管理动态内存的堆。形状是完全二叉树。堆分两种&#xff1a;最大堆&#xff0c;最小堆。最大堆&#xff1a;每个节点比子树所有节点的数值都大&#xff0c;根节点为最大值。最小堆&#xff1a;每个节点比子树…

C语言 指针和数组——指针的算术运算

目录 指针的算术运算 指针加上一个整数 指针减去一个整数 指针相减 指针的关系比较运算 小结 指针的算术运算 指针加上一个整数 指针减去一个整数 指针相减 指针的关系比较运算 小结  指针变量 – 指针类型的变量&#xff0c;保存地址型数据  指针变量与其他类型…

ubuntu软件源的两种格式和环境变量

1. ubuntu的/etc是什么目录&#xff1f; 在Ubuntu操作系统中&#xff0c;/etc/是一个特殊的目录&#xff0c;它包含系统的配置文件。这些配置文件用于设置各种系统和应用程序的参数和选项。 一般来说&#xff0c;用户可以在这个目录下找到各种重要的配置文件&#xff0c;如网络…

Java增加线程后kafka仍然消费很慢

文章目录 一、问题分析二、控制kafka消费速度属性三、案例描述 一、问题分析 Java增加线程通常是为了提高程序的并发处理能力&#xff0c;但如果Kafka仍然消费很慢&#xff0c;可能的原因有&#xff1a; 网络延迟较大&#xff1a;如果网络延迟较大&#xff0c;即使开启了多线…