当前位置: 首页> 财经> 访谈 > maven打包指定依赖与commitId

maven打包指定依赖与commitId

时间:2025/7/10 12:02:28来源:https://blog.csdn.net/m0_37583655/article/details/140101672 浏览次数:1次

maven打包指定依赖与commitId

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

1. 需求说明

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

2. 实现方式

maven打包关联commitId采用。

......<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这个模块

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配置

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

关键字:maven打包指定依赖与commitId

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: