我在构建 Maven 项目时遇到问题。我有一个生产要求确定性的jar 文件,这些文件在不同的构建和版本之间必须是二进制一致的,以防这些构建之间没有源代码更改。为此,我使用了本文 http://gary-rowe.com/agilestack/2013/08/08/how-to-create-a-deterministic-jar/以获得指导。
我已经成功地构建了我的罐子,它们符合我的要求。这是我的配置:
<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,因为它不符合我的要求。
所以,我已经禁用了maven-jar-plugin
执行,通过显式地将其设置为在无效阶段运行,如下所示:(从其他帖子在这里 https://stackoverflow.com/questions/4101750/disable-the-execution-default-jar)
<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
这样我就可以手动附加从汇编器插件生成的任何工件:
<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>
这会导致我无法解决的错误:
[错误] 无法在项目 my-project 上执行目标 org.codehaus.mojo:build-helper-maven-plugin:1.9.1:attach-artifact (attach-instrumented-jar):执行目标组织的 Attach-instrumented-jar .codehaus.mojo:build-helper-maven-plugin:1.9.1:attach-artifact失败:对于工件{full-name-of-my-project.jar}:附加工件必须具有与其对应的主工件不同的 ID。-> [帮助1]
有办法解决这个问题吗?我已经检查过解决方案,并且大多数建议使用分类器,但我想安装主要工件,就像maven-jar-plugin
。我们正在开发的其他软件将需要标准的 jar 依赖项,我们希望避免通过不合理地引入分类器来使我们的设置复杂化。