在组装之后但在安装之前进行后处理jar(以获得幂等的构建) [英] Post-process jar after assembly but before installation (to get idempotent builds)

查看:50
本文介绍了在组装之后但在安装之前进行后处理jar(以获得幂等的构建)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Jenkins,它使用md5指纹识别工件,以及工件自上次构建以来是否已更改.不幸的是,Maven构建始终会生成二进制不同的工件.

We use Jenkins which use md5 fingerprinting to identify artifacts and whether the artifact has changed since the last build. Unfortunately Maven builds always generate binary different artifacts.

因此,我正在考虑使Maven为相同的输入文件集生成相同的jar工件,而不管它们在何处以及何时构建,这尤其意味着必须对jar文件中的条目进行排序-不仅在索引,但按照将它们写入jar文件的顺序.

Therefore I am looking into making Maven generate the same jar artifact for the same set of input files regardless of where and when they were built, which amongst other things mean that the entries in the jar file must be sorted - not only in the index, but in the order they are written to the jar file.

检查使用maven-assembly-plugin的maven-jar-plugin后,我的结论是,在一次写入所有文件之前,它们不会收集要写入内存的所有文件,而是一次写入一个文件.这意味着最好对生成的jar进行后处理,而不是更改当前行为,以便我那时可以对条目进行排序,将时间戳记清零,等等.

After examining maven-jar-plugin which use maven-assembly-plugin, my conclusions are that they do not collect all files to be written in memory before writing them all at once, but write one at a time. This mean that it may be better to postprocess the generated jar instead of changing the current behavior so I at that time can sort the entries, zero the timestamps, etc.

我不熟悉编写Maven插件,所以我的问题是,我应该如何编写一个Maven知道如何分辨工件-jar-in-progress所在位置以及如何将其连接到pom.xml中的插件. ?

I am unfamiliar with writing Maven plugins, so my question is, how should I write a plugin which Maven knows how to tell where the artifact-jar-in-progress is located and how I hook it up in my pom.xml?

(起初,我需要使用它来处理jar文件,但是war文件也可以).

(At first I need this to work for jar files, but war files would be nice too).

推荐答案

如上所述,可以基于类似于maven-shade-plugin的方法来完成此操作.我继续编写了一个简单的插件来添加此功能-请参见 https://github.com/manouti/jar-timestamp-normalize-maven-plugin (在

As mentioned, this can be done based on something similar to maven-shade-plugin. I went ahead and wrote a simple plugin to add this capability -- see https://github.com/manouti/jar-timestamp-normalize-maven-plugin (available on the Central repo).

该行为基于shade插件.它由一个称为normalize的目标组成,该目标可以绑定到package生命周期阶段,并可以在项目的POM中进行配置:

The behavior is based on the shade plugin. It consists of a single goal called normalize which can be bound to the package lifecycle phase and configured in the project's POM:

<plugins>
    <plugin>
        <groupId>com.github.manouti</groupId>
        <artifactId>jar-timestamp-normalize-maven-plugin</artifactId>
        <version>1.0-SNAPSHOT</version>
        <executions>
            <execution>
                <id>jar-normalize</id>
                <goals>
                    <goal>normalize</goal>
                </goals>
                <phase>package</phase>
            </execution>
        </executions>
    </plugin>
</plugins>

关于插件的一些注意事项:

A few notes about the plugin:

  1. 正在构建的工件可以通过project#getArtifact()访问,其中projectorg.apache.maven.project.MavenProject.

规范化主要包括三个步骤:

Normalization consists of mainly three steps:

  • 将所有Jar条目的最后修改时间设置为特定的时间戳(默认值为1970-01-01 00:00:00AM,但可以通过-Dtimestamp系统属性进行更改).

  • Setting the last modified time of all Jar entries to a specific timestamp (default value is 1970-01-01 00:00:00AM but can be changed via -Dtimestamp system property).

清单中的属性(按字母顺序)重新排序,但始终排在首位的Manifest-Version除外.

Reordering (alphabetically) of attributes in the manifest except for Manifest-Version which always comes first.

pom.properties文件中删除包含时间戳记的注释,该时间戳记会导致Jar在不同版本之间发生差异.

Removing comments from the pom.properties file which contain a timestamp that causes the Jar to differ from one build to another.

调用后,目标将在原始工件(名为artifactId-version-normalized.jar)旁边(即在project.build.directory目录中)生成输出文件.

Once invoked, the goal will generate the output file next to the original artifact (named artifactId-version-normalized.jar), i.e. in the project.build.directory directory.

这篇关于在组装之后但在安装之前进行后处理jar(以获得幂等的构建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆