本文介绍了Maven:输出具有任意扩展名的JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java项目,当对它执行mvn install时,会生成一个JAR文件.到目前为止一切顺利.

I have a Java project that, when mvn install is executed on it, generates a JAR file. So far so good.

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

After that generation, I'd like to rename the resultant JAR file to something with an arbitrary extension. For example, say the resultant JAR that is generated is called "Foo.jar", I'd like to rename it to "Foo.baz"

我正在尝试确定使我能够执行此操作的神奇插件组合.到目前为止,我已经根据 SO答案尝试了以下操作:

I'm trying to ascertain the magical plugin combo that will allow me to do this. So far, I've tried the following, based on 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>

所以,有两件事.首先,上面的插件似乎并没有做任何事情.另外,我不清楚第二个插件要完成什么.

So, two things. First off, the above plugins don't seem to actually do anything. Also, I'm not clear on what the second plugin is trying to accomplish.

我还尝试过与上面的插件分开在组装中执行此操作,效果并不理想.我的程序集文件(请注意,我没有包含该插件,因为它完全是样板):

I've also attempted to do this in assembly separately from the above plugins, which didn't go so well. My assembly file (Note that I didn't include the plugin, because it's completely boilerplate):

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

Attempting to run this assembly, however, resulted in: Failed to create assembly: Error creating assembly archive dist: A zip file cannot include itself

因此,在这一点上,我很茫然.经过大约一个小时的谷歌搜索,我给人的印象是,蚂蚁脚本是解决这个问题的方法,但是我无法开始告诉您为什么它不起作用.

So, at this point, I'm at a loss. After about an hour of Googling, I'm under the impression that the ant script is the way to go for this, but I couldn't begin to tell you why it isn't working.

感谢您的帮助.

推荐答案

Heri技巧介绍了copy-maven-plugin技巧,但是,由于尚不十分清楚如何进行设置(即使在阅读了文档之后),该技巧也不是很明显. ),我将在此处发布实际答案.

Hat tip to Heri for the copy-maven-plugin tip, but, since it isn't immediately obvious how to set this up (even after reading the documentation), I'll post the actual answer here.

我通过深入研究GitHub问题跟踪器并与之抗衡找到了这个示例.

I found this example by digging through the GitHub issue tracker, and generally fighting with it.

请注意,由于一些SO问题中记录了Sonatype以太更改,因此该代码在Maven 3.1.0上不起作用.该插件的作者声称(截至13年8月),下一版本(显然是3.0版)将支持Maven 3.1.0

Note that this code doesn't work on Maven 3.1.0 because of the Sonatype aether change that is documented in several SO questions. The author of this plugin claims (as of August '13) that the next release (version 3.0 apparently) will support Maven 3.1.0

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

Anyway, the plugin should be configured as follows:

        <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(或者即使您没有指定finalName,我也没有测试过),这对于任何项目都是通用的.

The beautiful simplicity of the above code is that, as long as you specify a finalName (or even if you don't? I haven't tested that), this should work generically for any project.

享受.

这篇关于Maven:输出具有任意扩展名的JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:14