Maven - 在 Java 中添加可访问的校验和另一种方法 [英] Maven - add checksum accessible inside Java another approach

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

问题描述

仍然用 Maven 欺骗.我的目标是一样的:

Still trick with Maven. My target is the same:

应用程序提供:

  1. JAR 形式 maven-assembly-plugin
    • 由 WAR 中的 checksum-maven-plugin 生成的内部存储校验和文件
  1. JAR form maven-assembly-plugin
    • internally stored checksum file generated by checksum-maven-plugin from WAR

经过一些改进后,我完成了 2 个模块项目.我看到两个选项:

After some refining I finished with 2 modules project. I see two options:

1. Option:
    parent
      - module 1 for WAR (+ checksum-maven-plugin)
      - module 2 for JAR (with dependancy)
2. Option:
    parent
      - module 1 for WAR
      - module 2 for JAR (with dependency to WAR + checksum-maven-plugin)

我选择了选项 1.我的父母:

I chose option 1. My parent:

modules -> deploy-webapp
        -> deploy
plugin -> compiler

我的模块 1:

set parent -> ...
plugin -> maven-war-plugin -> execution -> compile -> war
plugin -> checksum-maven-plugin -> execution -> package -> files

我的模块 2:

set parent -> ...
plugin -> maven-assembly-plugin -> execution -> package -> single

上面生成了我感兴趣的 3 个文件(工作正常):

The above generates 3 files which I am interested (works OK):

module 1 -> target/webapp.war
module 1 -> target/webapp.war.md5
module 2 -> target/deploy-jar-with-dependances.jar

问题:

我需要 JAR 文件中的校验和.如果创建的所有东西都是一些工件,那么这个校验和也是工件吗?如果是,maven-dependency-plugin 应该完成这项工作.但如果我要(在模块 2 内):

I need checksum inside JAR file. If everything which is created is some artifact, is this checksum artifact too? If yes the maven-dependency-plugin should do the job. But if I am going for (inside module 2):

maven-dependency-plugin -> copy-artifact:package:copy-> net.ju-n.maven.plugins:checksum-maven-plugin -> ${basedir}/src/main/resources

它复制插件的内容,而不是插件创建的工件...

It copies the content of the plugin, not artifact which is created by plugin...

推荐答案

我发现考虑在何处放置配置需要与考虑代码相同的决定.因此,对于给定的事物,使用该事物的代码应该处理该事物.

I find thinking about where to place configuration requires the same decisions as thinking about code. So, for a given thing, the code that uses that thing should handle that thing.

换句话说,您应该选择选项 2 - WAR 不关心校验和,那么为什么要生成它?

In other words, you should go with Option 2 - the WAR does not care about the checksum, so why should it generate it?

相反,您应该在 JAR 项目中计算校验和.该插件有一个很好的files 目标.这是一个应该适合您的最小示例.

Instead, you should calculate the checksum in the JAR project. The plugin has a nice files goal for that. Here's a minimal example that should work for you.

          <plugin>
                <groupId>net.ju-n.maven.plugins</groupId>
                <artifactId>checksum-maven-plugin</artifactId>
                <version>1.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>files</goal>
                        </goals>
                        <phase>prepare-package</phase>
                    </execution>
                </executions>
                <configuration>
                    <fileSets>
                        <fileSet>
                            <directory>PATH_TO_WAR</directory>
                            <includes>
                                <include>test.txt</include>
                            </includes>
                        </fileSet>
                    </fileSets>
                    <individualFilesOutputDirectory>PATH_TO_WAR</individualFilesOutputDirectory>
                </configuration>
            </plugin>

请注意,此插件的执行应该在您将 WAR 复制到 JAR 项目后(以便该文件存在),特别是:

Note that this plugin execution should happen after you copy the WAR into your JAR project (so that the file exists), specifically:

  • 如果它们处于不同的阶段,应该发生在后期,
  • 如果它们处于同一阶段,它应该位于POM中的WAR复制执行定义之后,因为Maven运行插件目标执行,它们是绑定的到同一个阶段,按照它在 POM 中遇到它们的顺序.
  • if they're in different phases, it should happen in a later phase,
  • if they're in the same phase, it should be located after the WAR copy execution definition in the POM, since Maven runs plugin goal executions, that are bound to the same phase, in the sequence it encounters them in the POM.

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

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