Search code examples
javamavenmaven-2maven-assembly-plugin

Maven assembly dependent on assembly?


I am trying to create an assembly which depends on the output of the built-in assembly jar-with-dependencies. The resulting assembly is specific to an architecture.

E.g. I want to create a ZIP which contains the JAR output by jar-with-dependencies plus a script file and a library directory.

I have tried the following. In the pom.xml file:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <descriptors>
      <descriptor>src/main/assembly/linux64.xml</descriptor>
      <descriptor>src/main/assembly/windows64.xml</descriptor>
    </descriptors>
    <archive>
      <manifest>
        <mainClass>es.entrees.BoxOffice</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

And then the descriptor files. One of them:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  <id>linux64</id>
  <formats>
    <format>zip</format>
  </formats>
  <files>
    <file>
      <source>target/${project.artifactId}-${project.version}-jar-with-dependencies.jar</source>
      <outputDirectory>/</outputDirectory>
    </file>
  </files>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    ...
  </depdendencySets>
</assembly>

But the custom descriptors are run first, while the output from jar-with-dependencies is not there yet.

Should I use submodules?


Solution

  • You need to have 2 executions for maven-assembly-plugin:

    • The first one, bound to the prepare-package phase, will create the jar-with-dependencies.
    • The other one, bound to the package, will create the zip file.

    In the build lifecycle, prepare-package phase is run before the package phase so this ensures that the JAR is built before the ZIP. Also, it documents the fact that creating the JAR prepares the final packaging, which is the ZIP file.

    Sample pom.xml would be:

    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>2.6</version>
      <executions>
        <execution>
          <id>make-jar-with-dependencies</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
              <manifest>
                <mainClass>es.entrees.BoxOffice</mainClass>
              </manifest>
            </archive>
          </configuration>
        </execution>
        <execution>
          <id>make-zip</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptors>
              <descriptor>src/main/assembly/linux64.xml</descriptor>
              <descriptor>src/main/assembly/windows64.xml</descriptor>
            </descriptors>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    With the following, running Maven with mvn clean install will correctly create the ZIP.