构建maven项目时遇到问题。我需要生成确定性的 jar文件,这些文件必须在不同的版本和版本中二进制一致,以防这些版本之间没有源代码更改。为此,我使用this article作为指导。如果使用maven-assembly-plugin构建,手动附加主构件
我成功地设计了我的罐子,并且它们符合我的要求。下面是我的配置:
<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,然后将拉链复制超预期的罐子神器。
上面的问题是,maven仍然执行maven-jar-plugin
,所以手动组装的罐子被maven-jar-plugin
中的一个覆盖。我不想使用这个罐子,因为它不符合我的要求。
所以,我已经禁用了maven-jar-plugin
执行,通过明确地将其设定为一个无效的阶段运行,如下图所示:(看到来自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>
似乎一切都很好,直到我发现我的主要罐子工件永远不会安装在.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>
这就导致了一个错误,我无法解决:
[错误]无法执行目标org.codehaus.mojo:build-helper-maven-plugin:1.9.1:在项目my-project上执行attach-artifact(attach-instrumented-jar):执行目标为org.codehaus的attach-instrumented-jar。 mojo:build-helper-maven-plugin:1.9.1:attach-artifact失败:对于工件{full-name-of-my-project.jar}:附加的工件必须具有与其相应的主工件不同的ID。 - > [Help 1]
有没有办法解决这个问题?我已经检查过解决方案,并且大多数人推荐使用分类器,但是我想安装主要的神器,就像maven-jar-plugin
一样。我们正在开发的其他软件将需要标准的jar依赖,并且我们希望通过不合理地引入分类器来避免复杂化我们的设置。