home.


Tagged: maven-assembly


Maven p16: Assembly, java-with-dependencies

The standard jar created will not include any dependencies included, as a quick way around this you can create a jar with dependencies. This is all the byte code included in the same jar, ergo it will be large.

In the build then plugins tag:

    <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <configuration>
         <descriptorRefs>
          <descriptorRef>
              jar-with-dependencies
          </descriptorRef>
         </descriptorRefs>
       </configuration>       
    </plugin>

We’re using the assembly plugin, and configuring it to create a description jar-with-dependencies.

This won’t be created automatically. We’ll have to run mvn install assembly:assembly to create a jar in target/itsname-jar-with-dependencies.jar

To ensure it is created automatically, change its configuration to tell it to run on execution by specifying a executions -> execution, with the phase and the configuration within that.

    <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <executions>
         <execution>
          <id>itsname</id>
          <phase>package</phase>
          <goals>
              <goal>single</goal>
          </goals>
          <configuration>
              <descriptorRefs>
                 <descriptorRef>
                   jar-with-dependencies
                 </descriptorRef>
              </descriptorRefs>
          </configuration>
         </execution>
       </executions>
    </plugin>
maven maven-assembly

Maven p10: Assemblies

Assemblies are archives Maven produces, a zipped up source folder, an execuatble jar file, for just two examples, alongsize your default type.

Here’s how to create a bundle of the source tree. Under project, build, plugins, place this:

    <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <version>2.2-beta-2</version>
       <executions>
         <execution>
          <id>source-it-yeah</id>
          <phase>package</phase>
          <goals> 
              <goal>single</goal>
          </goals>
          <configuration>
              <descriptorRefs>
                 <descriptorRef>
                 src
                 </descriptorRef>
              </descriptorRefs>
          </configuration>
         </execution>
       </executions>
    </plugin>

The plugin has the artifactId of the assembly plugin. The id is the name you’re going to give it. It executes in the package phase. The goal is single, run once. The configguration tells it its running the ‘src’ version of the plugin. That is, package the source.

Now, if you run mvn package, you’ll see the archives of the source tree in /target.

Another plugin type creates a executable-jar for you:

    <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <version>2.2-beta-2</version>
       <executions>
         <execution>
          <id>create-executable-jar</id>
          <phase>package</phase>
          <goals>
          <goal>single</goal>
          </goals>
          <configuration>
              <descriptorRefs>
                 <descriptorRef>
                 jar-with-dependencies
                 </descriptorRef>
              </descriptorRefs>
              <archive>
                 <manifest>
                   <mainClass>Hello</mainClass>
                 </manifest>
              </archive>
          </configuration>
         </execution>
       </executions>
    </plugin>

The only differences, except for the id, are the descriptorRef saying create an executable jar, and the artchive configuration, stating the class to execute when it’s run.

maven maven-assembly

Page 1 of 1