Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define the order of executions using maven-assembly-plugin

I am working on the migration of a project from Ant to Maven. The final distribution I need to deliver is a zip containing an executable jar with all its depencencies. Here is part of my pom:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-4</version>
                <configuration>
                    <finalName>ProjectDistribution</finalName>
                    <appendAssemblyId>false</appendAssemblyId>              
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>                            
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>fullQualifiedNameToMainClass</mainClass>
                            <addClasspath>true</addClasspath>                               
                        </manifest>
                    </archive>
                    <descriptors>
                        <descriptor>${project.basedir}/src/main/assembly/dep.xml</descriptor>
                    </descriptors>                      
                </configuration>
                <executions>
                    <execution>
                        <id>jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>dist</id>
                        <phase>assembly</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>                        
                </executions>                                       
            </plugin>

And here is the assembly file:

<assembly>
<id>dist</id>
<formats>
    <format>zip</format>
</formats> 
<!-- 1st approach-->
<!--files>
    <file>
        <source>
            /target/ProjectDistribution.jar
        </source>
        <outputDirectory>/</outputDirectory>
    </file>
</files-->
<fileSets>
            <!-- 2nd approach-->        
    <!--fileSet>
        <directory>/target</directory>
        <outputDirectory></outputDirectory>
        <includes>      
            <include>*.jar</include>
        </includes>
    </fileSet-->
    <fileSet>
        <directory>/HelpFiles</directory>
        <outputDirectory></outputDirectory>
        <includes>
            <include>*.*</include>
        </includes>
    </fileSet>
</fileSets>

I run 1.- mvn compile, 2.- mvn package, and 3.- mvn assembly:single

The problem I am dealing with is that

It does generate the jar with all the dependencies and it does generate the zip but it does not includ the jar in the zip. I pretty much need to figure out a way of making the assembly first generate the jar and wait until it is created (because its size is 5 MB) and then create the zip. Right now the 1st and 2nd approaches -from the assembly file- are commented out, however, I have used both and none of them seem to work.

Any help will be greatly appreciated!

Eric

like image 358
user1532449 Avatar asked Jul 17 '12 17:07

user1532449


People also ask

Are Maven plugins executed in order?

Maven builds are executions of an ordered series of phases. These phases are determined by the lifecycle that is appropriate to your project based on its packaging. Therefore, you control when a plugin's goal is executed by binding it to a particular phase.

How does Maven Assembly plugin work?

The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files. Your project can easily build distribution "assemblies" using one of the prefabricated assembly descriptors.

What is fileset in Maven?

Defines the rules for matching and working with files in a given base directory.

What is assembly descriptor in Maven?

This descriptor specifies the type of assembly archive to create, the contents of the assembly, and the ways in which dependencies or its modules are bundled with an assembly. <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


2 Answers

To get this working you need to split the <configuration> and put it into the two plugin executions:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.2-beta-4</version>
      <executions>
        <execution>
          <id>jar-with-dependencies</id>
          <phase>verify</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <finalName>ProjectDistribution</finalName>
            <appendAssemblyId>false</appendAssemblyId>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
              <manifest>
                <mainClass>fullQualifiedNameToMainClass</mainClass>
                <addClasspath>true</addClasspath>
              </manifest>
            </archive>
          </configuration>
        </execution>
        <execution>
          <id>dist</id>
          <phase>verify</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptors>
              <descriptor>${project.basedir}/src/main/assembly/dep.xml</descriptor>
            </descriptors>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

The first execution will create the jar file. The second execution will take that JAR file and put it into the ZIP file together with the other files. With this configuration, you can just execute mvn verify or mvn install to create the assembly.

There are two other things to consider:

  • You should use the verify phase to build your assembly because the jar-with-dependencies descriptor includes the project artifact itself. During the package phase the project artifact will not be ready for packaging
  • The jar-with-dependencies descriptor has very limited capabilities to create a JAR file with all dependencies. You should use the maven-shade-plugin instead.
like image 58
Stefan Ferstl Avatar answered Sep 16 '22 23:09

Stefan Ferstl


You are mixing the pre-defined jar-with-dependencies with a custom zip descriptor. You would normally want one of them - not both.

It looks like you want a zip which contains your project artifact along with its dependencies. For this you would not need to create a jar-with-dependencies. If, however, you do want a single executable jar with all the dependencies in it, then it is not clear why you need to zip it again.

like image 43
Raghuram Avatar answered Sep 20 '22 23:09

Raghuram