0

How to pack the executable jar in zip using maven-assembly-plugin and not the Original file i.e. .jar.original

Currently, on providing maven-assembly-plugin as dependency, it packs the jar.original in the zip.

maven-assembly-plugin definition

    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>

                    <finalName>${project.artifactId}-${project.version}</finalName>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>

assembly.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

    <id>assembly-id</id>

    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>./</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>**/*.yml</include>
                <include>**/RestCoordinator-1.0-SNAPSHOT.jar</include>
            </includes>
            
        </fileSet>
    </fileSets>

</assembly>

Here, RestCoordinator-1.0-SNAPSHOT.jar, is the jar I want in the zip. However, it takes content of RestCoordinator-1.0-SNAPSHOT.jar.original as RestCoordinator-1.0-SNAPSHOT.jar and zips that.

pom.xml* file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hsbc.gmmo.frtb</groupId>
    <artifactId>RestCoordinator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>RestCoordinator</name>
    <description>Rest API to talk to FDS/Vault system</description>

    <properties>
        <java.version>1.8</java.version>
        <aws.version>1.11.818</aws.version>
        <org.json.version>20200518</org.json.version>
        <swagger.version>3.0.0</swagger.version>
        <lz4.version>1.7.0</lz4.version>
        <opencsv.version>5.0</opencsv.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat.embed</groupId>
                    <artifactId>tomcat-embed-websocket</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.glassfish</groupId>
                    <artifactId>jakarta.el</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.yaml</groupId>
                    <artifactId>snakeyaml</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>${aws.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>com.fasterxml.jackson.dataformat</groupId>
                    <artifactId>jackson-dataformat-cbor</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>${org.json.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.vaadin.external.google</groupId>
                    <artifactId>android-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.lz4</groupId>
            <artifactId>lz4-java</artifactId>
            <version>${lz4.version}</version>
        </dependency>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>${opencsv.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.5</version>
        </dependency>

    </dependencies>



    <build>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <!-- <exclude>**/*.properties</exclude> -->
                    <!-- <exclude>trustedterms/**</exclude> -->
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.3</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>dsnexus</serverId>
                    <nexusUrl>https://dsnexus.uk.hibm.hsbc:8081/nexus</nexusUrl>
                    <!--<stagingProfileId>ff935ee1cf3eb</stagingProfileId> -->
                </configuration>
            </plugin>

            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>

                    <finalName>${project.artifactId}-${project.version}</finalName>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>
            
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.7.0.1746</version>
            </plugin>
            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.4.0.905</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludeDevtools>true</excludeDevtools>
                    <!-- <mainClass></mainClass> <layout>ZIP</layout> -->
                </configuration>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <uniqueVersion>false</uniqueVersion>
            <id>dsnexus</id>
            <url>https://dsnexus.uk.hibm.hsbc:8081/nexus/content/repositories/releases
            </url>
        </repository>
        <snapshotRepository>
            <id>dsnexus-snapshots</id>
            <url>https://dsnexus.uk.hibm.hsbc:8081/nexus/content/repositories/snapshots
            </url>
        </snapshotRepository>

    </distributionManagement>


</project>
  • What kind of application are you trying to package? Where is the `RestCoordinator` coming from? – khmarbaise Aug 20 '21 at 07:14
  • SpringBoot application , it is the name of my project. Also in POM, – Divya Kampasi Aug 20 '21 at 07:20
  • Also in POM,com.hsbc.gmmo.frtb RestCoordinator 1.0-SNAPSHOT RestCoordinator – Divya Kampasi Aug 20 '21 at 07:22
  • I'd guess that the assembly plugin simply runs *before* the `spring-boot-maven-plugin`. From what you provided it's not possible to tell certainly however. – slauth Aug 20 '21 at 07:41
  • Please show the full pom where the maven-assembly-plugin is defined ... – khmarbaise Aug 20 '21 at 08:33
  • @slauth From the console log, it shows that order as -> maven-compiler-plugin --> maven-assembly-plugin --> maven-spring-boot-plugin In the target folder, two artefacts are created : 1. RestCoordinator-1.0-SNAPSHOT.jar.original (1.5mb) 2. RestCoordinator-1.0-SNAPSHOT.jar (42mb executable jar file) Maven assembly zips taking contents of the jar.original file and not the executable jar file. – Divya Kampasi Aug 20 '21 at 08:35
  • @khmarbaise added pom code above – Divya Kampasi Aug 20 '21 at 08:40
  • So based on that I simply don't understand why you need maven-assembly-plugin. A spring boot needs to be packed by spring-boot-maven-plugin (boot etc.) Apart from that move the maven-assembly-plugin after the spring boot maven plugin in the pom file... because while two plugins are bound to the same life cycle phase there order of execution is given by the order in the pom file... – khmarbaise Aug 20 '21 at 09:31
  • Got it, this scenario worked. Thanks! – Divya Kampasi Aug 20 '21 at 12:38

1 Answers1

0

The spring-boot-maven-plugin is running after the maven-assembly-plugin. So at the time the assembly is created, there simply is no executable JAR file available yet. To fix the execution order just move the definition of the maven-assembly-plugin below the spring-boot-maven-plugin in your POM.

slauth
  • 2,667
  • 1
  • 10
  • 18