Combining Jar-with-dependencies and a ZIP file using the Maven assembly plugin

user3676476 picture user3676476 · Mar 8, 2015 · Viewed 7.9k times · Source

I want to combine my source code in a JAR with all dependencies as well as package this up into a zip file along with other files. I'm able to create the one Jar with all dependencies as well as the ZIP file but I can't combine the two.

I ultimately want the following directory structure in the zip file:

loader/bin/shellscript.sh

loader/lib/jar-with-dependencies.jar

loader/appname/config/config.xml

Here's an extract from my pom file:

<plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                       <mainClass>fully.qualified.MainClass</mainClass>
                    </manifest>
                 </archive>
                 <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                 </descriptorRefs>
                 <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                 </descriptors>
              </configuration>
        </plugin>

Here's an extract from my assembly

<assembly>
    <id>bin</id>
    <!-- Specifies that our binary distribution is a zip package -->
    <formats>
        <format>zip</format>
    </formats>

    <baseDirectory>SpreadsheetLoaderApp</baseDirectory>

    <fileSets>
        <fileSet>
            <directory>corporatebondpurchases</directory>
            <outputDirectory>${basedir}/corporatebondpurchases/config</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>corporatebondpurchases</directory>
            <outputDirectory>${basedir}/bin</outputDirectory>
            <includes>
                <include>*.sh</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

Here's the output from part of the build:

[INFO] --- maven-assembly-plugin:2.4:single (default-cli) @ SpreadsheetLoader ---
[INFO] Reading assembly descriptor: src/main/assembly/assembly.xml
[INFO] Building zip: C:\Software\SpringSTS\workspace\SpreadsheetLoader\target\SpreadsheetLoader-0.0.1-SNAPSHOT-bin.zip
[INFO] Building jar: C:\Software\SpringSTS\workspace\SpreadsheetLoader\target\SpreadsheetLoader-0.0.1-SNAPSHOT-jar-with-dependencies

This succeeds in creating one Jar under target as well as the ZIP file (albeit with full paths rather than relative paths). What I want is for the Jar file to be included as part of the zip file.

EDIT:

After researching various blog posts I've managed to get this working by using the following POM and assembly files

<build>
   <plugins>
       <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <executions>
           <execution>
               <id>jar-with-dependencies</id>
               <phase>package</phase>
               <goals>
                   <goal>single</goal>
               </goals>
               <configuration>
                   <descriptorRefs>
                       <descriptorRef>jar-with-dependencies</descriptorRef>
                   </descriptorRefs>
                   <archive>
                       <manifest>
                           <mainClass>fully.qualified.MainClass</mainClass>
                       </manifest>
                   </archive>
               </configuration>
           </execution>
           <execution>
               <id>dist</id>
               <phase>package</phase>
               <goals>
                   <goal>single</goal>
               </goals>
               <configuration>
                   <descriptors>
                       <descriptor>src/main/assembly/assembly.xml</descriptor>
                   </descriptors>
               </configuration>
           </execution>
       </executions>
   </plugin>
  </plugins>
</build>

And the corresponding assembly file...

<assembly>
    <id>dist</id>
    <!-- Specifies that our binary distribution is a zip package -->
    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>
    <baseDirectory>SpreadsheetLoader</baseDirectory>

    <files>
        <file>
            <source>target/${project.artifactId}-${project.version}-jar-with-dependencies.jar</source>
            <outputDirectory>lib</outputDirectory>
        </file>
    </files>

    <fileSets>
        <fileSet>
            <directory>applicationbuild/${buildname}</directory>
            <outputDirectory>${buildname}/config</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>applicationbuild/${buildname}</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>*.sh</include>
            </includes>
            <lineEnding>unix</lineEnding>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>./</directory>
            <outputDirectory>logs</outputDirectory>
            <excludes>
                <exclude>*/**</exclude>
            </excludes>
        </fileSet>
        <fileSet>
            <directory>./</directory>
            <outputDirectory>${buildname}/sourcedata</outputDirectory>
            <excludes>
                <exclude>*/**</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

This gives me a nice simple directory structure for deployment purposes. My main source of info came from the following site:

http://www.drawbackz.com/stack/175442/maven-assembly-plugin-how-to-create-nested-assemblies.html

Thanks

Answer

user3676476 picture user3676476 · Mar 9, 2015

I'm happy enough with the above result. However this is my first attempt at using Maven. If someone has a better way of doing this I'd gladly take the advice.