Home > Software engineering >  Create an executable JAR with all its dependencies and put in the ZIP file using maven assembly
Create an executable JAR with all its dependencies and put in the ZIP file using maven assembly

Time:11-17

as mentionned in the question title, is there is any way to create a zip file using maven assembly that contains all the files mentionned in the assembly.xml file and also contains a runnable jar of the project.

For the moment what I came to do is to generate a shaded jar of the project and a zip file that contains a normal jar and all the dependencies in the bin folder.

Here is my assembly.xml :

    <assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>dist</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <files>
        <file>
            <source>target/${project.artifactId}-${project.version}.jar</source>
        </file>
    </files>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <scope>compile</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
                <include>pom.xml</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
        <fileSet>
            <directory>${project.build.sourceDirectory}/src</directory>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
        <fileSet>
            <directory>lib</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*</include>
                <include>*/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>images</directory>
            <outputDirectory>images</outputDirectory>
            <includes>
                <include>*</include>
                <include>*/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}</directory>
            <includes>
                <include>*.dll</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

and he is my plugin that I am using for now:

<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <configuration>
                                <descriptor>assembly.xml</descriptor>
                            </configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.sgs.terminal.Run</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

I have tried this solution but it does not work: Maven: Build runnable jar and then add to zip with maven-assembly-plugin

EDIT: I have edited my pom.xml so there is 2 different execution: the first creates a runnable jar and the second creates the zip but this did not resolve my problem too.

CodePudding user response:

So the problem is that I was running the maven command: mvn package and this is the problem. The final pom.xml assembly plugin:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

and the final assembly.xml file:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>dist</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <files>
        <file>
            <source>target/${project.artifactId}-${project.version}-jar-with-dependencies.jar</source>
        </file>
    </files>
    <!-- IF YOU NEED TO ADD ALL THE DEPENDENCIES IN ONE FOLDER (lib) -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <scope>compile</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
                <include>pom.xml</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
        <fileSet>
            <directory>${project.build.sourceDirectory}/src</directory>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
        ....
       </fileSets>
</assembly>

And never forget to run the command : mvn assembly:assembly -DdescriptorId=jar-with-dependencies package

  • Related