Maven:输出具有任意扩展名的JAR

时间:2013-09-23 22:20:47

标签: java maven maven-assembly-plugin maven-jar-plugin maven-antrun-plugin

我有一个Java项目,当在其上执行mvn install时,会生成一个JAR文件。到目前为止一切都很好。

在那一代之后,我想将生成的JAR文件重命名为具有任意扩展名的文件。例如,假设生成的结果JAR称为“Foo.jar”,我想将其重命名为“Foo.baz”

我正在尝试确定允许我这样做的神奇插件组合。到目前为止,我已经尝试了以下内容,基于this SO回答:

<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文件不能包含自身

所以,在这一点上,我很茫然。经过大约一个小时的谷歌搜索,我的印象是蚂蚁脚本是这样做的方式,但我无法开始告诉你它为什么不起作用。

感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

Cody S.回答对我来说非常好。但是如果你不想最终得到两个文件(* .jar和* .baz),就像我的情况一样,试试下面的maven插件,它会重命名&#34;,而不是&#34;副本&#34;。

<plugin>
    <groupId>com.coderplus.maven.plugins</groupId>
    <artifactId>copy-rename-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
        <id>rename-file</id>
        <phase>generate-sources</phase>
        <goals>
            <goal>rename</goal>
        </goals>
            <configuration>
                <sourceFile>${project.build.directory}/${project.build.finalName}.jar</sourceFile>
                <destinationFile>${project.build.finalName}.baz</destinationFile>
            </configuration>
        </execution>
    </executions>
</plugin>

其他使用示例(复制和/或重命名多个文件/目录)可在其网站上找到: http://coderplus.github.io/copy-rename-maven-plugin/usage.html

答案 1 :(得分:1)

您可以使用copy-maven-plugin将生成的文件复制或移动到新名称。

答案 2 :(得分:1)

给Heri提供了复制maven-plugin技巧的提示,但是,由于现在并不是很明显如何进行设置(即使阅读完文档后),我也会在这里发布实际答案。

我通过挖掘GitHub问题跟踪器找到了这个例子,并且经常与之抗争。

请注意,此代码在Maven 3.1.0上不起作用,因为在几个SO问题中记录了Sonatype以太变化。这个插件的作者声称(截至2013年8月)下一个版本(显然3.0版)将支持Maven 3.1.0

无论如何,插件应配置如下:

        <plugin>
            <groupId>com.github.goldin</groupId>
            <artifactId>copy-maven-plugin</artifactId>
            <version>0.2.5</version>
            <executions>
                <execution>
                    <id>create-archive</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <targetPath>${project.build.directory}</targetPath>
                                <file>${project.build.directory}/${project.build.finalName}.jar</file>
                                <destFileName>${project.build.finalName}.baz</destFileName>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

上述代码的简洁之处在于,只要你指定一个finalName(或者即使你没有指定它?我还没有测试过),这应该适用于任何项目。

享受。

相关问题