18

I have a project which I need to rename the final output file generated by the Maven Assembly Plugin after everything else finishes (in the compiling/building/assembly process).

The Maven Assembly Plugin is generating a final .zip file, based on the project's name, and I need to rename this completely to final-version.oxt. I'm trying to use the maven-antrun-plugin to rename it, as pointed by other similar questions here, but no luck (I've never used Maven or Ant before, so maybe I'm missing something).

This is the <build> section of the project's pom.xml. The rename part seems to be completely ignored, since no file is generated in my home folder.

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attached</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                        </archive>
                        <descriptors>
                            <descriptor>src/main/assembly/ooo-jar.xml</descriptor>
                            <descriptor>src/main/assembly/ooo.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>deploy</phase>

                    <configuration>
                        <tasks>
                            <copy file="${project.build.directory}/target/libreofficeplugin-ooo.zip"
                             tofile="/home/brunofinger/final-version.oxt" />
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>
Bruno Finger
  • 2,105
  • 3
  • 27
  • 47

3 Answers3

16

A few modifications made it work, probably the phase was wrong, but using <phase>install</phase> seems to make it work:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>install</phase>

                    <configuration>
                        <target>
                            <copy file="${project.build.directory}/libreofficeplugin-ooo.zip" tofile="${project.build.directory}/final-version.oxt" />
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
Bruno Finger
  • 2,105
  • 3
  • 27
  • 47
11

This question is nearly an year old, but in case anyone else is facing similar issue, here is an alternate solution.

If you are looking for a more mavenish way of doing it,you can use

 <plugin>
    <groupId>com.coderplus.maven.plugins</groupId>
    <artifactId>copy-rename-maven-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
      <execution>
        <id>rename-file</id>
        <phase>install</phase>
        <goals>
          <goal>rename</goal>
        </goals>
        <configuration>
          <sourceFile>${project.build.outputDirectory}/libreofficeplugin-ooo.zip</sourceFile>
          <destinationFile>${project.build.outputDirectory}/final-version.oxt</destinationFile>
        </configuration>
      </execution>
    </executions>
  </plugin>

and in case you want to copy instead of rename, use the copy goal instead of the rename goal.

coderplus
  • 5,793
  • 5
  • 34
  • 54
  • Cant seem to be able to skip the execution of this plugin with a profile. Do you know how to get this done? – BEvo Oct 20 '22 at 14:20
-2

You don't need to use antrun to rename the output file. Just use the tag finalName of the assembly plugin to rename the output file.

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-assembly-plugin</artifactId>
   <version>2.4</version>
   <executions>
      <execution>
         <id>assembly</id>
         <phase>package</phase>
         <goals>
            <goal>attached</goal>
         </goals>
         <configuration>
            <archive>
               <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
            </archive>
            <descriptors>
               <descriptor>src/main/assembly/ooo-jar.xml</descriptor>
               <descriptor>src/main/assembly/ooo.xml</descriptor>
            </descriptors>
            <finalName>final-version.oxt</finalName>
         </configuration>
      </execution>
   </executions>
</plugin>
Pablo R. Mier
  • 719
  • 1
  • 7
  • 13
  • 1
    It didn't work, it's still attaching the `.zip` extension to the end of the file, generating a file named `final-version.oxt.zip`. I need to get rid of the `.zip`. – Bruno Finger Dec 23 '13 at 10:44
  • 1
    You are right. The finalName renames only the name of the file, not the extension. – Pablo R. Mier Dec 23 '13 at 11:01
  • The `finalName` configuration element no longer exists in maven-assembly-plugin 3.0.0. – Barett Jan 10 '17 at 01:42
  • `finalName` still exists but has been moved out of the plugin section into the `build` section of the config. See [this SO answer](https://stackoverflow.com/a/14490656/286685). – Stijn de Witt Oct 04 '17 at 07:24
  • `finalName` renames all JARs, not just the assembled JAR. That's probably because it changes the actual POM value instead of keeping it isolated with assembly. Maybe that's why it was removed from documentation. – ingyhere Aug 17 '21 at 03:30
  • `finalName` renames all JARs, not just the assembled JAR. That's probably because it changes the actual POM value instead of keeping it isolated with assembly. Maybe that's why it was removed from documentation. – ingyhere Aug 17 '21 at 03:30