Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Assembly plugin doesnt copy jar-with-dependences to the zip file

Tags:

maven

maven-3

I do "mvn clean package" and it creates a zip file (based on my assembly.xml) with snapshot.jar, then it creates the zip file and lastly creates the jar-with-dependencies.jar

I think because zip file was created before jar-with-dependencies.jar was created, it was never copied to the zip file.

How can I force maven to copy both snapshot.jar and jar-with-dependencies.jar into the zip file (in other words, how can I force jar-with-dependencies is built first before assembly goal is invoked so that all jars would be copied to the zip file)

Here is pom.xml

    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycomonay</groupId>
<artifactId>artid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>art</name>
<description>artdescription</description>
<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.13</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.13</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.6</version>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.4.3</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>

</dependencies>
<build>
    <plugins>
        <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> 
            <version>2.4</version> <configuration> <archive> <manifestEntries> <Built-By>xyz</Built-By> 
            <Class-Path>.</Class-Path> </manifestEntries> </archive> </configuration> 
            </plugin> -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArgument>-Xlint:unchecked</compilerArgument>
            </configuration>

        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>

            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifestEntries>
                        <Built-By>abc</Built-By>
                        <Class-Path>.</Class-Path>
                    </manifestEntries>
                    <manifest>
                        <!-- <addClasspath>true</addClasspath> -->

                        <mainClass>com.mycomonay.Main</mainClass>
                    </manifest>
                </archive>


                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>

                </descriptors>
            </configuration>
                <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <!-- <resources> <resource> <directory>src/main/resources</directory> </resource> 
        </resources> -->
</build>

Here is 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>bin</id>
        <!--  <baseDirectory>/</baseDirectory> -->
            <formats>
      <format>zip</format>
          </formats>
       <includeBaseDirectory>false</includeBaseDirectory>
       <fileSets>
          <fileSet>
             <directory>${project.build.directory}</directory>
             </fileSet>
           <fileSet>
            <directory>${project.basedir}/scripts</directory>
               <outputDirectory>scripts</outputDirectory>
              <includes>
                  <include>*</include>
               </includes>
             </fileSet>
<fileSet>
        <directory>${project.build.directory}</directory>
        <outputDirectory>bin</outputDirectory>
        <includes>
            <include>*with-dependencies.jar</include>
        </includes>
    </fileSet>

like image 290
kashili kashili Avatar asked Oct 21 '25 01:10

kashili kashili


1 Answers

You can definitely separate the single goals into two executions (see https://stackoverflow.com/a/8726969/3114959 or https://stackoverflow.com/a/15799254/3114959).
Maven executes things in the same order as they appear in the effective pom (as long as the belong to the same phase) so if you specify both executions separated, you should be fine.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>create-jar-with-dependencies</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
                <execution>
                    <id>make-the-zip</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

as a result, you'll see 2 executions during build:

[INFO] --- maven-assembly-plugin:2.2-beta-5:single (create-jar-with-dependencies) @ artid ---
[INFO] ... some more output ...
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (make-the-zip) @ artid ---

the latter execution (the make-the-zip) can definitely include the results of the former one (the create-jar-with-dependencies).

like image 177
Stepan Vavra Avatar answered Oct 24 '25 18:10

Stepan Vavra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!