3

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.

maryem neyli
  • 467
  • 3
  • 20
  • Just to clarify, do you want to use the assembly plugin only and have nothing to do with the shade plugin for solving this? – Nagaraj Tantri Nov 16 '21 at 02:32
  • @NagarajTantri yes I tried to use shade plugin, one jar plugin but always no solution ! I have tried to have two execution in the same assembly plugin but no, it did not work ! – maryem neyli Nov 16 '21 at 08:19
  • @maryemneyli please look at this [answer](https://stackoverflow.com/questions/7837778/maven-best-practice-for-creating-ad-hoc-zip-artifact). You can just have two stages in the same phase, the first one creates the jar, the second one creates the zip file. The order of execution in the same phase is the same in which they are described in your pom file. – 3Fish Nov 16 '21 at 12:04
  • @3Fish I have already tested it but it does not work ! for the moment I am capable to create a Runnable jar an to create a normal jar(without the dependencies) and I am able to add the normal jar with all the dependencies in the lib folder into the zip file but my only problem is how to add a runnable jar (jar contains all the dependencies) into the zip file ?! – maryem neyli Nov 16 '21 at 14:47

1 Answers1

5

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

maryem neyli
  • 467
  • 3
  • 20