如果由maven-assembly-plugin构建,则手动附加主工件 [英] Manually attach main artifact if built by maven-assembly-plugin

查看:203
本文介绍了如果由maven-assembly-plugin构建,则手动附加主工件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在构建maven项目时遇到了问题。我需要生成 deterministic jar文件,如果这些版本之间没有源代码更改,则必须在不同版本和版本之间保持二进制一致。为此,我使用了这篇文章作为指导。

I am having problems building a maven project. I have a requirement to produce deterministic jar files, which must be binary-consistent across different builds and versions, in case there are no source code changes in between these builds. For the purpose, I have used this article for guidance.

我已成功设法制作我的罐子,它们符合我的要求。这是我的配置:

I have successfully managed to build my jars and they are consistent up to my requirements. Here is my configuration:

  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <id>step-1-remove-timestamp</id>
        <phase>prepare-package</phase>
        <configuration>
          <target>
            <touch datetime="01/01/2015 00:10:00 am">
              <fileset dir="target/classes"/>
              <fileset dir="src"/>
            </touch>
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
      <execution>
        <id>step-3-rename-assembly</id>
        <phase>package</phase>
        <configuration>
          <target>
            <copy file="${project.build.directory}/${project.build.finalName}-deterministic.zip"
                  tofile="${project.build.directory}/${project.build.finalName}.jar"/>
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2.1</version>
    <configuration>
      <descriptors>
        <descriptor>src/main/assembly/zip.xml</descriptor>
      </descriptors>
    </configuration>
    <executions>
      <execution>
        <id>step-2-make-assembly</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

在上面的代码中我构建并打包jar作为zip,然后将zip复制到预期的jar神器。

上面的问题是maven仍然执行 maven-jar-plugin ,所以手动组装的jar被覆盖了其中一个 maven-jar-plugin 。我不想使用这个jar,因为它与我的要求不一致。

In the above code I build and package the jar as a zip, then copy the zip over the expected jar artifact.
The problem with the above, is that maven still executes the maven-jar-plugin, so the manually assembled jar is overwritten by the one of the maven-jar-plugin. I do not want to use this jar, since it is not consistent with my requirements.

所以,我已经禁用了 maven-jar-plugin 执行,通过明确设置为无效阶段运行,如下所示:(从此处的其他帖子

So, I have disabled the maven-jar-plugin execution, by explicitly setting it to run for an invalid phase, like below: (saw that from other posts here)

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.5</version>
    <executions>
      <execution>
        <id>default-jar</id>
        <phase>never</phase>
        <configuration>
          <finalName>unwanted</finalName>
          <classifier>unwanted</classifier>
        </configuration>
      </execution>
    </executions>
  </plugin>

一切似乎都没问题,直到我发现我的主要jar工件从未安装在 .m2 目录。为了解决这个问题,我还添加了 maven-helper-plugin ,以便我手动附加我从汇编程序插件生成的任何工件:

Everything seems fine, until I discovered my main jar artifact is never installed in the .m2 directory. To correct this, I also added the maven-helper-plugin so that I manually attach any artifacts I produce from the assembler plugin:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
      <execution>
        <id>attach-instrumented-jar</id>
        <phase>verify</phase>
        <goals>
          <goal>attach-artifact</goal>
        </goals>
        <configuration>
          <artifacts>
            <artifact>
              <file>${project.build.directory}/${project.build.finalName}.jar</file>
              <type>jar</type>
            </artifact>
          </artifacts>
        </configuration>
      </execution>
    </executions>
  </plugin>

这导致我无法解决的错误:

This leads to an error I am unable to solve:


[错误]无法执行目标org.codehaus.mojo:build-helper-maven-plugin:1.9.1:attach-artifact(attach-instrumented-jar)on project my -project:执行attach-instrumented-jar of goal org.codehaus.mojo:build-helper-maven-plugin:1.9.1:attach-artifact failed:对于artifact {full-name-of-my-project.jar}: 附加工件的ID必须与其对应的主工件不同。 - > [帮助1]

[ERROR] Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.9.1:attach-artifact (attach-instrumented-jar) on project my-project: Execution attach-instrumented-jar of goal org.codehaus.mojo:build-helper-maven-plugin:1.9.1:attach-artifact failed: For artifact {full-name-of-my-project.jar}: An attached artifact must have a different ID than its corresponding main artifact. -> [Help 1]

有没有办法克服这个问题?我已经检查了解决方案,并且大多数建议使用分类器,但我想安装主工件,就像 maven-jar-plugin 一样。我们正在开发的其他软件将需要标准的jar依赖,我们希望通过不合理地引入分类器来避免使我们的设置复杂化。

Is there a way to overcome this issue? I've checked for solutions, and most recommend using classifiers, but I want to install the main artifact as would the maven-jar-plugin. Other software we are developing will require the standard jar dependency, and we want to avoid complicating our setup by introducing classifiers unreasonably.

推荐答案

经过一些试验和失败后,我碰巧找到了一个有效的解决方案。
我在这里张贴它希望有用,或者有任何问题指向我,因为我不确定这是否是一种可靠的方法。

After some more trial and failures I happened to come with a working solution. I am posting it here with the hopes to either be useful, or to have any issues with pointed to me, as I am not really confident if this is a reliable approach.

所以,我收到的错误


附加工件的ID必须与其对应的主工件不同。

An attached artifact must have a different ID than its corresponding main artifact.

对我来说意味着我无法手动安装再次主要工件。由于 maven-jar-plugin 不再生成该工件,因此即使存在该文件,也永远不会安排安装(antrun复制任务会生成一个带有相同名称)。

meant to me that I cannot manually install "again" the main artifact. Since that artifact is not produced anymore by the maven-jar-plugin, it never gets scheduled for installation even if the file is present (the antrun copy task produces a jar with the same name).

令人惊讶的是,它需要一些小技巧才能再次使用:

Surprisingly, it needed a few little tricks to make this work again:


  1. 重新启用 maven-jar-plugin ,因为它应该是:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.5</version>
  <executions>
    <execution>
      <id>default-jar</id>
      <phase>package</phase>
    </execution>
  </executions>
</plugin>

这将在包上生成标准jar 阶段,最重要的是,让maven知道在安装阶段安装它。

This will produce the standard jar on the package phase, and most importantly, makes maven aware for it to be installed during the install phase.

使用deterministic zip将 maven-antrun-plugin 复制任务复制到覆盖已生成的jar。设置几乎与我的问题相同,所以我只是添加了差异:

Tweak the maven-antrun-plugin copy tasks to overwrite the already produced jar with the deterministic zip. The setup is nearly identical as in my question, so I am only adding the differences:

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.7</version>
  <executions>
    <execution>...</execution>
    <execution>
      <id>step-3-rename-assembly-and-sources</id>
      <phase>package</phase>
      <configuration>
        <target>
          <copy file="${project.build.directory}/${project.build.finalName}-deterministic.zip"
                tofile="${project.build.directory}/${project.build.finalName}.jar"
                overwrite="true"/>
        </target>
      </configuration>
    </execution>
      . . .
  </executions>
</plugin>

复制操作现在有 overwrite =true指定。最初,复制操作似乎忽略了目标中的文件(如果它们已经存在),发生的事情是 maven-jar-plugin 已经产生了默认的jar工件。发生了复制。设置此选项后, maven-antrun-plugin 现在覆盖具有确定性jar的前jar,后者成为maven install的主题阶段。

The copy operation now has overwrite="true" specified. Originally, the copy operation seemed to ignore files in the destination if they already exist, and what happened is that the maven-jar-plugin had already produced the default jar artifact when the copying occured. With this option set, the maven-antrun-plugin now overrides the former jar with the deterministic one, and the latter becomes a subject of the maven install phase.

从build-helper-maven-plugin中删除设置,以便不复制主jar工件第二次:

Removed the setup from the build-helper-maven-plugin, so that the main jar artifact is not copied a second time:



            <artifact>
              <file>${project.build.directory}/${project.build.finalName}.jar</file>
              <type>jar</type>
            </artifact>

就是这样,正确的jar安装在 .m2 目录中。

That's it, the correct jar is installed in the .m2 dir.

这篇关于如果由maven-assembly-plugin构建,则手动附加主工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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