【发布时间】:2013-09-23 22:20:46
【问题描述】:
我有一个 Java 项目,当在其上执行 mvn install 时,会生成一个 JAR 文件。到目前为止一切顺利。
在那一代之后,我想将生成的 JAR 文件重命名为具有任意扩展名的文件。例如,假设生成的 JAR 名为“Foo.jar”,我想将其重命名为“Foo.baz”
我正在尝试确定可以让我执行此操作的神奇插件组合。到目前为止,我已经根据this SO answer尝试了以下方法:
<project>
...
<build>
<finalName>Foo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${project.build.finalName}.jar"
tofile="${project.build.directory}/${project.build.finalName}.baz" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<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}.baz</file>
<type>baz</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
所以,有两件事。首先,上述插件似乎并没有真正做任何事情。另外,我不清楚第二个插件试图完成什么。
我也尝试在汇编中与上述插件分开执行此操作,但效果并不好。我的程序集文件(注意我没有包含插件,因为它完全是样板文件):
<assembly xmlns="http://maven.apache.org/xsd/assembly"
xsi:schemaLocation="http://maven.apache.org/xsd/assembly-1.0.0.xsd">
<id>dist</id>
<formats>
<format>jar</format>
<format>dir</format>
</formats>
<files>
<file>
<source>${project.build.directory}/${project.build.finalName}.${packaging}</source>
<outputDirectory>${project.build.directory}</outputDirectory>
<destName>${project.build.finalName}.baz</destName>
</file>
</files>
</assembly>
但是,尝试运行此程序集导致:无法创建程序集:创建程序集存档 dist 时出错:zip 文件无法包含自身
所以,在这一点上,我不知所措。在谷歌搜索了大约一个小时后,我的印象是 ant 脚本是解决这个问题的方法,但我无法开始告诉你为什么它不起作用。
感谢任何帮助。
【问题讨论】:
-
它是否必须在 maven 构建周期中,或者您可以只制作一个 bat/sh 文件来调用安装然后重命名 jar 文件吗?
-
为什么需要对工件进行这样的重命名?
-
@ns47731 出于可移植性的原因,我需要它作为构建周期的一部分运行。
标签: java maven maven-assembly-plugin maven-jar-plugin maven-antrun-plugin