Maven: Output JAR with arbitrary extension

半世苍凉 提交于 2019-12-11 04:33:19

问题


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

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"

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>

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.

Any help is appreciated.


回答1:


Cody S. answer worked very well for me. But if you don't want to end up with two files (*.jar and *.baz), as in my case, try the following maven plug-in, which does a "rename", instead of a "copy".

<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>

Other examples of use (copying and/or renaming multiple files/directories) are available at their website: http://coderplus.github.io/copy-rename-maven-plugin/usage.html




回答2:


You can use the copy-maven-plugin to copy or move your generated file to a new name.




回答3:


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.

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

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>

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.

Enjoy.



来源:https://stackoverflow.com/questions/18969880/maven-output-jar-with-arbitrary-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!