構建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依賴,並且我們希望通過不合理地引入分類器來避免複雜化我們的設置。