Maven - 在Java中添加可访问的校验和 [英] Maven - add checksum accessible inside Java

查看:145
本文介绍了Maven - 在Java中添加可访问的校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上次我在maven中成功添加了一个不错的 checksum-maven-plugin 。我为我的一个文件生成校验和。我正在使用这个特殊配置:

Last time I successfully added a nice checksum-maven-plugin in maven. I generate the checksum for one of my file. I am using for that this special configuration:

<execution>
      <phase>package</phase>
  <goals>
    <goal>files</goal>
  </goals>
</execution>
    ...
<fileSets>
<fileSet>
  <directory>${basedir}/myfiles</directory>
  <includes>
    <include>file-name</include>
  </includes>
</fileSet>
</fileSets>

它完美无缺并生成 file-name.md5 带校验和的文件 / target

It works perfect and generates a file-name.md5 file with checksum inside /target.

I用这个执行用 maven-assembly-plugin 构建我的JAR。

I build my JAR with maven-assembly-plugin with this execution.

<execution>
  <phase>package</phase>
  <goals>
    <goal>single</goal>
  </goals>
</execution>






在Java中我已经可以访问我的 src / main / resources / my.properties 属性文件包含:

InputStream inputStream = RunJetty.class.getClassLoader()
        .getResourceAsStream("my.properties");

(这样可行)。

我需要将 checksum-maven-plugin 生成的校验和传递给Java代码。怎么做?

I need the checksum generated by checksum-maven-plugin pass to Java code. How to do that?

经过一些评论我看到两个选项:

After some comments I see two options:


  • 一种是将校验和文件添加到JAR。

  • 2'是将校验和值添加到 my.properties 文件(或创建单独的属性文件)。

  • One is to add the checksum file to JAR.
  • 2'nd is to add the checksum value to my.properties file (or create separate properties file).

推荐答案

也许,JAR清单是一个适当的存储位置哈希,当然,只要你不想存储罐子本身的哈希值。如果哈希用于超过一致性检查,例如许可,可以使用盐和加密来提高安全性。

Perhaps, the JAR manifest is an appropriate location for storing hashes, as long as you don't want to store hashes of the jar itself, of course. If hashes are used for more than consistency checks, e.g. licensing, one could use salts and encryption to increase security.


  • maven-jar-plugin 能够


    • 签署jar

    • 自定义清单

    • 包自定义文件



      我想,这就是这种方式要去,因为maven文物应该保持这种方式一致,而后来改变罐子会让你陷入困境。请查看此处,了解如何添加自定义文件,例如你的财产文件。但是,请注意在此插件之前配置 checksum-maven-plugin ,或者,您也可以通过将其映射到 prepare来使其执行前进-package 阶段。



    • The maven-jar-plugin is able to
      • sign the jar
      • customize the manifest
      • package custom files

        I think, this is the way to go, as the maven artefacts ought to remain consistent this way, whereas altering the jars afterwards could get you into trouble. Look here for how to add custom files, e.g. your property file. However, take care to configure the checksum-maven-plugin before this plugin, alternatively, you could also bring its execution forward by mapping it to the prepare-package phase.

      关于如何阅读清单,请看这里:阅读我自己的Jar的清单

      For how to read manifests, look here: Reading my own Jar's Manifest

      为了保持Java类之间的一致性,也可以'密封'一个罐子。

      For consistency among Java classes, one could also 'seal' a jar.

      oracle清单教程: http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

      The oracle manifest tutorial: http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

      checksum-maven-plugin 确实无法将哈希作为一个属性发出。

      The checksum-maven-plugin is indeed not capable to emit the hashes as a property.

      我现在可以想到的解决方案是:

      Solutions I can currently think of are:


      1. 将jar中该哈希列表文件的相对路径添加到清单中。到目前为止,我不认为将哈希与清单分开是很糟糕的,特别是如果哈希列表很大。

      2. 添加另一个插件执行来读取用cheksum插件生成的文件;请参阅: https://stackoverflow.com/a/9339405/1175253 。但仅此一点可能是不够的,因为如果没有进一步的工作,就不能迭代地添加哈希值。最终可能会将散列文件内容转储到清单条目或编写自定义插件。如果我要开发这样的插件,我将从 mojo-executor-maven-开始插件,允许集成现有插件。

      1. Add the relative path of that hash list file within the jar to the manifest. I don't think it's that bad to separate the hashes from the manifest as you've done it so far, especially if the hash list is large.
      2. Add another plugin execution to read the file generated with the cheksum plugin; See: https://stackoverflow.com/a/9339405/1175253. But this alone is probably insufficient, as one cannot add the hashes iteratively without further work. One would possibly end up with dumping the hash file contents into a manifest entry or writing a custom plugin. If I'd be up to develop such a plugin, I'd start with the mojo-executor-maven-plugin, which allows integration of existing plugins.

      这篇关于Maven - 在Java中添加可访问的校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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