tags:

views:

766

answers:

1

I have a multi-module maven project with an installer sub-project. The installer will be distributed as an executable JAR. It will setup the DB and extract the WAR file to the app server. I would like to use maven to assemble this jar like so:

/META-INF/MANIFEST.MF
/com/example/installer/Installer.class
/com/example/installer/...
/server.war

The manifest will have a main-class entry pointing to the installer class. How might I get maven to build the jar in this fashion?

+1  A: 

You can build the jar using the Maven Assembly Plugin.

First, you'll need to add some information to your pom.xml plugins section to make the resulting jar executable:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>com.example.installer.Installer</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>


I recommend using a separate assembly descriptor to build the actual installer jar. Here's an example:

<assembly>
  <id>installer</id>

  <formats>
    <format>jar</format>
  </formats>

  <baseDirectory></baseDirectory>

  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <includes>
        <!-- this references your installer sub-project -->
        <include>com.example:installer</include>
      </includes>
      <!-- must be unpacked inside the installer jar so it can be executed -->
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <includes>
        <!-- this references your server.war and any other dependencies -->
        <include>com.example:server</include>
      </includes>
      <unpack>false</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>


If you've saved the assembly descriptor as "installer.xml" you can build your jar by running the assembly like this:

mvn clean package assembly:single -Ddescriptor=installer.xml


Hope this helps. Here are some additional links that you might find useful:

David Crow