mavenantmaven-antrun-pluginmaven-ant-tasks

Unpack jar into a folder with same name as the file


I am using maven-antrun-plugin to unpack a jar file into a folder. This jar file is generated in every build and has a variant TIMESTAMP (as seen in the following snippet). How can I unpack the jar file into folder that has the same name as the jar file? E.g. Folder should be /sample_TIMESTAMP and not /folder

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>unpack-jar-features</id>
            <phase>install</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <echo message="unpack jar file" />
                    <unzip dest="/folder">
                        <fileset dir="/folder">
                            <include name="sample_TIMESTAMP.jar" />
                        </fileset>
                    </unzip>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

Solution

  • To unzip into a new directory, first create the directory with <mkdir> and then change the dest of <unzip> to sample_TIMESTAMP:

    <mkdir dir="/sample_TIMESTAMP"/>
    <unzip dest="/sample_TIMESTAMP">
        <fileset dir="/folder">
            <include name="sample_TIMESTAMP.jar" />
        </fileset>
    </unzip>
    

    You can use <pathconvert> to create a property with the name of the JAR file:

    <fileset id="my-jar-file" dir="/folder">
        <include name="sample_*.jar"/>
    </fileset>
    <pathconvert property="my-jar-file-name">
        <chainedmapper>
            <flattenmapper/>
            <globmapper from="*.jar" to="*"/>
        </chainedmapper>
        <path>
            <fileset refid="my-jar-file"/>
        </path>
    </pathconvert>
    <mkdir dir="/${my-jar-file-name}"/>
    <unzip dest="/${my-jar-file-name}">
        <fileset refid="my-jar-file"/>
    </unzip>
    

    If the my-jar-file <fileset> could match multiple JAR files, use <restrict> to limit the matching to a single file.