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