Creating an assembly

A typical project requirement is to aggregate the project output along with its dependencies, modules, and other files into a single distributable archive. An assembly is a group of files, directories, and dependencies that are assembled into an archive format and distributed. Maven provides prefabricated assembly descriptors to build these assemblies. The descriptors handle common operations, such as packaging a project's artifact, along with the dependencies.

Getting ready

Maven should be set up on your system and verified to work. To do this, refer to Chapter 1, Getting Started.

How to do it...

  1. Open a Maven project for which you want to generate the assembly; in our case, project-with-assembly.
  2. Add the following plugin and configuration to the pom file:
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>2.5.3</version>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>com.packt.cookbook.App</mainClass>
          </manifest>
        </archive>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id> 
          <phase>package</phase> 
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  3. Run the following Maven command:
    mvn clean package
    
  4. Observe the output:
    [INFO] --- maven-assembly-plugin:2.5.3:single (make-assembly) @ project-with-assembly ---
    [INFO] Building jar: C:projectsapache-maven-cookbookproject-with-assembly	argetproject-with-assembly-1.0-SNAPSHOT-jar-with-dependencies.jar
    
  5. Run the created distribution JAR:
    C:projectsapache-maven-cookbookproject-with-assembly	arget>java -jar project-with-assembly-1.0-SNAPSHOT-jar-with-dependencies.jar
    07:13:25.660 [main] INFO  com.packt.cookbook.App - Hello World
    

How it works...

We made the following changes to the pom file:

  • We chose jar-with-dependencies, one of the prefabricated assembly descriptors provided by the Maven Assembly plugin. This creates a single JAR with all the dependencies of the project.
  • We also used the archive configuration to specify the main class of the project. This is to make the JAR file executable.
  • We then specified when the single goal of assembly should be run, namely, the package phase.

When Maven ran, it used the preceding configurations to assemble a JAR with dependencies in the package phase. We could run this as a normal executable JAR.

Besides predefined descriptors, the Maven Assembly plugin also allows us to create custom descriptors that can have fine-grained control over the contents of the assembly.

The Assembly plugin can also build an assembly from a multi-module project, where the modules can be part of the final assembly.

There's more...

While opening the JAR file, you would have observed that all the dependant JARs have been unpacked as well.

There's more...

This is due to the default configuration for the predefined descriptor. Let us see how to create the same distribution but retain dependant JARs as they are. To do this, we will now use one Maven JAR plugin, which uses a custom class loader to load dependant JARs within the parent JAR:

  1. Open the project for which you want to create an executable with unpackaged dependant jars (project-with-one-jar).
  2. Add the following plugin in the pom file:
        <plugin>
            <groupId>org.dstovall</groupId>
            <artifactId>onejar-maven-plugin</artifactId>
            <version>1.4.4</version>
            <executions>
              <execution>
                <id>make-assembly</id> 
                <phase>package</phase> 
                <goals>
                  <goal>one-jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
  3. Add the JAR plugin to specify the main class for the executable JAR:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
        <archive>
            <manifest>
                     <mainClass>com.packt.cookbook.App</mainClass>
            </manifest>
         </archive>
       </configuration>
    </plugin>
  4. Add the following code as the plugin binaries are not in the central Maven repository:
        <pluginRepositories>
            <pluginRepository>
                <id>onejar-maven-plugin.googlecode.com</id>
                <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
            </pluginRepository>
       </pluginRepositories>
  5. Run the following command:
    mvn package
    
  6. Run the generated executable and observe the result:
    java -jar project-with-one-jar-1.0-SNAPSHOT.one-jar.jar
    06:57:45.995 [main] INFO  com.packt.cookbook.App - Hello World
    
  7. Open the created JAR file:
    There's more...

    We can see that in contrast to the assembly JAR, the executable JAR is created without unpacking the libraries (dependencies) involved.

  8. Navigate to the lib folder in the JAR:
    There's more...

    The dependant JARs are stored in the lib folder.

See also

  • The Generating an executable JAR recipe in Chapter 10, Java Development with Maven
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.198.81