Search code examples
javamavenmaven-assembly-plugin

Build java project jar and then zip files and jar using Maven


I am trying to generate a jar for a Java project and then zip the jar file and the lib directory using the maven-assembly-plugin but it does not include the project jar in the zip file. Whatever I try out has some issue or the other. Does anyone have a similar example?


Solution

  • try this

    pom.xml

    ...
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                            </manifest>
                        </archive>
                        <descriptors>
                            <descriptor>src/assembly/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </plugin>
    ...
    

    assembly.xml

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