Customize folder name using same descriptor maven assembly plugin for different format

4.4k views Asked by At

I am new to maven world and I am having some problems with maven assembly plugin. I want to use the same descriptor file to generate different format with custom name for "dir" format.

Following is my maven assembly plugin configuration :-

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>assembly</id>
                <goals>
                    <goal>single</goal>
                </goals>
                <phase>install</phase>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/descriptor.xml</descriptor>
                    </descriptors>
                    <finalName>service</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>target</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>

This is the descriptor file :-

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>assembly</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <baseDirectory>.</baseDirectory>
    <files>
        <file>
            <source>target/service.jar</source>
            <outputDirectory>.</outputDirectory>
            <filtered>false</filtered>
        </file>
        <file>
            <source>src/main/deploy/version.txt</source>
            <outputDirectory>.</outputDirectory>
            <filtered>true</filtered>
        </file>
        <file>
            <source>src/main/deploy/start.sh</source>
            <outputDirectory>.</outputDirectory>
            <filtered>true</filtered>
        </file>
        <file>
            <source>src/main/deploy/stop.sh</source>
            <outputDirectory>.</outputDirectory>
            <filtered>true</filtered>
        </file>
    </files>
</assembly>

I want to use the same descriptor file(basically same files to combine) into a service.zip and also create another folder in target named deploy if I add

<format>dir</format>

then it will create a folder named service under target and will have all the listed files. I want a way to customize the name of directory to "deploy". I tried checking with the documentation but could not find anything helpful unless I create another descriptor which is not the right way since my new descriptor will have same configuration as the previous one with just format as "dir".

Any help will be appreciated thanks in advance.

1

There are 1 answers

1
JimHawkins On BEST ANSWER

You can achieve your requirement by using different maven profiles. In

In your assembly descriptor, you use a property instead of a fixed string:

<formats>
    <format>${distributionFormat}</format>
</formats>

distributionFormat is defined in pom.xml. The name of the property is your choice.

In your pom.xml, you define distributionFormat in two different profiles. There you also define a name for the assembly (assemblyName in my example).

<project>
    ...
    ...
    <profiles>
         <profile>
              <id>distAsZip</id>
              <activeByDefault>true</activeByDefault>
              <assemblyName>service</assemblyName>
              <distributionFormat>zip</distributionFormat>
         </profile>
         <profile>
              <id>distAsDir</id>
              <activeByDefault>false</activeByDefault>
              <assemblyName>deploy</assemblyName>
              <distributionFormat>dir</distributionFormat>
         </profile>
    </profiles>
    ...
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>assembly</id>
                <goals>
                    <goal>single</goal>
                </goals>
                <phase>install</phase>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/descriptor.xml</descriptor>
                    </descriptors>
                    <finalName>${assemblyName}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>target</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
 ...
</project>

Now you can execute maven and define the profile that you want to use:
mvn clean package -PdistAsDir or mvn clean package -PdistAsZip