蚂蚁如何编译和 jar 字节相同的 jar 文件,即 MD5 匹配,除非 .java(因此 .class)改变? [英] How can ant compile-and-jar byte-identical jar files, i.e. so MD5 matches unless .java (and thus .class) changes?

查看:27
本文介绍了蚂蚁如何编译和 jar 字节相同的 jar 文件,即 MD5 匹配,除非 .java(因此 .class)改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结

如何让 ant 从同一个 .class 文件中重复生成字节相同的 jar 文件?

How can you make ant repeatedly generate byte-identical jar files from the same .class files?

背景

我们的构建过程执行以下操作:

Our build process does the following:

  1. 从另一个应用程序的源代码库中获取 web-services-definition (wsdl) 文件
  2. 运行 wsdl2java 以生成 .java 文件以供网络服务客户端(即我们的应用程序)使用
  3. 编译java文件
  4. 从编译器输出生成一个 .jar 文件
  5. 将artifact"jar 文件检查到源代码管理中

注意:我们执行最后一步是为了让开发人员无需自行构建即可访问​​此 jar 文件.我们使用一个特殊的派生"目录来区分源和工件.

Note: We do this last step so developers have access to this jar file w/o building it themselves. We use a special 'derived' directory to distinguish source from artifacts.

问题

我们无法让 ant 生成字节相同的 .jar 文件,即使源文件没有改变,即每个构建生成一个略有不同的 jar(具有不同的 MD5)

We cannot get ant to generate byte-identical .jar files, even if the source files have not changed, i.e. each build generates a slightly different jar (with different MD5)

我查了互联网,发现这个问题是大约 5 年前的:

I checked the internet and found this question from some 5 years back:

如果我编译一些代码并使用 ANT 创建一个 jar 和相关的 md5 文件md5 文件中的校验和每次都不同,即使代码没有改变.任何想法是为什么这是如此如何规避?我怀疑某处有一些时间戳信息.

If I compile some code and create a jar and related md5 file using ANT the checksum in the md5 file is different everytime even though the code hasn't changed. Any idea's why this is so how it can circumvented ? I suspect there is some timestamp information coming in somewhere.

http://www.velocityreviews.com/forums/t150783-creating-new-jar-same-code-different-md5.html

根据回复,我尝试了以下操作:

Per the responses, I've attempted the following:

  1. 在震动前将所有 .class 文件的时间戳设置为0"
  2. 指定一个清单文件,并将此清单的时间戳设置为 0

[注意:这第二步似乎无效.见下文]

[Note: this second step seems ineffective. See below]

每次构建后,.jar 文件 仍然 具有不同的 MD5 总和.

After each build, the .jar file still has a different MD5 sum.

CSI:Jar 文件

我已经解压并检查了 jar 的内容和时间戳在不同"的 jar 之间匹配,但有一个例外:META-INF/MANIFEST.MF 的时间戳不同.

I've unjarred and examined and the jars both contents and timestamps match between the "different" jars with one exception: different timestamps for META-INF/MANIFEST.MF.

代码

   <-- touch classes and manifest to set consistent timestamp across builds -->
   <touch millis="0">
    <fileset dir="${mycompany.ws.classes.dir}"/>
   </touch>
   <touch millis="0" file="mymanifest.mf"/>

   <jar destfile="${derived.lib.dir}/mycompanyws.jar"
        manifest="mymanifest.mf"
        basedir="${mycompany.ws.classes.dir}"
        includes="**/com/mycompany/**,**/org/apache/xml/**" 
    />

其他选项

如果 .java 文件发生变化,我们可以使用更高级的 ant 编程来仅检查 .jar 文件.

We could use fancier ant programming to only check in the .jar file if the .java files have changed.

推荐答案

由于 jar 只是一个隐身的 zip 文件,您可以尝试使用 zip 任务在 下添加清单文件META-INF/ 手动.希望这可以避免与 jar 任务处理清单相关的任何内部魔法.

Since a jar is just a zip file incognito, you could try using the zip task to add the manifest file under META-INF/ by hand. Hopefully that circumvents any internal magic associated with handling the manifest by the jar task.

只是一个旁注,因为听起来具有相同的 MD5s 是至关重要的,我建议您添加一个健全性测试作为构建的一部分,例如编译一些永远不会更改为 jar 的特殊虚拟"代码并检查jar MD5 等于预期的那个.这将保护构建免受意外更改(例如在升级到 ant、JRE、操作系统、时区更改等之后)

Just an side note, since it sounds like having equal MD5s is critical, I would recommend you add a sanity test as part of the build, such as compile some special "dummy" code that never changes into a jar and check the jar MD5 equals the one expected. This will safeguard the build against unexpected changes (e.g. after an upgrade to ant, JRE, OS, timezone change etc.)

这篇关于蚂蚁如何编译和 jar 字节相同的 jar 文件,即 MD5 匹配,除非 .java(因此 .class)改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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