Search code examples
mavenmaven-assembly-plugin

How to create custom zip file including dependencies using Maven


I'm new to Maven and need to create a "custom" zip which basically contains:

  • A jar with my classes
  • Some (but not all) the dependencies I use to build: these are declared within Maven (e.g. these are not local copies manages within the project)

The desired ZIP file will have this structure:

  • META-INF/
    • some-custom- descriptor.xml
  • lib/
    • myLib.jar
    • dependency1.jar
    • dependency2.jar

So far I understand that the assembly plugin is the tool of trade but I don't get how to tell to perform the two steps:

  • create the jar for the binaries
  • add this jar together with some specific dependencies

Solution

  • I'm not sure if i unterstood everything. But i think you must first copy the dependencies(there are not inside the jar after)

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
               <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.demo.App</mainClass>
                    <classpathPrefix>lib/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeScope>runtime</includeScope>
                        <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                        <excludeScope>test</excludeScope>
                    </configuration>
                </execution>
         </executions>
    </plugin>
    

    After that you can use the assembly plugin with a descriptor xml file like this:

    <assembly>
        <id>zip</id>
        <formats>
            <format>zip</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <fileSet>
                <directory>${project.build.directory}/lib</directory>
                <outputDirectory>lib</outputDirectory>
            </fileSet>
            <fileSet>
                <directory>${project.build.directory}</directory>
                <outputDirectory></outputDirectory>
                <includes>
                    <include>*.jar</include>
                </includes>
            </fileSet>
        </fileSets>
    </assembly>