温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • maven打包时候修改包名称带上git版本号和打包时间的方法是什么

maven打包时候修改包名称带上git版本号和打包时间的方法是什么

发布时间:2023-04-07 17:45:09 来源:亿速云 阅读:274 作者:iii 栏目:开发技术

本文小编为大家详细介绍“maven打包时候修改包名称带上git版本号和打包时间的方法是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“maven打包时候修改包名称带上git版本号和打包时间的方法是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

maven打包时候修改包名称带上git版本号和打包时间

使用 maven 插件 git-commit-id-plugin 可以获取项目的git信息,然后,使用这个信息,修改打包的名称,使其带上git版本号以及打包时间。

	<build>
        <finalName>${artifactId}-${git.commit.id.abbrev}-${git.build.time}</finalName>
        <plugins>
            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>2.1.5</version>
                <executions>
                    <execution>
                        <id>get-the-git-infos</id>
                        <!-- 默认绑定阶段initialize -->
                        <phase>initialize</phase>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--日期格式;默认值:dd.MM.yyyy '@' HH:mm:ss z;-->
                    <dateFormat>yyyy-MM-dd_HH-mm-ss</dateFormat>
                    <!--,构建过程中,是否打印详细信息;默认值:false;-->
                    <verbose>true</verbose>
                    <!-- ".git"文件路径;默认值:${project.basedir}/.git; ${project.basedir}:项目根目录,即包含pom.xml文件的目录-->
                    <dotGitDirectory>${project.basedir}/../../../.git</dotGitDirectory>
                    <!--若项目打包类型为pom,是否取消构建;默认值:true;-->
                    <skipPoms>false</skipPoms>
                    <!--是否生成"git.properties"文件;默认值:false;-->
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                    <!--指定"git.properties"文件的存放路径(相对于${project.basedir}的一个路径);-->
                    <generateGitPropertiesFilename>/src/main/resources/git.properties</generateGitPropertiesFilename>
                    <!--".git"文件夹未找到时,构建是否失败;若设置true,则构建失败;若设置false,则跳过执行该目标;默认值:true;-->
                    <failOnNoGitDirectory>true</failOnNoGitDirectory>
 
                    <!--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>
        </plugins>
    </build>

实际运行结果:

maven打包时候修改包名称带上git版本号和打包时间的方法是什么

git.properties文件内容

#Generated by Git-Commit-Id-Plugin
#Fri Nov 12 15:06:14 CST 2021
git.commit.id.abbrev=ff60f80
git.commit.user.email=xxx@163.com
git.commit.message.full=git提交说明
git.commit.id=ff60f8091627e53891fc15bdccad93115f8623c9
git.commit.message.short=简要说明
git.commit.user.name=abc
git.build.user.name=efg
git.commit.id.describe=xxxx
git.build.user.email=xxx@163.com
git.branch=xxx-dev
git.commit.time=2011-11-09_14-00-40
git.build.time=2011-11-12_15-06-14
git.remote.origin.url=http\://1.1.1.1\:1/group/xxx.git

maven打包日常总结

1、 将第三方依赖性jar包中的文件打包入jar中,打包时修改引入jar包的包名,防止包冲突

 <!--将第三方依赖性jar包中的文件打包入jar中-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <!-- 打包失败可能是版本太低,提高版本 -->
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <!-- 打包时修改引入jar包的包名,防止包冲突 -->
                            <relocations>
                                <relocation>
                                    <pattern>org.apache.http</pattern>
                                    <shadedPattern>shaded.org.apache.http</shadedPattern>
                                    <!--<excludes>-->
                                    <!--<exclude>org.codehaus.plexus.util.xml.Xpp3Dom</exclude>-->
                                    <!--<exclude>org.codehaus.plexus.util.xml.pull.*</exclude>-->
                                    <!--</excludes>-->
                                </relocation>
                            </relocations>
 
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

2、阻止第三方jar包被打入执行包

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.2</version>
            <!-- 阻止第三方jar包被打入执行包 -->
            <scope>provided</scope>
        </dependency>

3、打包时不包含该包下的部分子包

       <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.3.2</version>
            <!-- 不包含org.apache.httpcomponents包 -->
            <exclusions>
                <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

读到这里,这篇“maven打包时候修改包名称带上git版本号和打包时间的方法是什么”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI